question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Tests fail when using TypeScript project references

See original GitHub issue

šŸ› Bug Report

Iā€™m in the process of migrating one of my projects to use Typescript project references, but after migration the tests that validated error types failed.

Iā€™ve created a repo that has one directory with a very small project where tests succeed, and one directory with a project using typescript projects references where the tests fail. The projects are identical with the exception of project references. Iā€™m not saying that ts-jest is to blame. It could as well be my understanding of project references or jest.

To Reproduce

git clone https://github.com/FantasticFiasco/jest-error-type-issue.git
cd jest-error-type-issue/does-not-work
yarn
yarn test

Expected behavior

I would expect the test to pass, because in the same git repository there is a directory called works with an identical project except the project references that has a test that passes.

Link to repo (highly encouraged)

https://github.com/FantasticFiasco/jest-error-type-issue

Debug log:

{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"25.5.1"},"message":"creating jest presets not handling JavaScript files","sequence":1,"time":"2020-05-15T16:59:54.105Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"25.5.1"},"message":"creating Importer singleton","sequence":2,"time":"2020-05-15T16:59:55.006Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"25.5.1"},"message":"creating jest presets not handling JavaScript files","sequence":3,"time":"2020-05-15T16:59:55.010Z"}
{"context":{"actualVersion":"25.5.4","expectedVersion":">=25 <26","logLevel":20,"namespace":"versions","package":"ts-jest","version":"25.5.1"},"message":"checking version of jest: OK","sequence":4,"time":"2020-05-15T16:59:55.011Z"}
{"context":{"baseOptions":{},"logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"25.5.1"},"message":"created new transformer","sequence":5,"time":"2020-05-15T16:59:55.011Z"}
{"context":{"fileName":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/test/foo.spec.ts","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"config":{"automock":false,"browser":false,"cache":true,"cacheDirectory":"/private/var/folders/cl/yqb0znw56dj17l06y_45rzx40000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"providesModuleNodeModules":[],"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"065014f8040ebe756b2da8ebc80e3c0a","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","roots":["/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"instrument":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","supportsDynamicImport":false,"supportsStaticESM":false},"transformerId":1,"version":"25.5.1"},"message":"computing cache key for /Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/test/foo.spec.ts","sequence":6,"time":"2020-05-15T16:59:55.012Z"}
{"context":{"logLevel":30,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"25.5.1"},"message":"no matching config-set found, creating a new one","sequence":7,"time":"2020-05-15T16:59:55.012Z"}
{"context":{"config":{"automock":false,"browser":false,"cache":true,"cacheDirectory":"/private/var/folders/cl/yqb0znw56dj17l06y_45rzx40000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"providesModuleNodeModules":[],"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"065014f8040ebe756b2da8ebc80e3c0a","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","roots":["/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"backports","package":"ts-jest","transformerId":1,"version":"25.5.1"},"message":"backporting config","sequence":8,"time":"2020-05-15T16:59:55.012Z"}
{"context":{"jestConfig":{"automock":false,"browser":false,"cache":true,"cacheDirectory":"/private/var/folders/cl/yqb0znw56dj17l06y_45rzx40000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{"ts-jest":{}},"haste":{"computeSha1":false,"providesModuleNodeModules":[],"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"065014f8040ebe756b2da8ebc80e3c0a","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","roots":["/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"version":"25.5.1"},"message":"normalized jest config","sequence":9,"time":"2020-05-15T16:59:55.013Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsJestConfig":{"compiler":"typescript","diagnostics":{"ignoreCodes":[6059,18002,18003],"pretty":true,"throws":true},"isolatedModules":false,"packageJson":{"kind":"file"},"transformers":[],"tsConfig":{"kind":"file"}},"version":"25.5.1"},"message":"normalized ts-jest config","sequence":10,"time":"2020-05-15T16:59:55.013Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"version":"25.5.1"},"message":"babel is disabled","sequence":11,"time":"2020-05-15T16:59:55.015Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","requireResult":{"exists":true,"given":"typescript","path":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/typescript/lib/typescript.js"},"version":"25.5.1"},"message":"loaded module typescript","sequence":12,"time":"2020-05-15T16:59:55.169Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"25.5.1"},"message":"patching typescript","sequence":13,"time":"2020-05-15T16:59:55.169Z"}
{"context":{"actualVersion":"3.9.2","expectedVersion":">=3.4 <4","logLevel":20,"namespace":"versions","package":"ts-jest","version":"25.5.1"},"message":"checking version of typescript: OK","sequence":14,"time":"2020-05-15T16:59:55.169Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsConfigFileName":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/tsconfig.json","version":"25.5.1"},"message":"readTsConfig(): reading /Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/tsconfig.json","sequence":15,"time":"2020-05-15T16:59:55.169Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsconfig":{"compileOnSave":false,"configFileSpecs":{"filesSpecs":[],"wildcardDirectories":{}},"errors":[],"fileNames":[],"options":{"configFilePath":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/tsconfig.json","declaration":false,"inlineSourceMap":false,"inlineSources":true,"module":1,"noEmit":false,"removeComments":false,"sourceMap":true,"target":1},"projectReferences":[{"originalPath":"./src","path":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/src"},{"originalPath":"./test","path":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/test"}],"raw":{"compileOnSave":false,"compilerOptions":{},"files":[],"references":[{"path":"./src"},{"path":"./test"}]},"typeAcquisition":{"enable":false,"exclude":[],"include":[]},"wildcardDirectories":{}},"version":"25.5.1"},"message":"normalized typescript config","sequence":16,"time":"2020-05-15T16:59:55.175Z"}
{"context":{"fileName":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/src/foo.ts","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"config":{"automock":false,"browser":false,"cache":true,"cacheDirectory":"/private/var/folders/cl/yqb0znw56dj17l06y_45rzx40000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"providesModuleNodeModules":[],"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"065014f8040ebe756b2da8ebc80e3c0a","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","roots":["/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"instrument":false,"rootDir":"/Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work","supportsDynamicImport":false,"supportsStaticESM":false},"transformerId":1,"version":"25.5.1"},"message":"computing cache key for /Users/mattiask/code/open-source/temp/jest-error-type-issue/does-not-work/src/foo.ts","sequence":17,"time":"2020-05-15T16:59:55.178Z"}

envinfo

System:
    OS: macOS Catalina v10.15.4

Npm packages:
    jest: v25.5.4
    ts-jest: v25.5.1
    typescript: v3.9.2

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:35

github_iconTop GitHub Comments

13reactions
tommysullivancommented, Feb 18, 2021

@rhyek same.

if i run yarn workspace subproject1 build, and then in the subproject1 folderā€™s package.json i define build to do a tsc -b tsconfig.subproject1.json, where this tsconfig contains references to subproject2, which has its own package.json and tsconfig etc, then, everything compiles, all the dist folders are there, and my jest tests, located in subproject1, work just fine.

if i do not build myself, then it complains Cannot find module 'subproject1' or its corresponding type declarations., regardless of whether or not I attempt to use moduleNameMapper in subproject1ā€™s jest config.

So, bottom line is that it seems that ts-jest is not doing what tsc -b would do, so when in watch mode, i really suffer.

9reactions
alexbrunocommented, Aug 1, 2022

I know this is an old issue, but if anyone else is running into similar problemsā€¦ I just made this on Jest config and it worked for me:

  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest compilation issues with Typescript 3.0 project references
So, using project references; I can build src, test, ... The second and following runs will all then fail with compilation issues.
Read more >
Breaking down TypeScript project references | by Jake Ginnivan
The setup I have landed on is that I want my Type Checking to use project references but my test and builds simply...
Read more >
Documentation - Project References - TypeScript
Project references are a new feature in TypeScript 3.0 that allow you to structure your TypeScript programs into smaller pieces.
Read more >
Need help with project references (separating src and unit tests)
Your tests should reference src , since test code imports src , not the other eay around. IIRC TypeScript complains if your references...
Read more >
TypeScript auto-imports should respect project references
Auto-imports currently do not seem to respect project references. When several classes with the same name exist under different tsconfig.json files, auto-Ā ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found