ts-jest: Can't use top-level await
🐛 Bug Report
Not sure if I’m doing something wrong, or if TLA isn’t supposed to work due to #1709.
To Reproduce
cd into the cloned repro repo, npm install, npm test.
Expected behavior
The test should pass.
Link to repo (highly encouraged)
https://github.com/dandv/ts-jest-tla
envinfo
System:
OS: Ubuntu Linux 20
Npm packages:
jest: 26.6.0
ts-jest: 26.4.1
typescript: 4.0.3
babel(optional): not used
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 15
- Comments: 25 (4 by maintainers)
@mayacoda @brendonco I struggled with this for a bit and finally got it working — see my fork of @dandv’s repo here: https://github.com/HerbCaudill/ts-jest-tla
The key things to make sure of are:
You need
jestandts-jestversion 27 or higher.These settings need to be in your Jest config:
You need
"type": "module"inpackage.json.In
tsconfig.json, you need(The top-level await error says that targeting es2017 or higher would work, but only esnext has worked for me.)
Jest needs to be run with the
--experimental-vm-modulesflag. For example mytestscript looks like this:The
--no-warningsflag isn’t strictly necessary, but otherwise you get yelled at on every test run that--experimental-vm-modulesis experimental. 🙄Hope this helps others who run into this.
This is very much still a problem.
@dandv Please reopen
@mayacoda: I used the ESM preset too. The manual configuration led to some error.
I’m also getting this issue. Based on the discussion in this thread, this is what my
jest.config.jslooks like, but it still doesn’t work 😭 I have no clue what you did @dandv, but please share your wisdom.envinfo
Actually the default ESM preset works for me with ts-jest 29.1.2, but only after a bunch of fiddling around that might have cleared the Jest cache, AND with the
--experimental-vm-modulespassed to node.jest.config.ts:
tsconfig.json has
module:esnextandtarget:esnext. I’m using Node v18.19.0.I was not able to get it to work with the above described settings, unfortunately.
As for support in TypeScript 4.5, the link @fernandopasik provided also says
Which makes me think it should already work with the configuration above and something else is preventing it. Hopefully, I’m wrong and it does get fixed with the new version of TypeScript.
After a lot of spinning wheels tonight, I found a solution that may work for more recent versions.
In order to allow a top-level await, you’ll need to do something slightly different in your jest config.
In jest.config.cjs I used:
I’m use ts-jest and @types/jest in addition. This combined with my tsconfig allowed a top level await to work.
tsconfig.json:
Edit: forgot to add I changed my test command in package.json to the following:
This issue is not fixed, pls reopen someone @dandv @samestep