unit testing - Mocking dependency of module/function under test in Javascript -
let's want test following example:
import {fetchresourcefrombackend} './myfetch'; const fetchsomething = (dispatch) => { dispatch(2); return fetchresourcefrombackend('/api/abc').then( result => { dispatch(3); }); }; fetchresourcefrombackend complicated function. how can test this, , not affected fetchresourcefrombackend code (any pattern or tool recommendations, use mocha , sinon cannot achieve)?
is option provide fetchresourcefrombackend argument fetchsomething can mock it?
using jest @mikhail suggested solved this:
// test/something-test.js import {myfetch} '../_mocks_/utilities'; jest.domock('modules/utilities', () =>({ myfetch: myfetch })); const {fetchsomething} = require('modules/something'); describe('#fetchsomething', ... here fetchsomething function under test has internal dependency on myfetch function 'modules/utilities'. mock dependency myfetch '../_mocks_/utilities'.
Comments
Post a Comment