Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ if ( typeof window !== 'undefined' ) {
nock.disableNetConnect();
nock.enableNetConnect( 'raw.githubusercontent.com' );

/**
* TODO: Replace this manual filter with a more robust logger that can be
* disabled for the testing environment. Consider enabling a lint rule that
* discourages direct use of `console` methods.
Comment on lines +33 to +35

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this filtering approach to be a quick solution, but a more ideal solution would be a central logger utility that manages and formats logging in a consistent manner. This might allow us to disable logs in certain contexts (e.g., test runs via a VERBOSE=false environment variable) and ensure the same information (e.g., helpful metadata) is included for each log.

*/
function filteredConsole( level: ( ...args: any[] ) => void ) {
const ignoredMessages = [
'Loaded user data from ',
'Saved user data to ',
'Server start',
'Would have bumped stat: ',
'Starting new session',
'App version: ',
'Built from commit: mock-hash',
'Local timezone: UTC',
'App locale: ',
'System locale: ',
'Used language: ',
];

return ( ...args: any[] ) => {
if ( ignoredMessages.some( ( ignoredMessage ) => args[ 0 ].includes( ignoredMessage ) ) ) {
Comment thread
fluiddot marked this conversation as resolved.
return;
}

level( ...args );
};
}
console.log = filteredConsole( console.log );
console.error = filteredConsole( console.error );
console.warn = filteredConsole( console.warn );
console.info = filteredConsole( console.info );

// We consider the app to be online by default.
jest.mock( './src/hooks/use-offline', () => ( {
useOffline: jest.fn().mockReturnValue( false ),
Expand Down
4 changes: 2 additions & 2 deletions src/components/tests/assistant-code-block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe( 'createCodeComponent', () => {
const contextProps = {
blocks: [],
updateMessage: jest.fn(),
projectPath: '/path/to/project',
siteId: '1',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mirrors recent changes from #300.

messageId: 1,
};
const CodeBlock = createCodeComponent( contextProps );
Expand Down Expand Up @@ -152,7 +152,7 @@ describe( 'createCodeComponent', () => {
},
],
updateMessage: jest.fn(),
projectPath: '/path/to/project',
siteId: '1',
messageId: 1,
};
const CodeBlock = createCodeComponent( contextProps );
Expand Down
9 changes: 8 additions & 1 deletion src/tests/execute-wp-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe( 'executeWPCli', () => {
fs.writeFileSync( path.join( tmpPath, 'index.php' ), '' );
} );
afterAll( () => {
fs.rmdirSync( tmpPath, { recursive: true } );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the observed log, this method is deprecated.

(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)
fs.rmSync( tmpPath, { recursive: true } );
} );

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

it( 'should return error if wp-cli command does not exist', async () => {
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
console.error = jest.fn();
console.warn = jest.fn();
Comment on lines +36 to +37

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in the above test description, we expect an error to occur. Silencing it for test output reduces noise.

const args = [ 'yoda' ];

const result = await executeWPCli( tmpPath, args );
Expand All @@ -39,6 +43,9 @@ describe( 'executeWPCli', () => {
expect( result.stderr ).toContain(
"'yoda' is not a registered wp command. See 'wp help' for available commands."
);

console.error = originalConsoleError;
console.warn = originalConsoleWarn;
} );

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