DEV Community

Smart Home Dan
Smart Home Dan

Posted on

11

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!

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (5)

Collapse
 
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.

Collapse
 
viniciusveu profile image
Vinícius Vedovotto • Edited

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. 💯

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay