Cannot create custom TestEnvironment in Jest

╄→尐↘猪︶ㄣ 提交于 2020-06-25 09:47:29

问题


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!


回答1:


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

I had to export the CustomTestEnvironment

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

class CustomEnvironment extends NodeEnvironment {
  ...
}

module.exports = CustomEnvironment

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



来源:https://stackoverflow.com/questions/51359158/cannot-create-custom-testenvironment-in-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!