Jest mock inner function

后端 未结 5 2361
清歌不尽
清歌不尽 2020-11-28 06:49

I have one file called helper.js that consist of two functions

export const funcA = (key) => {
   return funcB(key)
};

export const func         


        
5条回答
  •  清酒与你
    2020-11-28 07:29

    I think this might work

    import * as helper from 'helper';
    
    describe('helper', () => {
       test('testFuncB', () => {
    
       }
       test('testFuncA', () => {
          const mockTestFuncB = jest.mock();
          // spy on calls to testFuncB and respond with a mock function
          jest.spyOn(helper, 'testFuncB').mockImplementationOnce(mockTestFuncB);
    
          // Do the testing ...
    
          // Restore helper.testFuncB to it's original function
          helper.testFuncB.mockRestore();
       }
    }
    

提交回复
热议问题