After you’ve mocked the module and replaced the methodToMock
with a spy, you need to import it. Then, at each test, you can change the behaviour of methodToMock
by calling the mockImplementation
spy method.
jest.mock('the-package-to-mock', () => ({
methodToMock: jest.fn()
}))
import { methodToMock } from 'the-package-to-mock'
it('Test A', () => {
methodToMock.mockImplementation(() => 'Value A')
// Test your function that uses the mocked package's function here.
})
it('Test B', () => {
methodToMock.mockImplementation(() => 'Value B')
// Test the code again, but now your function will get a different value when it calls the mocked package's function.
})