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 4 years ago
- Reactions: 2
- Comments: 39
Commits related to this issue
- Add specific tsconfig for jest to fix TypeScript support issue. As described in https://github.com/kulshekhar/ts-jest/issues/4081#issuecomment-1515758013. — committed to Aleksbgbg/streamfox by Aleksbgbg 3 years ago
- Get Jest working https://github.com/kulshekhar/ts-jest/issues/4081#issuecomment-1515758013 https://koenvg.medium.com/setting-up-jest-with-sveltekit-4f0a0e379668#3f2e https://github.com/pmucha/sveltek... — committed to MaddyGuthridge/Minifolio by MaddyGuthridge 3 years ago
- patch(fix): octo | reverting the verbatimModuleSyntax option. - Because of error "ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled." when running tests. - The Git... — committed to quadnix/octo by rash805115 2 years ago
- Re-enable `verbatimModuleSyntax` in `tsconfig.json`, fixing associated `ts-node`/`jest` config parsing error about using ECM syntax in CommonJS. Based on https://github.com/kulshekhar/ts-jest/issues/4... — committed to br-tyler-milner/spectral-tests-example by br-tyler-milner 2 years ago
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.jsonWhen people keep coming back, it should be a good signal that something is wrong. Please reopen the issue and look for a solution.
Sorry 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. 😅
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 bugThat 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.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?
I’m having the exact same issue that @lvlte is describing.
@ahnpnl I have a question. Why does the error imply that my test file is in CommonJS when in fact my configs in
package.json,tsconfig.json, andjest.config.jsall scream ESModule?My
package.json:My
tsconfig.json:My
jest.config.js:Yet the error reads:
How come running
jeststill treats mysample.test.tsas CommonJS?I guess I found a solution.
Install
ts-node.Create a separate TypeScript configuration file for Jest. Let’s name it
tsconfig.jest.json.(Alternatively, you can set
moduleResolutiontobundler. Strategies other thanbundlerornode10won’t work.)Assign the path to the file to the
TS_NODE_PROJECTenvironment variable just before running Jest.Bash:
CMD:
Alternatively, for a cross-platform implementation, add
to
package.json, installcross-envand runnpm test.The above solution will enable ESM syntax in
jest.config.tsonly. You should also installts-jestand follow this guidance to enable ESM syntax in test modules. Here is myjest.config.ts:Who has problems with TypeScript and Jest, here a possible solution. In your
tsconfig.jsonconfigurets-node:In your
jest.config.tsconfigurets-jest:@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.
All you need to reproduce the issue is in my first comment. The content of my .ts files is not relevant.
Just take an esm package with the config I mentioned and an index.ts file that exports something, then try to import it in a test file and see the results.
Note I use
jest.config.js(not .ts).Why is this issue closed as “completed” ?
Suppose you have an ESM module (package.json contains
"type": "module") with the following in tsconfig (no path options) :Your jest config (defined in jest.config.js, ie. ts-node not involved), as recommended by the doc (except for the CJS syntax obviously, and without the unnecessary / redundant settings that are already [pre]set) :
You run your tests and get the error : “ESM syntax is not allowed in a CommonJS module when ‘verbatimModuleSyntax’ is enabled” 😮.
Adding
isolatedModules: true- thanks @dustin-ruetz - makes the error go away :😮
Would it not be a bug ?
We shouldn’t have to set
isolatedModules: trueorverbatimModuleSyntax: falseto get rid of an error that says our ESM module is a CJS module.This is not a feature request.
This is still an issue in Aug 2024 FYI. Specifically
ts-jestdoes not properly parsejest.config.tsfiles when"verbatimModuleSyntax": true,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?
https://github.com/kulshekhar/ts-jest/pull/4082