WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

Running unit tests in watch mode throws error #48427

Open getdave opened 1 year ago

getdave commented 1 year ago

For a long while now I've tried to run unit tests with watch and it always fails.

Example command:

npm run test:unit:watch packages/block-editor/src/hooks/test/duotone.js

Here's the error (with parts redacted):

Error: Failed to initialize watch plugin "node_modules/jest-watch-typeahead/filename.js":

  ● Test suite failed to run

    file:///{{PATH_TO_MY_REPO}}/gutenberg/node_modules/jest-watch-typeahead/build/file_name_plugin/prompt.js:4
    import { PatternPrompt, printPatternCaret, printRestoredPatternCaret } from 'jest-watcher';
                            ^^^^^^^^^^^^^^^^^
    SyntaxError: Named export 'printPatternCaret' not found. The requested module 'jest-watcher' is a CommonJS module, which may not support all module.exports as named exports.
    CommonJS modules can always be imported via the default export, for example using:

    import pkg from 'jest-watcher';
    const { PatternPrompt, printPatternCaret, printRestoredPatternCaret } = pkg;

      at async requireOrImportModule (node_modules/@jest/core/node_modules/jest-util/build/requireOrImportModule.js:65:32)
      at async watch (node_modules/@jest/core/build/watch.js:337:34)
      at async _run10000 (node_modules/@jest/core/build/cli/index.js:311:7)
      at async runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

I don't understand why this is happening. I wonder if folks experienced in test setup might be able to help. Particularly @kevin940726.

t-hamano commented 1 year ago

It appears to be working correctly in my environment.

Example command:

npm run test:unit:watch packages/block-editor/src/hooks/duotone.js

I think you need to specify a test file:

npm run test:unit:watch packages/block-editor/src/hooks/test/duotone.js

The following is how it works. You can see the watch mode starting at around 28 seconds.

https://user-images.githubusercontent.com/54422211/221341098-1efd8616-0e3b-4769-9b73-b297e3beb824.mp4

kevin940726 commented 1 year ago

I'm not getting that error either. Maybe try reinstalling the dependencies to be sure?

getdave commented 1 year ago

It's been a problem for a while for me. This isn't a new thing. I'm installed dependencies.

I'm using zsh shell and wondering if that's having any form of impact on watcher or something like that.

kevin940726 commented 1 year ago

I'm also using zsh so I doubt that.

What's your Node.js version? And npm version? I'm not getting the error even when I installed the latest Node version though.

t-hamano commented 1 year ago

Is this due to the Node version being lower than 14.17.0 as reported in create-react-app issue?

getdave commented 1 year ago

I'll see if upgrading Node works.