Sometimes you are testing a component and need to mock a module (in this case I needed to mock a zustand store), but with the exemple of "next/navigation" mock, you are mocking the module for entire test suit on the file that you declare the jest.mock()
. But, if you want to mock a specific implementation for each test? So first, with need to declare the module mock, without pass the implementation function:
In this case we are mocking "../../stores/sounds-state-store"
module
And, inside each test, we will use the jest.Mock.mockImplementation()
function to mock the implementation of module inside it
/test
scope:
the store we want to mock is useSoundsStateStore
, from '../../stores/sounds-state-store'
Full code:
Note that we use:
instead of:
Because it's a typescript project, and we need to tell the ts compiler that useSoundsStateStore
have jest.Mock
functions on this scope.
references: