Skip to content

[Feature]: support node's type stripping #15443

Closed

Description

@slagiewka

🚀 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

  1. depending on which transpiler is used and in which mode ↩

Activity

  1. github-actions commented on Feb 10, 2025

    @github-actions

    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.

  2. slagiewka commented on Feb 10, 2025

    @slagiewka
    Author

    Is this issue tracker dead?

  3. github-actions commented on Mar 12, 2025

    @github-actions

    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.

  4. metaskills commented on Mar 22, 2025

    @metaskills

    Does this work? I could not get 23.10.0 to work using your work arounds.

  5. github-actions commented on Apr 22, 2025

    @github-actions

    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.

  6. github-actions commented on May 22, 2025

    @github-actions

    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.

  7. github-actions commented on May 22, 2025

    @github-actions

    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.

  8. int19h commented on May 29, 2025

    @int19h

    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:

    import { stripTypeScriptTypes } from 'node:module';
    
    export default {
        process(sourceText: string, sourcePath: string) {
            const code = stripTypeScriptTypes(sourceText, {
                mode: 'transform',
                sourceMap: true,
                sourceUrl: sourcePath
            });
            return { code };
        }
    };
    

    In addition to this, it doesn't like it when you import a type or namespace from some package under node_modules without using import type, even in transform mode. The error you'll get in this case will also be very non-descriptive, e.g.:

     FAIL  src/test/breakpoints.test.ts
      ● Test suite failed to run
    
        SyntaxError: The requested module '@vscode/debugprotocol' does not provide an export named 'DebugProtocol'
    
          at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:686:5)
    

    (note that this doesn't say which module is trying to import this thing, never mind the exact source line)

  9. github-actions commented on Jun 29, 2025

    @github-actions

    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.

  10. locked as resolved and limited conversation to collaborators on Jun 29, 2025
  11. SimenB commented on Sep 1, 2026

    @SimenB
    Member

    somehow missed this - let's do it

  12. reopened this on Sep 1, 2026
  13. unlocked this conversation on Sep 1, 2026
  14. SimenB commented on Sep 18, 2026

    @SimenB
    Member
  15. github-actions commented on Oct 19, 2026

    @github-actions

    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.

  16. locked as resolved and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions