在testRunner设置为jest-circus的情况下访问茉莉花会导致:ReferenceError:茉莉花未定义 [英] Accessing jasmine with testRunner set to jest-circus results in: ReferenceError: jasmine is not defined
问题描述
默认情况下,jest
允许您简单地全局访问jasmine
.但是,只要将testRunner
切换为jest-circus
,jasmine
就不会被定义.以下是一个最小的,可重复的示例:
On default jest
allows you to simply access jasmine
globally. But as soon as you switch the testRunner
to jest-circus
, jasmine
is undefined. Following is a minimal, reproducible example:
babel.config.js
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
jasmine.spec.js
it("check jasmine", () => {
console.log(jasmine);
});
jest.config.js
module.exports = {
rootDir: ".",
testRunner: "jest-circus/runner",
};
package.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"
}
}
运行此测试将导致以下输出:
Running this test will cause following output:
$ 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.
如果您删除/注释jest.config.js
中的testRunner
行(因此回退到默认运行器),它将按预期工作.
If you remove/comment the testRunner
line in jest.config.js
(so it fallbacks to the default runner) it works as expected.
问题
如何在testRunner
设置为jest-circus/runner
的情况下访问全局jasmine
对象?如果不能,为什么?
How can I access global jasmine
object with testRunner
set to jest-circus/runner
? If I can't, why?
推荐答案
使用笑话马戏团时无法访问茉莉花.这是设计使然. jest-circus是从头开始构建的新的测试运行程序.它模仿了定义测试的茉莉花功能(即describe
,it
,除了expect
断言和间谍的所有内容).
You can’t access jasmine when you use jest-circus. This is by design. jest-circus is a new test runner that was built from scratch. It mimics jasmine functionality for defining tests (i.e., describe
, it
, everything except expect
assertions and spies).
如果您依赖茉莉花,请npm install -D jest-jasmine2
并在您的jest配置中使用它:
If you depend on jasmine, then npm install -D jest-jasmine2
and use it in your jest config:
{
testRunner: 'jest-jasmine2'
}
这篇关于在testRunner设置为jest-circus的情况下访问茉莉花会导致:ReferenceError:茉莉花未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!