Repository navigation
Description
🚀 Feature Proposal
Since node is going into the direction of (partial) TypeScript support it might be beneficial for Jest to support that flow automatically together with #9430.
The support can be done on a best-effort basis.
Motivation
Less configuration required
Less dependencies in use - no need to run through Babel
Faster execution 1
Quicker way from bootstrap to running suites
Example
Consider the native-esm-typescript E2E test.
Should the config be changed to:
{
"name": "native-esm-typescript",
"version": "1.0.0",
"jest": {
"transform": {}, // effectively disables `babel-jest`
"extensionsToTreatAsEsm": [
".ts"
],
"testEnvironment": "node"
}
}However, even with node 22 and 23 where type stripping (or transformation) can be enabled, this will fail inside jest-runtime where the vm.SourceTextModule expects plain JS and fails inevitably on first : type it finds.
Node v22.13.0 and v23.2.0 have a module helper stripTypeScriptTypes.
It is possible to write a small shim transformer - my PoC:
jest.typestripping.mjs
import { stripTypeScriptTypes } from 'node:module';
export default {
process(sourceText) {
return {code: stripTypeScriptTypes(sourceText)};
}
};jest.typetransforming.mjs
import { stripTypeScriptTypes } from 'node:module';
export default {
process(sourceText) {
return {code: stripTypeScriptTypes(sourceText, {mode: 'transform'})};
}
};And configure jest to run it:
{
"name": "native-esm-typescript",
"version": "1.0.0",
"jest": {
"transform": {
"\\.[jt]sx?$": "<rootDir>/jest.typestripping.mjs", // if no transforms are necessary
"\\.[jt]sx?$": "<rootDir>/jest.typetransforming.mjs", // if using enums or other unsupported syntax
},
"extensionsToTreatAsEsm": [
".ts"
],
"testEnvironment": "node"
}
}Using this with my integration tests works wonders. It replaces ts-jest and speeds the execution time from ~30s to ~7-8s.
The output string can also be equipped with a source map (for transform cases) and source URL to annotate in the source map.
Pitch
The feature can be hooked into jest-runtime and run automatically depending on available APIs. The runtime already does various checks against node:module, e.g. for createRequire.
Running ESM (which requires disabling transforms) with TypeScript could therefore be as effortless as possible for those wanting to join and use as less build steps as possible.
Footnotes
-
depending on which transpiler is used and in which mode ↩
Activity
github-actions commentedon Feb 10, 2025
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
slagiewka commentedon Feb 10, 2025
Is this issue tracker dead?
github-actions commentedon Mar 12, 2025
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
metaskills commentedon Mar 22, 2025
Does this work? I could not get 23.10.0 to work using your work arounds.
github-actions commentedon Apr 22, 2025
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
github-actions commentedon May 22, 2025
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
github-actions commentedon May 22, 2025
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
int19h commentedon May 29, 2025
The workaround works for me on Node 22.16 and Jest 30 beta 5, but with some caveats.
First, if you want proper error reporting, you need to enable source maps when doing the transform, like so:
In addition to this, it doesn't like it when you import a type or namespace from some package under
node_moduleswithout usingimport type, even in transform mode. The error you'll get in this case will also be very non-descriptive, e.g.:(note that this doesn't say which module is trying to import this thing, never mind the exact source line)
github-actions commentedon Jun 29, 2025
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
SimenB commentedon Sep 1, 2026
somehow missed this - let's do it
SimenB commentedon Sep 18, 2026
https://github.com/jestjs/jest/releases/tag/v30.5.2
github-actions commentedon Oct 19, 2026
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.