ts-jest: [Bug]: no type check when tsconfig's isolatedModules compiler option is true

Version

29.3.3

Steps to reproduce

isolatedModules true

  • npm run test: tests succeed
  • npm run type:check: type check fails

isolatedModules false

  • Disable isolatedModules in tsconfig.json
  • npm run test: tests type check fails
  • npm run type:check: type check fails

Expected behavior

Type check should be executed at test execution time no matter isolatedModules compiler option.

Actual behavior

Enabling tsconfig’s isolatedModules compiler option seems to cause type check from being executed at test execution time.

Disabling tsconfig’s isolatedModules compiler option, brings type check back.

Debug log

Sorry, the log was too large to make sure it doesn’t expose sensitive information. Happy to provide any relevant extra info.

Additional context

No response

Environment

System:
    OS: macOS 14.7.4
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0

About this issue

Most upvoted comments

@ahnpnl My point still stands:

Is there any thought about disabling type checking by default?

We can start doing that by default in the next major version. Currently, you can disable it via diagnostics: false + isolatedModules: false or isolatedModules: true.

However, whenever using isolatedModules: false, it always consumes a lot memories and CPU regardless how many workers. This is the cost of using full TypeScript Program in combination with using Jest. Like I explained above, the implementation with Program isn’t exactly the same as tsc either, so that’s also something to look at.

It is incredibly slow at the moment, and often redundant (since tsc type checks all files by default).

each Jest worker consumes an instance of typescript Program

My example of the repo going from 2 -> 80 seconds is with only one worker (--runInBand).

It’s different between running only tsc vs running Jest + compilation step (done in ts-jest). Currently, the only way to get the most performant is using isolatedModules: true regardless 1 or many workers.

Yes, strictly speaking there are 2 behaviors done by tsc with isolatedModules: true

  • produce transpiled js like other transpilers.
  • Additionally, type checking is still performed at the same time.

From the start, ts-jest introduced a similar isolatedModules option to only replicate the 1st behavior. As codebase has grown, we have decided to simplify the config option by reusing the option from tsconfig while keeping the behavior as is without making breaking change. It was intentional decision.

Type checking is an expensive process to do in a worker thread giving the current architecture of TypeScript itself. Porting to Go will help this in the future.

Giving the state of your PR, it is the right direction for implementation 👍 I think to prepare for the switch in the next major version, I would need to prepare a separate PR to indicate a logging about “default value” for diagnostics

For now, ts-jest will still focus on using TypeScript API. For this request, it’s a simple thing to do with just invoking tsc and read from output dir.

About other transpilers, those would be new codes and not touching to legacy folder. It’s out of concern for this request 😃

Do you mean that ts-jest gives tsconfig.json’s isolatedModules the same meaning as its own deprecated isolatedModules option, diverging from typescript compiler interpretation?

Yes, the only different behavior with tsc is that tsc still offers type checking while ts-jest doesn’t.

In theory it should be possible to add an extra flag for type checking.