我正在为一个带有玩笑的小型反应应用程序开发一个测试用例。
我已经在描述测试块的范围内声明了一个模拟函数。
当我在调试模式下运行测试时,我已在接收模拟函数的 react 组件内的断点处停止,该模拟已声明且实现未定义。
有人知道这种行为的原因是什么吗?
非常感谢!!!
describe("Test App component", () => {
describe("Test Component rendering", () => {
const mockUtils = {
debounceFunc: jest.fn((func) => {
return func;
}),
isSomeStringEmpty: jest.fn(),
confirmAction: jest.fn(),
};
test(`when render App component, then wrapper element should be rendered`, () => {
render(<App utils={mockUtils} apiService={mockApiService} />);
let appDiv = screen.getByTestId("app-test");
expect(appDiv).toBeInTheDocument();
});
});
答案 0 :(得分:1)
我找到了这种行为的答案。
原因是我在这个项目中使用了 creat-react-app,默认情况下在 react-scripts node_modules/react-scripts/scripts/utils/createJestConfig.js
.
jest 配置设置为 "resetMocks": true
这意味着在每次测试 jest 之前重置所有模拟。
默认情况下 JEST 没有配置 "resetMocks": false
的 creat-react-app。
要更改此配置,您只需在 package.json 的根级别添加此条目 "jest": { "resetMocks": false }
。package.json example