how to change jest mock function return value in each test?

前端 未结 3 1673
暗喜
暗喜 2020-12-23 11:29

I have a mock module like this in my component test file

  jest.mock(\'../../../magic/index\', () => ({
    navigationEnabled: () => true,
    guidance         


        
3条回答
  •  伪装坚强ぢ
    2020-12-23 11:33

    I had a hard time getting the accepted answers to work - my equivalents of navigationEnabled and guidanceEnabled were undefined when I tried to call mockReturnValueOnce on them.

    Here's what I had to do:

    In ../../../magic/__mocks__/index.js:

    export const navigationEnabled = jest.fn();
    export const guidanceEnabled = jest.fn();
    

    in my index.test.js file:

    jest.mock('../../../magic/index');
    import { navigationEnabled, guidanceEnabled } from '../../../magic/index';
    import { functionThatReturnsValueOfNavigationEnabled } from 'moduleToTest';
    
    it('is able to mock', () => {
      navigationEnabled.mockReturnValueOnce(true);
      guidanceEnabled.mockReturnValueOnce(true);
      expect(functionThatReturnsValueOfNavigationEnabled()).toBe(true);
    });
    

提交回复
热议问题