jest: [Bug]: Failed to parse the TypeScript config file with typescript@5.6.2

Version

29.7.0

Steps to reproduce

After I upgraded typeScript version from 5.5.4 to 5.6.2, my test pipeline execution failed.

My execution failed pipeline is https://github.com/wtto00/android-tools/actions/runs/10907495575/job/30271298092

Expected behavior

jest --detectOpenHandles --verbose executed successfully.

Actual behavior

jest --detectOpenHandles --verbose execution failed, the failure information is as follows:

> @wtto00/android-tools@1.0.5 test E:\projects\github\wtto00\android-tools
> jest --detectOpenHandles --verbose

Error: Jest: Failed to parse the TypeScript config file E:\projects\github\wtto00\android-tools\jest.config.ts
  SyntaxError: Unexpected token 'export'
    at readConfigFileAndSetRootDir (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\jest-config@29.7.0_@types+node@22.5.5_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:116:13)
    at async readInitialOptions (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\jest-config@29.7.0_@types+node@22.5.5_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\jest-config\build\index.js:403:13)
    at async readConfig (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\jest-config@29.7.0_@types+node@22.5.5_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\jest-config\build\index.js:147:48)
    at async readConfigs (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\jest-config@29.7.0_@types+node@22.5.5_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\jest-config\build\index.js:424:26)
    at async runCLI (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\@jest+core@29.7.0_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\@jest\core\build\cli\index.js:151:59)
    at async Object.run (E:\projects\github\wtto00\android-tools\node_modules\.pnpm\jest-cli@29.7.0_@types+node@22.5.5_ts-node@10.9.2_@types+node@22.5.5_typescript@5.6.2_\node_modules\jest-cli\build\run.js:130:37)

Additional context

Related dependency versions:

jest@29.7.0
ts-jest@29.2.5
ts-node@10.9.2
tslib@2.7.0
typescript@5.6.2

Environment

System:
    OS: Windows 11 10.0.22631
    CPU: (6) x64 Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  Binaries:
    Node: 20.13.1 - ~\AppData\Local\fnm_multishells\11248_1726675469235\node.EXE
    npm: 10.8.1 - ~\AppData\Local\fnm_multishells\11248_1726675469235\npm.CMD
    pnpm: 9.1.1 - ~\AppData\Local\fnm_multishells\11248_1726675469235\pnpm.CMD
  npmPackages:
    jest: ^29.7.0 => 29.7.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 20
  • Comments: 34

Commits related to this issue

Most upvoted comments

I am facing this issue aftrer upgrading to node 24. Downgrading to node 22 seems to have solved the issue

Still an issue with typescript 5.7.3

As I understand the announcement typescript >5.6 now respects the package.json from node_modules regarding ESM. So even if you include a package for transformation by adjusting transformIgnorePatterns, the package will run through the typescript compiler, but typescript will not remove those ESM import/exports. Therefore jest fails, because it does not understand them.

Time to go back to the good ol’ babel:

transform: {
    '\\.tsx?$': [
        'ts-jest',
        {
            tsconfig: {
                outDir: './.ts-jest'
            }
        }
    ],
    '\\.jsx?$': ['babel-jest', {plugins: ['@babel/plugin-transform-modules-commonjs']}]
},
transformIgnorePatterns: [
    '/node_modules/(?!(yourstuff))'
],
npm install --save-dev babel-jest @babel/core @babel/plugin-transform-modules-commonjs

Keep ts-jest for the TypeScript stuff and use babel for the rest.

Ok, I do understand it - here is the precise problem: https://github.com/microsoft/TypeScript/issues/59991

Alas, the answer is “working as intended”.

Before 5.6, TypeScript would output whatever it was configured to output. Since 5.6, if the module is a type: module or if the file is a .mjs, TypeScript will output ES6 which jest fails to parse.

I’m using a project with ESM settings:

  • package.json has "type": "module"
  • tsconfig has "module": "nodenext".

After upgrading to node v22.11.0, the error mentioned in this issue disappeared in almost all modules of the monorepository. But it was still issued if jest.config.ts contained imports of third-party modules (eg node:path or dotenv).

After I commented out "moduleResolution": "nodenext" in tsconfig, everything started working as expected.

@luisabtrace I’ve simply moved to Vitest: easier and way better monorepo and ESM support.

@manchuck why would the rules be different for the tests? Are you tests named *.js? I just changed a type and ts-jest spewed an error.

@patricklafrance he is in a very specific particular case of this issue, because unlike us, he is not transpiling any external node_modules - he is transpiling only his own code. But it is still interesting to know what is different in Node 22.

@luisabtrace I have used isolatedModules to be able to upgrade Typescript. See https://github.com/kulshekhar/ts-jest/issues/4561#issuecomment-2676155216

I’ve been hit by this issue since September. Typescript is now at version 5.8 and the problem remains. How are people solving this? @carlocorradini’s solution did not work for me, I’m afraid.

Workaround for working with:

  • TypeScript (>= 5.6)
  • tsconfig.json
    {
      "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "NodeNext",
        "noEmitOnError": true
      }
    }
    
  • ESM (type: module)
  • jest.config.ts (requires ts-node)
    import type { Config } from "jest";
    import { createDefaultEsmPreset } from "ts-jest";
    
    export default {
      ...createDefaultEsmPreset()
    } as const satisfies Config;
    

STEPS | FIX:

  1. Create a dedicated tsconfig.test.json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "noEmitOnError": false
  }
}
  1. Update jest.config.ts
import type { Config } from "jest";
import { createDefaultEsmPreset } from "ts-jest";

export default {
  ...createDefaultEsmPreset({ tsconfig: "<rootDir>/tsconfig.test.json" })
} as const satisfies Config;
  1. Use tsconfig.test.json when running jest (ts-node project)
    1. Linux & Mac
      TS_NODE_PROJECT="tsconfig.test.json" npx jest
      
    2. Windows
      $env:TS_NODE_PROJECT="tsconfig.test.json"
      npx jest
      
    3. OS AGNOSTIC
      1. Install cross-env
        npm install --save-dev cross-env
        
      2. Run jest via cross-env
        npx cross-env TS_NODE_PROJECT="tsconfig.test.json" npx jest
        

Hope it helps! 🙌🥳

@mmomtchev sorry I meant you loose type checking for your test scripts. Yes you still get TS checking for code. Apologies for the mis type

@patricklafrance I didn’t do anything it was working just fine with 22. Here is the run that passed in our action: https://github.com/Vonage/vonage-node-sdk/actions/runs/10997790768/job/30534604593

@manchuck how do you get it to work with Node 22? Doesn’t work for me with Node 22 until I force TS to version 5.5.4.

No, you still use ts-jest for your TypeScript - with type checking - and babel for the ES6 JS. You still get type checking with this configuration.

Same issue for me, I have a jest.config.js which transforms some node_modules that are ES6 via transformIgnorePatterns. This does not work anymore since TypeScript 5.6.

I am linking the changelog which mentions a behavior change for ES6 modules, but I really don’t understand how this break jest (or ts-jest).

https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/

Could you try renaming the file to jest.config.cts? tsconfig.json targets "module": "NodeNext" and package.json has "type": "module", so TS will emit ESM and I’m wondering whether changing the extension to request CJS would make a difference.