question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
HerbCaudillcommented, Nov 15, 2021

@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 and ts-jest version 27 or higher.

  • These settings need to be in your Jest config:

    "jest": {
      "preset": "ts-jest/presets/default-esm",
      "globals": {
        "ts-jest": {
          "useESM": true
        }
      }
    }
    
  • You need "type": "module" in package.json.

  • In tsconfig.json, you need

      "target": "esnext",
      "module": "esnext"
    

    (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 my test script looks like this:

    "test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js"
    

    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.

1reaction
mayacodacommented, Oct 3, 2021

@mayacoda did you manage to test top level await?

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

This [top-level await] was already supported in --module esnext (and nowā€“module nodenext), but es2022 is the first stable target for this feature.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found