Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"await is only valid in async function" even with top-level await support #9667

Closed
dandv opened this issue Mar 14, 2020 · 2 comments
Closed

Comments

@dandv
Copy link
Contributor

dandv commented Mar 14, 2020

I've added '@babel/plugin-syntax-top-level-await' and am passing --harmony-top-level-await to Node v13.10, but Jest still returns a syntax error:

SyntaxError: await is only valid in async function

 at Runtime._execModule (../node_modules/jest-runtime/build/index.js:988:58)

Are there plans to support top-level await in tests, perhaps part of #9430?

@SimenB
Copy link
Member

SimenB commented Mar 17, 2020

This is a syntax error from node.

const { Script } = require('vm');

new Script('await Promise.resolve()');

Running that will throw the same error you're seeing from Jest.

It does work with ESM though, so it should work when #9430 lands, yes.

const { SourceTextModule } = require('vm');

const m = new SourceTextModule('await Promise.resolve()');

m.link(() => {})
  .then(() => m.evaluate())
  .then(
    () => console.log('success!'),
    error => console.error('argh', error),
  );

Prints "success!".


I'm not sure if vm.Script is supposed to support TLA? Maybe worth an issue in the node repo, but I suppose it's on purpose since I think Scripts has to evaluated synchronously. Not 100% certain though.

EDIT: TLA will only be support in modules

@SimenB SimenB closed this as completed Mar 17, 2020
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants