ts-jest 26.1.2 - "cannot find source file"

Created on 5 Apr 2020  ·  44Comments  ·  Source: kulshekhar/ts-jest

🐛 Bug Report

When attempting to run jest tests with VSCode debugger in 25.3.1, I keep getting error "cannot find source file" after making seemingly innocuous changes to the spec or tested file. Reverting the changes to the source file allows the tests to run. Reverting to 25.2.1 fixes the error.

System:
    OS: OSX 10.15.4

Npm packages:
    jest: 25.2.7
    ts-jest: 25.3.1
    typescript: 3.8.3
Regression

Most helpful comment

yes, running jest --clearCache allows the test to pass, but then changing the value again (e.g. to qum = 'quo') causes it to immediately fail again

All 44 comments

Would you please provide us the reproduce repo ? The information is very limited so it's very difficult for us to investigate. We need:

  • a repo
  • any useful loggings
  • steps to reproduce

Also please follow the bug report template. Those are the useful information that can help us.

Logs:

libs/features/provider-details/src/lib/services/secondary-nav.service.spec.ts
  ● Test suite failed to run

    Could not find source file: '/Users/pete.burkindine/git/ccxp/ccxp-client/libs/features/provider-details/src/lib/services/secondary-nav.service.spec.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)

Working on a repro

@ahnpnl https://github.com/pburkindine/tsjest-cannot-find-source-file

Just adding foo = 'bar'; to this file produced the error: https://github.com/pburkindine/tsjest-cannot-find-source-file/blob/master/apps/tsjest-cannot-find-source-file/src/app/app.component.ts

(this is the test file: https://github.com/pburkindine/tsjest-cannot-find-source-file/blob/master/apps/tsjest-cannot-find-source-file/src/app/app.component.spec.ts)

After running it the first time and getting the error, I noticed I needed to upgrade jest in the repro to match my environment. After the upgrade, the test passed. Then I changed "foo = 'bar'" to "baz = 'quu'" in the component and ran the test, and it failed again with the log below:

Log:

╰─  cd /Users/pete.burkindine/git/test/tsjest-cannot-find-source-file ; /Users/pete.burkindine/.nvm/versions/node/v12.13.1/bin/node --inspect-brk=31549 node_modules/.bin/jest apps/tsjest-cannot-find-source-file/src/app/app.component.spec.ts --config /Users/pete.burkindine/git/test/tsjest-cannot-find-source-file/.debug/jest.config.js --runInBand --runTestsByPath --detectOpenHandles 
Debugger listening on ws://127.0.0.1:31549/74d796b8-cec8-4a6b-a0d1-610530d66cdb
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
 FAIL  apps/tsjest-cannot-find-source-file/src/app/app.component.spec.ts
  ● Test suite failed to run

    Could not find source file: '/Users/pete.burkindine/git/test/tsjest-cannot-find-source-file/apps/tsjest-cannot-find-source-file/src/app/app.component.spec.ts'.

      at getValidSourceFile (node_modules/typescript/lib/typescript.js:135637:29)
          at Array.forEach (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        4.482s, estimated 5s
Ran all test suites within paths "apps/tsjest-cannot-find-source-file/src/app/app.component.spec.ts".
Waiting for the debugger to disconnect...
[1]    22054 killed     /Users/pete.burkindine/.nvm/versions/node/v12.13.1/bin/node    --config    

does clear jest cache and rerun help ?

yes, running jest --clearCache allows the test to pass, but then changing the value again (e.g. to qum = 'quo') causes it to immediately fail again

oh I think I know, it's related to the fix for #1390 . For some reasons it doesn't work for your case

I have the same issue with all my typescript projects since the update to 25.3.1. After a successful test run, as soon as I change anything in a source file and run the test again, I get the same error. Reverting the change allows the test to complete.

At the moment, all I can do is keep running jest --clearCache every time I change something, or roll back to a previous version of ts-jest.

I set up an example repo that mimics my project setup if it's of any use:
https://github.com/rav2040/ts-jest-error-could-not-find-source-file

I included a GitHub CI test workflow in the repo. If you look at the workflow logs you'll see the test managed to complete even after I changed the value of 'foo' in the source file.

However, when I do the same thing on my own machine I get the following:

 FAIL  __tests__/index.test.ts
  ● Test suite failed to run

    Could not find source file: '/mnt/f/dev/projects/ts-jest-error-could-not-find-source-file/__tests__/index.test.ts'.

      at getValidSourceFile (node_modules/typescript/lib/typescript.js:135637:29)
          at Array.forEach (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.701s
Ran all test suites.

I also tried cloning the repo to a Digital Ocean droplet and running it there, and I still got the same error.

thank you guys for the information, will look into it asap. Stay tune :)

Updated: would you guys please test the fix with this tgz file https://drive.google.com/open?id=1QRnkDOIy7BXQdSjcybzveBkmE1uCU8h5

thank you guys for the information, will look into it asap. Stay tune :)

Updated: would you guys please test the fix with this tgz file https://drive.google.com/open?id=1QRnkDOIy7BXQdSjcybzveBkmE1uCU8h5

It works for me 👍

@ahnpnl yeah, fixes it for me as well, thanks

thank you guys, will open a PR for it.

👋🏽just hit this same issue in VSCode, I can workaround by running jest --clearCache && jest, so it's not a blocker. I was just wondering about the ETA of a new release

👋🏽just hit this same issue in VSCode, I can workaround by running jest --clearCache && jest, so it's not a blocker. I was just wondering about the ETA of a new release

cc @kulshekhar

This bug is still present on version ^25.3.0
The issue is almost the same:
modified source file without modifying the test file and boom
"Could not find source file: '/home/user/project/test/User.test.ts'

@mordigann228 you can: either use 25.2.1 or use clear cache or wait for the next release :)

