Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jest automatically restore mocked modules between test modules?

Does jest automatically restore mocked modules between test files? For example, if I call jest.mock('some_module') in one file, do I need to ensure I call jest.unmock('some_module') after all the tests are run in that file?

It's not clear to me whether that happens in the documentation.

like image 833
Charlie Truong Avatar asked Dec 10 '22 11:12

Charlie Truong


1 Answers

You don't have to reset the mocks, as the test are run in parallel, every test file run in its own sandboxed thread. Even mocking JavaScript globals like Date or Math.random only affects the actual test file.

The only problem we had so far was mocking process.env.NODE_ENV which affected other test that run at the same time. But reseting this after the test run solved the problem.

like image 99
Andreas Köberle Avatar answered Jan 01 '23 13:01

Andreas Köberle