ts-jest: With cached results, changes to type definitions are not detected

Issue :

Given a type declaration in core code, and a reference to that type definition in jest code, type-safety is not always enforced.

Expected behavior :

Changes to type definitions should be detected (failing to compile where appropriate).

Minimal repo :

  1. Unzip the attached zip file and run yarn and then yarn test (should succeed).
  2. Change the type definition in thing.ts to contain b: number
  3. Run yarn test again. Due to the cache, it succeeds and should not. It will (correctly) fail if you remove the cache folder “jestCache”

thing.ts contains:

export type Thing = {
	a: number;
	// b: number;
};

thing.test.ts contains:

export const thing: Thing = { a: 1 };

jestCache.zip

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 24 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I also have the same issue, it is so annoying, we had to disable the cache to prevent errors. Please fix this issue.

So far it seems to do the job. I made a new test which attempts to dereference an invalid property on a class. Once the test suite runs, I see a failure as expected. Upon making a change to the source code and waiting for the test suite to rerun ts-jest now properly sees the newly create property in the class and the test passes as expected.

I ran jest with --watch and --watchAll same results.

OK, your fix looks good in watch mode. Starting with the original zip file (but upgraded to 25.3.0), I verified that running yarn test --watchAll failed to detect changes in watch mode as expected.

  • I emptied node_modules\ts-jest and spliced in the contents from the tgz file.
  • Removed the jestCache folder
  • Ran yarn test --watchAll
  • Messed around altering the Thing type, even deleting it, and then restoring it.
  • At all points correct reports were given.

LGTM 😃

I’ll try to publish a new version tomorrow

the fix will be in the next release version. Please wait 😃

Note: watch mode fix isn’t included in the fix for this issue. Watch mode seems to be more complex to come up with the fix. For watch mode, please follow #943 for the update.

cc @kulshekhar

Running into the same issue here. Using --no-cache doesn’t alleviate the issue for me. The only out for me was to just not use ts-jest at all. And fall back to jest’s babel configuration as described here.

Yeah @drrnkin, I know that feeling too. I posted it because we were having some issues with errors in our tests, that were detected in CI but not in our local development environment.

We are workarounding this issue by passing --no-cache flag to jest