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

前端 未结 3 1653
暗喜
暗喜 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:59

    You can mock the module so it returns spies and import it into your test:

    import {navigationEnabled, guidanceEnabled} from '../../../magic/index'
    
    jest.mock('../../../magic/index', () => ({
        navigationEnabled: jest.fn(),
        guidanceEnabled: jest.fn()
    }));
    

    Then later on you can change the actual implementation using mockImplementation

    navigationEnabled.mockImplementation(()=> true)
    //or
    navigationEnabled.mockReturnValueOnce(true);
    

    and in the next test

    navigationEnabled.mockImplementation(()=> false)
    //or
    navigationEnabled.mockReturnValueOnce(false);
    

提交回复
热议问题