How to selectively use the actual module for a manually mocked, scoped module with Jest?

Viewed 108

I have a manual mock for a scoped module (that is, a file inside the mocks directory), basically a fork of Chakra UI, which according to the docs is automatically mocked for every test. That works fine, but let's say I want to use the actual module for some test, how can I do that?

I'm not sure if it's important to see the code for the manual mock itself but it's just exporting a couple functions right now like so:

export const useToast = jest.fn();
export const Box = jest.fn();

The test merely renders children inside two Boxes with particular styles, but Box always comes out as jest.fn(). I feel like I've tried everything: unmock, re-mocking with mock and requireActual, mockImplementation but nothing seems to work. It always uses the manual mock. Any ideas? Thanks.

1 Answers

Up to my understanding, you mean you have now in your test

jest.mock('../someModule.js', ....)

If that's correct, and question is "how to disable that mock for particular test and use real module?" then answer is move that "test with real module code" to separate file. As jest's docs highlight:

If you're using ES module imports then you'll normally be inclined to put your import statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist jest.mock calls to the top of the module (before any imports).

I don't know another way. jest.unmock is for different purpose - to disable automocking when there is a mock in __mocks__ folder and automocking is enabled. It does not to "partially disable manual mock".

Related