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.

Async/Await in custom testEnvironment

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
SimenBcommented, 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!

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async setup of environment with Jest - Stack Overflow
Based on current jest API constraints, you may choose to customize your test's runtime environment through the testEnvironment option.
Read more >
Getting async/await to work in truffle JS tests
I installed truffle using "npm install truffle" yesterday. tests/MyToken.js: ... beforeEach(async function() { instance = await MyToken.new(); }); ...
Read more >
State of Async/Await in JavaScript | by Keerti Kotaru
Async/await in JavaScript is syntactic sugar that simplifies writing code with promises. It is a two part implementation,. async : The async keyword...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
simple, flexible, fun. Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun....
Read more >
ES6 async/await | Developer Guide - Nightwatch.js
The async function enables the API commands to return a promise and makes it possible to use the await operator to retrieve the...
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