user7350430
user7350430

Reputation:

Clear manual mock jest

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

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 110892

You need to use one of this a the specific mock:

There 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

Related Questions