I have the following situation:
A.js
import fetch from 'node-fetch'
import httpClient from './myClient/httpClient'
export default class{
async init(){
const response = await fetch('some_url')
return httpClient.init(response.payload)
}
}
A_spec.js
import test from 'ava'
import sinon from 'sinon'
import fetch from 'node-fetch'
import httpClient from './myClient/httpClient'
import A from './src/A'
test('a async test', async (t) => {
const instance = new A()
const stubbedHttpInit = sinon.stub(httpClient, 'init')
sinon.stub(fetch).returns(Promise.resolve({payload: 'data'})) //this doesn't work
await instance.init()
t.true(stubbedHttpInit.init.calledWith('data'))
})
My idea it's check if the httpClient's init method has been called using the payload obtained in a fetch request.
My question is: How I can mock the fetch dependency for stub the returned value when i test the A's init method?
fetch-mock allows mocking http requests made using fetch or a library imitating its api, such as node-fetch or fetch-ponyfill. It supports most JavaScript environments, including Node. js, web workers, service workers, and any browser that either supports fetch natively or that can have a fetch polyfill installed.
In Jest, Node. js modules are automatically mocked in your tests when you place the mock files in a __mocks__ folder that's next to the node_modules folder. For example, if you a file called __mock__/fs. js , then every time the fs module is called in your test, Jest will automatically use the mocks.
Finally I resolved this problem stubbing the fetch.Promise
reference like this:
sinon.stub(fetch, 'Promise').returns(Promise.resolve(responseObject))
the explanation for this it's that node-fetch
have a reference to the native Promise and when you call fetch()
, this method returns a fetch.Promise
. Check this out
sinon.stub(fetch)
can't stub a function itself. Instead you need to stub the node-fetch
dependency from inside ./src/A
, perhaps using something like proxyquire
:
import proxyquire from 'proxyquire`
const A = proxyquire('./src/A', {
'node-fetch': sinon.stub().returns(Promise.resolve({payload: 'data'}))
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With