javascriptreactjsunit-testingjestjsmocking

What is the difference between these two mock ways in Jest?


I have some custom hook in React. I am working with unit-tests (Jest) where I have to mock this hook.

const theHook = () => {

// some code

return {func1, func2};
}

I have discovered that the two examples below are not the same.

import theHook from '../theHook';

jest.mock('../theHook');

theHook.mockReturnValue({
    func1: jest.fn(),
    func2: jest.fn()
});

and

import theHook from '../theHook';

jest.mock('../theHook', () => ({
    func1: jest.fn(),
    func2: jest.fn()
}));

Who can explain to me the difference between these two mock ways?


Solution

  • Key Differences:

    When to Use Each: