ala*_*yor 9 javascript mocking jasmine jestjs
我正在尝试使用创建以下单元测试jest.
jest.dontMock("pointsAwardingActions.js");
describe("points awarding actions", () => {
describe("award points", () => {
it("should dispatch begin ajax action", () => {
var pointsAwardingActions = require("pointsAwardingActions.js");
const mockedDispatch = jest.fn();
});
});
});
Run Code Online (Sandbox Code Playgroud)
但运行后我收到以下错误npm test.
TypeError: jest.fn is not a function
这是我的一部分package.json.
{
"scripts": {
"test": "jest"
},
"author": "alayor",
"license": "ISC",
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"testFileExtensions": ["spec.js"],
"moduleFileExtensions": ["js"],
"collectCoverage": "true"
},
"dependencies": {
"babel-cli": "6.8.0",
"babel-core": "6.8.0",
"babel-jest": "^6.0.1",
"babel-loader": "6.2.4",
"babel-plugin-react-display-name": "2.0.0",
"babel-polyfill": "6.8.0",
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"babel-preset-react-hmre": "1.1.1",
"expect": "1.19.0",
"express": "4.13.4",
"jest": "^0.1.40",
"jest-cli": "^0.8.1",
...
}
}
Run Code Online (Sandbox Code Playgroud)
可能是我得到那个错误的原因.
Ric*_* II 17
该玩笑的对象会自动在每个测试文件内的范围,所以没有必要明确地导入.如果您确实想直接导入jest对象,则需要通过以下方式导入jest-mock模块,而不是jest-cli模块:
// Not necessary inside a Jest test file
import jest from 'jest-mock';
const mock = jest.fn();
Run Code Online (Sandbox Code Playgroud)
有点奇怪,文档没有提到jestnot require('jest');but require('jest-mock'),以下代码应该适用于 v22:
const jest = require('jest-mock');
const spy = jest.fn();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6186 次 |
| 最近记录: |