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
- fix test failed with node 20. https://github.com/jestjs/jest/issues/15312 — committed to wtto00/android-tools by wtto00 2 years ago
- p up; update tsconfig to fix testing Relevant tsconfig issue: https://github.com/jestjs/jest/issues/15312 — committed to jakes-space/firetender by jakes-space 2 years ago
- https://github.com/jestjs/jest/issues/15312 — committed to emmercm/igir by emmercm 2 years ago
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.3As 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:Keep
ts-jestfor the TypeScript stuff and usebabelfor 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: moduleor if the file is a.mjs, TypeScript will output ES6 which jest fails to parse.I’m using a project with ESM settings:
package.jsonhas"type": "module""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.tscontained imports of third-party modules (egnode:pathordotenv).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 andts-jestspewed 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.jsontype: module)jest.config.ts(requirests-node)STEPS | FIX:
tsconfig.test.jsonjest.config.tstsconfig.test.jsonwhen runningjest(ts-nodeproject)Linux&MacWindowsOS AGNOSTICjestviacross-envHope 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-jestfor your TypeScript - with type checking - andbabelfor the ES6 JS. You still get type checking with this configuration.Same issue for me, I have a
jest.config.jswhich transforms somenode_modulesthat are ES6 viatransformIgnorePatterns. 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.jsontargets"module": "NodeNext"andpackage.jsonhas"type": "module", so TS will emit ESM and I’m wondering whether changing the extension to request CJS would make a difference.