Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/d3808f32191cd662502d99803e5decf2 to your computer and use it in GitHub Desktop.
Save kenmori/d3808f32191cd662502d99803e5decf2 to your computer and use it in GitHub Desktop.
test with Jest fails with `Could not locate module @/xxx`

Suppose you add async to componentDidmount and start a snapshot test

-  public componentDidMount() {
+  public async componentDidMount() {
     const { setup } = this.props;
     setup();
   }

run test

you will show below, sometime

  ● Test suite failed to run

   Configuration error:
   
   Could not locate module @/containers
   import OpenDialogButton from  mapped as:
   /Users/morita.kenji/Desktop/projectName/src/containers.
   
   Please check your configuration for these entries:
   {
     "moduleNameMapper": {
       "/@\/(.*)/": "/Users/morita.kenji/Desktop/projectName/src/$1"
     },
     "resolver": null
   }

     at createNoMappedModuleFoundError (node_modules/jest-runtime/node_modules/jest-resolve/build/index.js:501:17)
         at Array.reduce (<anonymous>)
     at SearchSource.findRelatedTests (node_modules/@jest/core/build/SearchSource.js:280:30)
     at SearchSource.findTestRelatedToChangedFiles (node_modules/@jest/core/build/SearchSource.js:363:14)

To fix

add option your command --watchAll in my case,

npm test -- --u --watchAll

npm test -- --u --watchAll is just ailias, In fact tsc --noEmit && react-app-rewired test --watchAll)

not npm test -- --u --watch

If you try it with --watch, you'll get an error again

jestjs/jest#4883

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment