我正在为一个带有玩笑的小反应应用程序开发一个测试用例。
我已经在描述测试块的范围内声明了一个模拟函数。
当我在调试模式下运行测试时,我已经停止在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();
});
});发布于 2021-04-29 15:31:56
我找到了这种行为的答案。
原因是我在这个项目中使用creat应用程序,并且默认情况下在react脚本中使用node_modules/react-scripts/scripts/utils/createJestConfig.js。
jest配置是用"resetMocks": true设置的
这意味着,在每次测试之前,jest都会重置所有的模拟。
默认情况下,没有creat应用程序配置有"resetMocks": false的玩笑。
要更改此配置,只需在package.json .package.json实例的根级添加此条目package.json实例即可。
https://stackoverflow.com/questions/67252912
复制相似问题