DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Smart Home Dan
Smart Home Dan

Posted on

Jest: Could not find source file error

Occasionally I've been seeing the following error when running my Jest test suite.

> @module/module@1.2.9 test C:\_Development\project
> jest

 FAIL  __tests__/resources/project.router.test.ts 
  ● Test suite failed to run

    Could not find source file: 'C:\_Development\project\__tests__\resources\project.actions.test.ts'.

      at getValidSourceFile (node_modules/typescript/lib/typescript.js:135637:29)
      at Object.getSemanticDiagnostics (node_modules/typescript/lib/typescript.js:135859:36)
      at doTypeChecking (node_modules/ts-jest/dist/compiler/language-service.js:11:35)
      at node_modules/ts-jest/dist/compiler/language-service.js:96:25
          at Array.forEach (<anonymous>)
      at compileFn (node_modules/ts-jest/dist/compiler/language-service.js:94:26)
      at Object.compile (node_modules/ts-jest/dist/compiler/instance.js:90:25)
      at TsJestTransformer.process (node_modules/ts-jest/dist/ts-jest-transformer.js:94:41)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:481:35)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:586:40)
Enter fullscreen mode Exit fullscreen mode

The source files always exist in the right location - it seems like actually Jest isnt always calling the real file, but grabbing it from the cache?

The solution is to pass the following arguemnts

jest --clearCache
Enter fullscreen mode Exit fullscreen mode

Obviously, alot of the time, we arn't running Jest directly, its in our package.json. In that case you can pass in an extra -- to tell the npm runner to pass the argument down into the underlying call.

npm run test -- --clearCache
Enter fullscreen mode Exit fullscreen mode

Hope this helps!

Top comments (4)

Collapse
viniciusveu profile image
VinΓ­cius Vedovotto • Edited on

u saved me, thks!

Collapse
ezequieltejada profile image
Ezequiel E. Tejada

Thank you Dan!

Collapse
crhist0 profile image
Crhistian de Oliveira

Thank you! That just did the trick

Collapse
tbrixey profile image
Trevor

Coming at you in early 2022, glad you posted this. πŸ’―

🌚 Life is too short to browse without dark mode