Jest mock inner function

后端 未结 5 2435
清歌不尽
清歌不尽 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:28

    Late answer but this should work. Also you should test funcB in its own file and not inside the 'helper' tests.

    import { funcB } from './funcB';
    import { funcA } from './helper';
    
    jest.mock('./funcB');
    
    describe('helper', () => {
        test('test funcA', () => {
            const funcBSpy = jest.fn();
            funcB.mockImplementation(() => funcBSpy());
    
            expect(funcBSpy).toHaveBeenCalledTimes(1);
        });
    });
    

提交回复
热议问题