问题
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.
If you remove/comment the testRunner
line in jest.config.js
(so it fallbacks to the default runner) it works as expected.
Question
How can I access global jasmine
object with testRunner
set to jest-circus/runner
? If I can't, why?
回答1:
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).
If you depend on jasmine, then npm install -D jest-jasmine2
and use it in your jest config:
{
testRunner: 'jest-jasmine2'
}
来源:https://stackoverflow.com/questions/65889205/accessing-jasmine-with-testrunner-set-to-jest-circus-results-in-referenceerror