我刚刚将react-scripts更新到4.0,其中包括Jest@26和一些附带的测试包,下面是package.json的不同之处:

升级后,一些Jest mock已经开始失败了。似乎模拟的返回值是未定义的?我是不是遗漏了什么?以下是其中一个失败的模拟
import useFetch from "use-http";
jest.mock("use-http", () => ({
__esModule: true,
default: jest.fn()
}));
describe("the user context", () => {
beforeAll(() => {
useFetch.mockReturnValue({
get: async () => Promise.resolve({ foo: 666 }),
response: { ok: true }
});
});尝试使用“get”方法的测试失败,并显示以下错误:
TypeError: Cannot destructure property 'get' of '(0 , _useHttp.default)(...)' as it is undefined.
还有一个不是默认的,不会为一次性mock导入包:
jest.mock("_hooks", () => ({
useBaseUrls: jest.fn().mockReturnValue({
app: "bar"
})
}));访问“app”属性的测试抛出TypeError: Cannot read property 'app' of undefined
发布于 2021-01-04 14:48:17
Jest 26将resetMocks的默认行为更改为true,这会在每次测试之前重置模拟状态。
您可以通过在package.json中禁用resetMocks来恢复到以前的行为
"jest": {
"resetMocks": false
}再次更改默认设置的讨论目前在他们的Github上是一个开放的问题:https://github.com/facebook/create-react-app/issues/9935
https://stackoverflow.com/questions/65363796
复制相似问题