Created
February 8, 2018 17:06
-
-
Save aleksik/8ca76c88d09c92ddda093471c9a000e4 to your computer and use it in GitHub Desktop.
jest mock node-fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch') | |
const getUsers = () => fetch('https://reqres.in/api/users') | |
.then(res => res.json()) | |
module.exports = getUsers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"jest": "^22.2.1", | |
"jest-fetch-mock": "^1.4.1", | |
"node-fetch": "^2.0.0" | |
}, | |
"scripts": { | |
"test": "jest" | |
}, | |
"jest": { | |
"setupFiles": [ | |
"./setupJest.js" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('jest-fetch-mock') | |
jest.setMock('node-fetch', fetch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JestObject - here it's say it ok