Can't use top-level await
See original GitHub issueš 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:18 (3 by maintainers)
Top Results From Across the Web
javascript - How can I use async/await at the top level?
when a module using top-level await is evaluated, it returns a promise to the module loader (like an async function does), which waits...
Read more >Top-level await is available in Node.js modules - Stefan Judis
Starting with Node.js v14.8 , top-level await is available (without the use of the --harmony-top-level-await commandĀ ...
Read more >Top-level `await` is a footgun - gists Ā· GitHub
Note that await can only be used inside an async function. Top-level await is a proposal to allow await at the top level...
Read more >Top-level await - V8 JavaScript engine
Top-level await enables developers to use the await keyword outside of async functions. It acts like a big async function causing otherĀ ...
Read more >Using Top-Level await. Modern Asynchronous JavaScript
With top-level await, ECMAScript modules can await resources, causing other modules who import them to wait before they start evaluating their own code....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
jest
andts-jest
version 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-modules
flag. For example mytest
script looks like this:The
--no-warnings
flag isnāt strictly necessary, but otherwise you get yelled at on every test run that--experimental-vm-modules
is experimental. šHope this helps others who run into this.
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.