Jest: Could not find source file error

smarthomedan

Smart Home Dan

Posted on September 9, 2021

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!

πŸ’– πŸ’ͺ πŸ™… 🚩
smarthomedan
Smart Home Dan

Posted on September 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related