reactjs - 在另一个测试中设置 Jest 模拟的指针
问题描述
我正在编写一个测试工厂,并希望公开一个“setupMocks”式函数指针来模拟一些常见的 API 响应。但是,它不起作用。这是我所拥有的:
测试-utils.ts
export const setupMocks = () => {
jest.mock(...);
jest.mock(...);
...
jest.mock(...);
}
组件.test.tsx
// Test passes if these are uncommented, and are identical to the lines in setupMocks
// jest.mock(...);
// jest.mock(...);
// ...
// jest.mock(...);
describe("test component", () => {
const props = { ... };
beforeEach(() => {
setupMocks();
});
afterEach(() => {
jest.resetAllMocks(); // already tried commenting out this line; it's not this
});
it("something", () => {
const component = render(<Component {...props} />);
expect(component.someStateDependentOnMockedDataFromJestCall).toBe(true); // fails
});
});
解决方案
推荐阅读
- javascript - 从加载了客户端 JavaScript 的页面获取呈现的 HTML?
- c++ - 为什么我无法在 Qt Designer 中删除 tabWidget?
- java - 如何通过控制台将非英文文件名传递给 java main args?
- tomcat - jenkins tomcat ssh权限被拒绝
- python - 基于 pd.Series 字符串添加辅助 x 轴
- c# - 如何在VS C#中按下回车键时启用自动完成
- reactjs - 除了使用 cookie 通过 express 和 react 将请求-响应从后端传递到前端之外,还有其他解决方案吗?
- json - jq:添加嵌套数字?
- angular - Angular 8 NgFor 仅支持绑定到 Iterables,例如 Arrays error 和 Access Control Allow origin
- assembly - 如何通过获取指令类型细分来表征工作负载?