在 testRunner 设置为 jest-circus 的情况下访问 jasmine 导致:ReferenceError: jasmine is not defined

ysf*_*ran 2 javascript node.js jasmine jestjs jest-circus

默认情况下jest,您可以简单地jasmine全局访问。但是一旦你切换testRunnerjest-circus,jasmine是未定义的。以下是一个最小的、可重现的示例:

babel.config.js

module.exports = {
  presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
Run Code Online (Sandbox Code Playgroud)

茉莉花规范.js

it("check jasmine", () => {
  console.log(jasmine);
});
Run Code Online (Sandbox Code Playgroud)

开玩笑的配置文件

module.exports = {
  rootDir: ".",
  testRunner: "jest-circus/runner",
};
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "test-jest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "babel-jest": "^26.6.3",
    "jest": "^26.6.3",
    "jest-circus": "^26.6.3"
  }
}
Run Code Online (Sandbox Code Playgroud)

运行此测试将导致以下输出:

$ npm test

> test-jest@1.0.0 test /Users/yusufaran/Projects/test/test-jest
> jest

 FAIL  ./jasmine.spec.js
  ? check jasmine (1 ms)

  ? check jasmine

    ReferenceError: jasmine is not defined

      1 | it("check jasmine", () => {
    > 2 |   console.log(jasmine);
        |               ^
      3 | });
      4 | 

      at Object.<anonymous> (jasmine.spec.js:2:15)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.01 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
Run Code Online (Sandbox Code Playgroud)

如果您删除/注释该testRunnerjest.config.js(因此它回退到默认运行器),它会按预期工作。

如何jasmine使用testRunnerset to访问全局对象jest-circus/runner?如果我不能,为什么?

Ser*_*kyi 5

使用 jest-circus 时无法访问 jasmine。这是设计使然。jest-circus 是一个从头开始构建的新测试运行器。它模仿茉莉定义测试功能(即describeit除了一切expect断言和间谍)。

如果你依赖 jasmine,那么npm install -D jest-jasmine2并在你的 jest 配置中使用它:

{
  testRunner: 'jest-jasmine2'
}
Run Code Online (Sandbox Code Playgroud)


Api*_*Api 5

只需添加testRunner: 'jasmine2'到 jest.config.js 对我来说就成功了