【发布时间】:2018-10-26 22:45:28
【问题描述】:
我正在尝试使用 jest 开始进行一些单元测试,但我遇到了很多人似乎遇到的这个错误,但我似乎无法弄清楚。
这是我正在尝试做的事情(我意识到 requirejs 不受支持):
jest.mock("../widgets/", () => {
return <button>button</button>;
});
describe('TextInput', () => {
describe('when user inputs a value', () => {
it('calls correct function to handle change', () => {
const handleChange = jest.fn();
const value = "test"
const wrapper = shallow(<TextInput handleChange={handleChange} />);
const textInputElement = wrapper.find("#textfield");
textInputElement.simulate('change', {target: { value }})
expect(handleChange).toHaveBeenCalledTimes(1);
});
});
});
import React from "react";
import ReactDOM from "react-dom";
import * as TestUtils from "react-dom/test-utils";
import { TextInput } from "../widgets/";
这是我的package.json
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"setupFiles": [
],
"testMatch": [
"**/src/**/?(*.)+(test).js"
],
"testEnvironment": "node",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js)$": "./node_modules/babel-jest",
"^.+\\.css$": "./config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|css|json)$)": "./config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
],
"moduleNameMapper": {
"^react-native$": "react-native-web"
},
"moduleFileExtensions": [
"web.js",
"js",
"json",
"web.jsx",
"jsx",
"node",
"mjs"
]
}
运行 jest 时,我收到错误 ReferenceError: define is not defined,这是一个更详细的错误:
如果有任何帮助,我将不胜感激!在过去的几天里一直在拉我的头发:-(
【问题讨论】:
-
uhhhh 你的第一个代码块非常奇怪......这些导入实际上是 below 你的测试那样吗?
标签: javascript reactjs jestjs