Skip to content

Transpile tests without type checking in jest - #472

Merged
atm-snag2 merged 1 commit into
mainfrom
jest-resolve-exports-only-packages
Nov 12, 2026
Merged

atm-snag2 merged 1 commit into
mainfrom
jest-resolve-exports-only-packages

Conversation

@atm-snag2

@atm-snag2 atm-snag2 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Stop type checking in jest and let ts-jest only transpile test files (isolatedModules: true, module: "commonjs"). Type checking stays with tsc and ts-loader, which use the same settings as production.

Why

The chalk 6 bump (#467) fails in jest with TS2307: Cannot find module 'chalk' or its corresponding type declarations.

  • chalk 6 dropped the top-level main / types in its package.json and exposes its types only through exports.
  • ts-jest type checks test files in CommonJS mode. When moduleResolution is not set, it uses node10, which does not read exports.
  • tsc (module: Node16) reads exports, so lint and build pass and runtime is not affected. Only the type check inside jest fails.

The type check inside jest duplicates tsc with different settings, so tsc and jest can disagree like this again on future bumps. It also prints a TS151002 warning for every transpiled file and makes jest slow.

How

  • Test files are already type checked in CI by the build job: tsc --build covers src except src/client, and webpack's ts-loader covers src/client. Type errors in test files are still caught there. Only running pnpm test alone no longer reports them.
  • module: "commonjs" is required. With isolatedModules: true and module: Node16, await import("chalk") is kept as a real dynamic import(), which fails under jest without --experimental-vm-modules, and jest.mock for ESM-only packages (chalk, chokidar) stops working.
  • The object passed to ts-jest's tsconfig option is merged on top of compilerOptions in tsconfig.json, so tsc is not affected.

Verification results are posted as a comment.

Refs

@atm-snag2 atm-snag2 self-assigned this Nov 11, 2026
ts-jest type checked test files with settings that differ from tsc
(`moduleResolution` falls back to `node10`, which ignores `exports`), so
packages that publish types only via `exports` (e.g. chalk 6) failed with
TS2307 in jest although tsc passes. Test files are already type checked by
`tsc` and ts-loader in the build, so let ts-jest only transpile them.

`module: commonjs` keeps `import()` compiled to `require()`, so ESM-only
packages can still be mocked with `jest.mock`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atm-snag2
atm-snag2 force-pushed the jest-resolve-exports-only-packages branch from b5c97e1 to 7ce83a5 Compare November 11, 2026 02:21
@atm-snag2 atm-snag2 changed the title Resolve packages that expose types only via exports in jest Transpile tests without type checking in jest Nov 11, 2026
@atm-snag2

Copy link
Copy Markdown
Contributor Author

Verification (7ce83a5)

Node 24.22.1 / pnpm 10.34.7, pnpm install --frozen-lockfile, pnpm exec jest --no-cache.

The fix makes jest pass with chalk 6

On the head of #467 (e14a187, chalk 6.0.1), switching only jest.config.js:

jest.config.js Result
Without this PR Test Suites: 3 failed, 24 passed, 27 total / Tests: 224 passed, 224 total (TS2307 for chalk)
With this PR Test Suites: 27 passed, 27 total / Tests: 251 passed, 251 total
Without this PR again Test Suites: 3 failed, 24 passed, 27 total / Tests: 224 passed, 224 total (TS2307 for chalk)

No regression on main

On this branch, passed twice in a row with no ts-jest warnings:

Test Suites: 27 passed, 27 total
Tests:       251 passed, 251 total
jest.config.js on main Tests Time TS151002 lines
Before this PR 251 passed 15.3 s, 13.2 s 222, 211
This PR 251 passed 3.6 s 0

The TS151002 count varies between runs because of parallel workers; the point is that it is printed without this PR and not printed with it.

module: "commonjs" is required

With isolatedModules: true only (keeping module: Node16), 12 tests in publish.test.ts, error-handler.test.ts and app.test.ts fail with:

TypeError: A dynamic import callback was invoked without --experimental-vm-modules

Type errors in test files are still caught by the build

Added const x: number = "x"; to a test file and ran the build that the build job runs:

Test file Command Result
src/lib/error-handler.test.ts pnpm run build:server (tsc --build) fails with TS2322
src/client/lib/embed-init-scripts.test.ts pnpm run build:client (webpack, ts-loader) fails with TS2322

Not verified yet

After this PR is merged, #467 needs to be rebased by Dependabot and its CI checked.

@atm-snag2
atm-snag2 marked this pull request as ready for review November 11, 2026 03:35
@atm-snag2
atm-snag2 requested review from a team and tsuzuki-takaaki and removed request for a team November 11, 2026 03:37

@tsuzuki-takaaki tsuzuki-takaaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@atm-snag2
atm-snag2 merged commit b210cb2 into main Nov 12, 2026
11 checks passed
@atm-snag2
atm-snag2 deleted the jest-resolve-exports-only-packages branch November 12, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants