Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create custom TestEnvironment in Jest

I am trying to create a custom test environment with Jest as described in their official docs
Unfortunately I get the following error:

Determining test suites to run...
FAIL acceptancetests/mongo.test.js
● Test suite failed to run

TypeError: TestEnvironment is not a constructor

at ../node_modules/jest-runner/build/run_test.js:88:25

My test is completely empty and my CustomTestEnvironment just calls the super classes. I am on the latest Jest version (24.3.1)

I think it is very weird, that the error is thrown within the Jest library.

This is my test-environment.js:

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

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

  async setup() {
    await super.setup();
  }

  async teardown() {
    await super.teardown();
  }

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

Any help is appreciated!

like image 394
JDurstberger Avatar asked Jul 16 '18 09:07

JDurstberger


1 Answers

Ok, this is a silly problem and I found the solution.

I had to export the CustomEnvironment

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

class CustomEnvironment extends NodeEnvironment {
  ...
}

module.exports = CustomEnvironment

And also add it in the jest.config.js

const { defaults } = require("jest-config");
module.exports = {
  testEnvironment: './../CustomEnvironment.js',
};

I don't know why the guide doesn't include that line :(

like image 196
JDurstberger Avatar answered Nov 04 '22 11:11

JDurstberger