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 (5)

akaustav profile image
Ameet Kaustav

Thank you for posting this, @smarthomedan. This helped me too!

Maybe it's just me, but on jest@27.5.1 with @angular-builders/jest@12.1.2, I get the following warning:

Support for camel case arguments has been deprecated and will be removed in a future major version.
Use '--clear-cache' instead of '--clearCache'.
Enter fullscreen mode Exit fullscreen mode

You might want to update the post for versions of jest which have deprecated support for camelCase arguments.

For older versions of jest, use the --clearCache argument.
For newer versions of jest, use the --clear-cache argument.

viniciusveu profile image
Vinícius Vedovotto • Edited

u saved me, thks!

ezequieltejada profile image
Ezequiel E. Tejada

Thank you Dan!

crhist0 profile image
Crhistian de Oliveira

Thank you! That just did the trick

tbrixey profile image

Coming at you in early 2022, glad you posted this. 💯