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

Async/Await in custom testEnvironment #6054

Closed
KhaledSakr opened this issue Apr 23, 2018 · 5 comments
Closed

Async/Await in custom testEnvironment #6054

KhaledSakr opened this issue Apr 23, 2018 · 5 comments

Comments

@KhaledSakr
Copy link

According to the docs, a custom test environment can be setup this way:

const NodeEnvironment = require('jest-environment-node');

class CustomEnvironment extends NodeEnvironment {
  constructor(config) {
    super(config);
  }

  async setup() {
    await super.setup();
    await someSetupTasks();
    this.global.someGlobalObject = createGlobalObject();
  }

  async teardown() {
    this.global.someGlobalObject = destroyGlobalObject();
    await someTeardownTasks();
    await super.teardown();
  }

  runScript(script) {
    return super.runScript(script);
  }
}

But when I do that, I get the following errors

 FAIL  tests\user.test.js
  ● Test suite failed to run

    D:\everest\tests\env.js:8
      async setup() {
            ^^^^^

    SyntaxError: Unexpected identifier

      at node_modules/jest-runner/build/run_test.js:31:29

Code:
env.js

@SimenB
Copy link
Member

SimenB commented Apr 23, 2018

Your runtime has to support async-await (so use node 8 or newer) or you need to transpile.

PR welcome with a doc update highlighting that fact!

@SimenB SimenB closed this as completed Apr 23, 2018
@KhaledSakr
Copy link
Author

But doesn't jest already transpile the code? We can use async/await out of the box in test files.

@SimenB
Copy link
Member

SimenB commented Apr 23, 2018

It transpiles your tests, not config files.

You can track #5164 for it (feel free to add a comment about jest-environment as well there 🙂)

@KhaledSakr
Copy link
Author

I see, thank you!

@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants