gpt4 book ai didi

javascript - 开 Jest - ReferenceError : define is not defined

转载 作者:行者123 更新时间:2023-11-30 11:12:40 24 4
gpt4 key购买 nike

我正在尝试使用 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 并且这是一个更详细的错误: errorMsg

如有任何帮助,我将不胜感激!在过去的几天里,我一直在为此烦恼:-(

最佳答案

编辑:刚刚意识到根据文档 toHaveBeenCalledTimes 实际上也应该有效。无论如何,您可以试试 mock.calls.length


据我所知,jest 函数模拟的断言不是这样工作的

expect(handleChange).toHaveBeenCalledTimes(1);

你应该试试这个

expect(handleChange.mock.calls.length).toBe(1);

docs 中所述

关于javascript - 开 Jest - ReferenceError : define is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com