Two ways to fix the Jest test error “the module factory of `jest.mock()` is not allowed to reference any out-of-scope variables”
Say you have this test:
It’s not going to work…
Jest will complain that:
The module factory of “jest.mock()” is not allowed to reference any out-of-scope variables.
Prepend your jest.fn() variable declaration with mock. So, in the above example, navigateToProfile becomes mockNavigateToProfile:
Use jest.doMock() instead of jest.mock():
🌻 Note: if you want to use this method, there are some additional steps you’ll first have to follow.