Upgrading to the latest (^25.5.1 at the time of writing) solved the issue for me. Thanks!

Happening again on OSX ^26.1.0

Downgrading to ^25.5.1 solves the issue

@lmanerich I cannot reproduce the issue with v26.1.3 on the repo https://github.com/rav2040/ts-jest-error-could-not-find-source-file and the repo https://github.com/pburkindine/tsjest-cannot-find-source-file

please try to clear your cache and try again. Make sure you are using v26.1.3

If you still encounter the issue, please link your repo here so we can check :)

Also having this issue when I upgraded the following:

-  "@types/jest": "^26.0.4",
+ "@types/jest": "^26.0.5",
- "ts-jest": "^26.1.2",
+ "ts-jest": "^26.1.3",
- "typescript": "^3.9.6",
+ "typescript": "^3.9.7",

Clearing the cache resolves the issue.

@impulse does clear cache resolve completely the issue or after clearing cache, you still have the issue after a while ?

@ahnpnl updating to v26.1.3 solved the issue.
Was with v26.1.2 and "jest:26.1.0".
Thanks

@impulse does clear cache resolve completely the issue or after clearing cache, you still have the issue after a while ?

didn't return so far, will let you know if that changes

I'm currently experiencing this issue with ts-jest 26.1.3 and jest 26.1.0 - I'll try a cache clear.

Nope, doesn't solve it.

would you please provide a reproduce scenario ?

Here you go:

https://github.com/dwjohnston/ts-jest-file-repro

I'm macOS 10.15.5.

I'll jump on to my other computer, see if it's happening there.

freshly clone, install dependencies and run tests with my Mac. I couldn't see the issue. Do you have specific steps which you did to see the error ?

Hmmmm, fresh clone for me isn't producing it either. :(

I think something happened to the cache of your previous computer.

I'm on the same computer. And that repro was created just by creating a new repo and running install commands. I wonder if VSCode is messing with it?

npm cache clean --force

doesn't seem to solve the issue in the original repo.

you need to clear jest cache, not npm cache

Yup, clearing the jest cache solves it.

Same here with v26.1.3. Jest tries to run deleted spec files.

We have a heavy pipeline with hundreds of tests and clearing Jest cache is not an option as it drastically increases running time. Are there any other options?

after clearing cache, does it still happen after using for a while ? The problem is because new version cannot reuse the old cache, therefore clear cache is required for 1st run.

first time it happened on v26.1.2. we bumped to v26.1.3 and the issue vanished away to re-appear the next day. If it helps, we have a nightly job that clears and rebuilds Jest cache so I'm 100% sure the issue happened on v26.1.3 with the updated cache.

it would be nice if you can provide a reproduce scenario. I tested with the 2 repos provided here but the issue doesn't appear.

Same here with jest and ts-jest versions 26.1.0

Error output:

 FAIL  test/dashboard/DashboardContainer.test.tsx
  ● Test suite failed to run

    Could not find source file: '/build/test/dashboard/DashboardChooserContainer.test.tsx'.

      at getValidSourceFile (node_modules/typescript/lib/typescript.js:139175:29)
      at Object.getSemanticDiagnostics (node_modules/typescript/lib/typescript.js:139398:36)
      at doTypeChecking (node_modules/ts-jest/dist/compiler/language-service.js:93:35)
      at compileFn (node_modules/ts-jest/dist/compiler/language-service.js:263:29)
      at Object.compile (node_modules/ts-jest/dist/compiler/instance.js:38:21)
      at TsJestTransformer.process (node_modules/ts-jest/dist/ts-jest-transformer.js:86:41)

DashboardChooserContainer.test.tsx was not available, as it was renamed to DashboardContainer.test.tsx

Probably you can reproduce the issue by running the tests, then renaming one of the test files.

I believe this issue should be reopened.

do you use watch mode or non-watch mode ? Hmm I tried renaming but I don't see the issue either. I might know how to fix it but need reproduce steps that can verify the fix.

I think the steps are:

  • Run test with cleared jest cache
  • Rename the a test file
  • Change something in the one of the imported modules (the modules which the test file imports)
  • Run test again and error will be thrown

Hi @magwas, @x87, @dwjohnston, @lmanerich, @impulse

v26.1.4-alpha.0 is out with the fix. Would you guys please test it by install ts-jest@next ?

If it works, I will publish a new release and close this issue.

Let me know

@ahnpnl using this version right now, so far so good.
Thank you for your support

hi, does 26.1.4-alpha.0 solve the issue completely ? If yes, maybe 26.1.4 can be released ?

no issues for me

v26.1.4 is out, I will close this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Vinnl picture Vinnl  ·  3Comments

artola picture artola  ·  3Comments

stangerjm picture stangerjm  ·  4Comments

remcohaszing picture remcohaszing  ·  4Comments

AlexGellert picture AlexGellert  ·  4Comments