Skip to content Skip to sidebar Skip to footer

Mocking Dependency Of Module/function Under Test In Javascript

Let's say I want to test the following for example: import {fetchResourceFromBackend} from './myfetch'; const fetchSomething = (dispatch) => { dispatch(2); return fetchRe

Solution 1:

Using jest as @Mikhail suggested I solved like this:

// test/something-test.jsimport {myFetch} from'../_mocks_/utilities';

jest.doMock('modules/utilities', () =>({
    myFetch: myFetch
}));


const {fetchSomething} = require('modules/something');

describe('#fetchSomething', ...

Here fetchSomething is function under test that has internal dependency on myFetch function from 'modules/utilities'. I mock that dependency with myFetch from '../_mocks_/utilities'.

Post a Comment for "Mocking Dependency Of Module/function Under Test In Javascript"