Skip to content

Commit 5e28b90

Browse files
authored
test: Reduce output noise (#316)
* test: Prevent invalid React HTML attribute error This context property was changed in the component itself, but overlooked in the test as it did not create a failure, just an error log. ``` console.error Warning: React does not recognize the `projectPath` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `projectpath` instead. If you accidentally passed it from a parent component, remove it from the DOM element. at code at CodeBlock (/Users/davidcalhoun/Sites/a8c/studio/src/components/assistant-code-block.tsx:22:32) 15 | 16 | it( 'should render inline styles for language-generic code', () => { > 17 | render( <CodeBlock children="example-code" /> ); | ^ 18 | 19 | expect( screen.getByText( 'example-code' ) ).toBeVisible(); 20 | expect( screen.queryByText( 'Copy' ) ).not.toBeInTheDocument(); ``` * test: Exclude expected console error and warning message in test output Minimize unnecessary noise in test output. ``` console.warn PHP.run() output was: #!/usr/bin/env php at _NodePHP.run (node_modules/@php-wasm/node/index.cjs:72997:17) console.error PHPExecutionFailureError2: PHP.run() failed with exit code 1 and the following output: at _NodePHP.run (/Users/davidcalhoun/Sites/a8c/studio/node_modules/@php-wasm/node/index.cjs:72998:23) { response: _PHPResponse { httpStatusCode: 500, headers: { 'x-powered-by': [Array], 'content-type': [Array] }, bytes: Uint8Array(19) [ 35, 33, 47, 117, 115, 114, 47, 98, 105, 110, 47, 101, 110, 118, 32, 112, 104, 112, 10 ], exitCode: 1, errors: '' }, source: 'request' } at _NodePHP.run (node_modules/@php-wasm/node/index.cjs:73003:17) ``` * test: Replace use of deprecated `fs.rmdir` with `fs.rm` Addresses warning logged in test output: ``` (node:82794) [DEP0147] DeprecationWarning: In future versions of Node.js, fs.rmdir(path, { recursive: true }) will be removed. Use fs.rm(path, { recursive: true }) instead (Use `node --trace-deprecation ...` to show where the warning was created) ``` * test: Filter verbose console messages Reduce the noise in test results caused by various usages of `console` within the source.
1 parent 585ec0d commit 5e28b90

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎jest-setup.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ if ( typeof window !== 'undefined' ) {
2929
nock.disableNetConnect();
3030
nock.enableNetConnect( 'raw.githubusercontent.com' );
3131

32+
/**
33+
* TODO: Replace this manual filter with a more robust logger that can be
34+
* disabled for the testing environment. Consider enabling a lint rule that
35+
* discourages direct use of `console` methods.
36+
*/
37+
function filteredConsole( level: ( ...args: any[] ) => void ) {
38+
const ignoredMessages = [
39+
'Loaded user data from ',
40+
'Saved user data to ',
41+
'Server start',
42+
'Would have bumped stat: ',
43+
'Starting new session',
44+
'App version: ',
45+
'Built from commit: mock-hash',
46+
'Local timezone: UTC',
47+
'App locale: ',
48+
'System locale: ',
49+
'Used language: ',
50+
];
51+
52+
return ( ...args: any[] ) => {
53+
if ( ignoredMessages.some( ( ignoredMessage ) => args[ 0 ].includes( ignoredMessage ) ) ) {
54+
return;
55+
}
56+
57+
level( ...args );
58+
};
59+
}
60+
console.log = filteredConsole( console.log );
61+
console.error = filteredConsole( console.error );
62+
console.warn = filteredConsole( console.warn );
63+
console.info = filteredConsole( console.info );
64+
3265
// We consider the app to be online by default.
3366
jest.mock( './src/hooks/use-offline', () => ( {
3467
useOffline: jest.fn().mockReturnValue( false ),

‎src/components/tests/assistant-code-block.test.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe( 'createCodeComponent', () => {
88
const contextProps = {
99
blocks: [],
1010
updateMessage: jest.fn(),
11-
projectPath: '/path/to/project',
11+
siteId: '1',
1212
messageId: 1,
1313
};
1414
const CodeBlock = createCodeComponent( contextProps );
@@ -152,7 +152,7 @@ describe( 'createCodeComponent', () => {
152152
},
153153
],
154154
updateMessage: jest.fn(),
155-
projectPath: '/path/to/project',
155+
siteId: '1',
156156
messageId: 1,
157157
};
158158
const CodeBlock = createCodeComponent( contextProps );

‎src/tests/execute-wp-cli.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe( 'executeWPCli', () => {
1818
fs.writeFileSync( path.join( tmpPath, 'index.php' ), '' );
1919
} );
2020
afterAll( () => {
21-
fs.rmdirSync( tmpPath, { recursive: true } );
21+
fs.rmSync( tmpPath, { recursive: true } );
2222
} );
2323

2424
it( 'should execute wp-cli version command and return stdout and stderr', async () => {
@@ -31,6 +31,10 @@ describe( 'executeWPCli', () => {
3131
} );
3232

3333
it( 'should return error if wp-cli command does not exist', async () => {
34+
const originalConsoleError = console.error;
35+
const originalConsoleWarn = console.warn;
36+
console.error = jest.fn();
37+
console.warn = jest.fn();
3438
const args = [ 'yoda' ];
3539

3640
const result = await executeWPCli( tmpPath, args );
@@ -39,6 +43,9 @@ describe( 'executeWPCli', () => {
3943
expect( result.stderr ).toContain(
4044
"'yoda' is not a registered wp command. See 'wp help' for available commands."
4145
);
46+
47+
console.error = originalConsoleError;
48+
console.warn = originalConsoleWarn;
4249
} );
4350

4451
it( 'should return the correct version of WP-CLI', async () => {

0 commit comments

Comments
 (0)