Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Reduce output noise #316

Merged
merged 4 commits into from
Jun 28, 2024
Merged

test: Reduce output noise #316

merged 4 commits into from
Jun 28, 2024

Conversation

dcalhoun
Copy link
Member

@dcalhoun dcalhoun commented Jun 27, 2024

Proposed Changes

Reduce the amount of noise found in test output to decrease the likelihood that we introduce new errors, warnings, or failures without noticing. Long term, we might consider leveraging @wordpress/jest-console to trigger a test failure whenever console output exists occurs from a test.

  • Prevent invalid React HTML attribute error.
  • Exclude expected console error and warning message in test output.
  • Replace use of deprecated fs.rmdir with fs.rm.
  • Filter verbose console messages.

Additional context can be found in the body of each commit message.

Testing Instructions

  1. npm run test
  2. Verify test output contains no warnings1 or logs.
Before After
before after

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

Footnotes

  1. The one remaining warning relates to an identifiable issue stemming from usage of @php-wasm/node.

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();
```
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)
```
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)
```
Reduce the noise in test results caused by various usages of `console`
within the source.
@dcalhoun dcalhoun self-assigned this Jun 27, 2024
@dcalhoun dcalhoun marked this pull request as ready for review June 27, 2024 18:36
@@ -8,7 +8,7 @@ describe( 'createCodeComponent', () => {
const contextProps = {
blocks: [],
updateMessage: jest.fn(),
projectPath: '/path/to/project',
siteId: '1',
Copy link
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.

@@ -18,7 +18,7 @@ describe( 'executeWPCli', () => {
fs.writeFileSync( path.join( tmpPath, 'index.php' ), '' );
} );
afterAll( () => {
fs.rmdirSync( tmpPath, { recursive: true } );
Copy link
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)

Comment on lines +36 to +37
console.error = jest.fn();
console.warn = jest.fn();
Copy link
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.

Comment on lines +33 to +35
* 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.
Copy link
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.

@dcalhoun dcalhoun requested a review from a team June 27, 2024 18:36
Copy link
Contributor

@katinthehatsite katinthehatsite left a comment

Choose a reason for hiding this comment

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

The changes look good on my end. I tested the changes against the trunk and the output looks much cleaner 👍 Thanks for making this improvement

@dcalhoun dcalhoun merged commit 5e28b90 into trunk Jun 28, 2024
13 checks passed
@dcalhoun dcalhoun deleted the test/reduce-output-noise branch June 28, 2024 11:43
jest-setup.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants