Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocks broken after updating to Jest 26

I just updated react-scripts to 4.0 which includes Jest@26 and some accompanying test packages here's the package.json diff:

Package diff

After upgrading, some Jest mocks have begun to fail. It seems like the mocked return value is just undefined? Am I missing something? Here's one of the failing mocks

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 }
        });
    });

Tests that try to utilize the 'get' method fail with:

TypeError: Cannot destructure property 'get' of '(0 , _useHttp.default)(...)' as it is undefined.

And another that isn't default, doesn't import the package for one-time mocks:

jest.mock("_hooks", () => ({
    useBaseUrls: jest.fn().mockReturnValue({
        app: "bar"
    })
}));

Tests that access the 'app' property throw TypeError: Cannot read property 'app' of undefined

like image 903
eej Avatar asked Dec 18 '20 20:12

eej


1 Answers

Jest 26 changed the default behavior of resetMocks to true, which resets mock state before each test.

You can revert to the prior behavior by disabling resetMocks in package.json

  "jest": {
    "resetMocks": false
  }

A discussion to change the default back again is currently an open issue on their Github: https://github.com/facebook/create-react-app/issues/9935

like image 165
MarkWPiper Avatar answered Sep 21 '22 22:09

MarkWPiper