ts-jest: [Bug]: verbatimModuleSyntax support
Version
v29.1.0
Steps to reproduce
I have upgraded to TS v5. I had to replace importsNotUsedAsValues: 'error' with verbatimModuleSyntax: true
Expected behavior
No error.
Actual behavior
ESM syntax is not allowed in a CommonJS module when ‘verbatimModuleSyntax’ is enabled.
for import { xxx } from './xxx';
Debug log
https://gist.github.com/simPod/90d2228089b7a8b1b16a827f73aa71f2
Additional context
config
module.exports = {
preset: 'ts-jest',
};
Environment
System:
OS: macOS 13.2.1
CPU: (10) arm64 Apple M1 Max
Binaries:
Node: 19.6.1 - /opt/homebrew/bin/node
Yarn: 4.0.0-rc.42 - /opt/homebrew/bin/yarn
npm: 9.4.0 - /opt/homebrew/bin/npm
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 24
I haven’t been able to figure out the root cause of this issue, but after reviewing the
/examples/ts-onlyfolder I found that the config injest-esm-isolated.config.mjsresolves the error for my projects. More specifically, settingisolatedModulestotruein thets-jesttransfom config object is what fixed the issue for me.Below is a summary of the changes that I made to get ts-jest working with TypeScript 5; hopefully this will help some folks who come across this issue in the future.
@dustin-ruetz Thanks, this answer is useful. But it seems to conflict with
"moduleResolution": "bundler"of tsconfig, and it will be invalid after adding. In order to solve this error, I configured a tsconfig specifically for jest, fortunately it is useful.tsconfig.jest.jsonThat solution doesn’t work for me 🙁
I get the error when parsing the actual
jest.config.tsfile. So the solution has to be somewhere else.yup - same here. the issue always happened when resolving any ESM jest config file (including ts config files), it gave the original error:
ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.seems like the issue has to do with parsing the config file in ESM, but typescript is used (I believe) to do so, but TypeScript keeps complaining about
"verbatimModuleSyntax"even when you specify a custom tsconfig config or config file in the"transform"key… soo the trick is to just disable the"verbatimModuleSyntax"TS config, BUT you can’t use the new transform key, because that is exactly what is breaking, and so you have to use the now deprecated"globals"key instead, and it just worked for me:when i run
jestcli, I do get this annoying warning, but tests are runningthis method very simply disables the
"verbatimModuleSyntax"tsconfig value to false when TS is used as a transformer in my tests files, but the normaltsconfig.jsonstill works for the test files in my editorit seems to me likely that
ts-jestis the culprit here as there is strictly different behaviour happening with theglobalsand thetransformkeys. pretty clearts-jestdoesn’t support “verbatimModuleSyntax” whatsoever in the like 12 permutations of settings i tried with ESM jest config file. i really did try eveyrthing in this thread and had to give up on “everything” being TS/ESM. is it so hard to use TS for everything and use jest? apparently yes it is hahadon’t exactly have time to create a new issue from this, but is very reproducible if you just switch the place of the tsconfig object/path from
transformtoglobalsif anyone else wants to create a new issue for this ongoing bugSorry the doc was a bit outdated, there is warning about using
transformandpresetsee https://kulshekhar.github.io/ts-jest/docs/getting-started/optionsthe doc for ESM needs to be updated accordingly.
the error you saw came from TS type checking similar to
tscwhich we can’t control which options require ESM.you should follow the manual configuration under ESM support doc which gives more control
Just commenting here in case most of you are using
verbatimModuleSyntaxas a linting mechansim so that your eslint auto fixes your imports to type only imports.https://johnnyreilly.com/typescript-5-importsnotusedasvalues-error-eslint-consistent-type-imports
If you are, then it appears that you only need:
and if you’re using vscode, then also this in
.vscode/settings.jsonor your*.code-workspace.json#settingsYou can can avoid all these errors by not using that option, but still get the eslint behaviour.
Yes i just suggested fixing the problem by turning the errors off. 😅
@juanjoDiaz You can use this PR example to try out https://github.com/kulshekhar/ts-jest/pull/4083 note that, this new tsconfig option only works with ESM mode so you need to choose the correct jest config file in one of those
examplesThe solution from @seanblonien does not work for me as well. (It’s February 2024). I decided to bury Jest and go with Vitest. In my Svelte project that works out of the box. The question remains as to how long this system will work.
Hi @ahnpnl ,
Thanks for the quick response.
I tried
as per the PR but I’m getting the same error.
Can you be a bit more specific on where the issue is and what setting in the config would allow me to treat the “jest.config.ts” as an esm module?
Hi @ahnpnl ,
I’m still facing this issue.
I’ve tried the default preset:
and the manual config:
However, none of them worked. I keep getting
jest.config.ts:12:1 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.Any suggestion?