Reputation:
I am using manual mock for a module. I want to clear a mocked function in the manual mock and mock return a new value for it. How can I do that? I tried many ways but it always returned value in manual mock.
I use jest.clearAllMocks()
and jest.resetAllMocks()
in beforeEach and use mockReturnValue
to mock new value for that function but it doesn't change.
This is my manual mock
const firebase = {};
firebase.storage = {
bucket: jest.fn(),
upload: jest
.fn()
.mockResolvedValue([
{ getSignedUrl: jest.fn().mockResolvedValue(["link"]) }
]),
file: _ => ({
delete: jest.fn()
})
};
module.exports = firebase;
I get value link
but I can't mock other values in my unit test
Upvotes: 9
Views: 15782
Reputation: 110892
You need to use one of this a the specific mock:
mockFn.mockClear()
: will remove all stored information about calling the mockmockFn.mockRestore()
: same above plus removing the mocked return valuesmockImplementation
: set a new return value for the mockThere are 2 ways to create the mockFn instance either:
const mockFn = jest.fn()
or by importing a mock:
import mockFn from 'mockedModule'
jest.mock('mockedModule', ()=> jest.fn())
Upvotes: 13