Skip to content

Instantly share code, notes, and snippets.

@aleksik
Created February 8, 2018 17:06
Show Gist options
  • Save aleksik/8ca76c88d09c92ddda093471c9a000e4 to your computer and use it in GitHub Desktop.
Save aleksik/8ca76c88d09c92ddda093471c9a000e4 to your computer and use it in GitHub Desktop.
jest mock node-fetch
const fetch = require('node-fetch')
const getUsers = () => fetch('https://reqres.in/api/users')
.then(res => res.json())
module.exports = getUsers
const getUsers = require('./getUsers')
const fetch = require('node-fetch')
it('fetches users', async () => {
const mockResponse = [
{ id: 1, name: 'Simo' },
{ id: 2, name: 'Massimo' },
]
fetch.mockResponse(JSON.stringify(mockResponse))
expect(await getUsers()).toEqual(mockResponse)
})
{
"dependencies": {
"jest": "^22.2.1",
"jest-fetch-mock": "^1.4.1",
"node-fetch": "^2.0.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"setupFiles": [
"./setupJest.js"
]
}
}
const fetch = require('jest-fetch-mock')
jest.setMock('node-fetch', fetch)
@GeorgianStan
Copy link

JestObject - here it's say it ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment