Created
July 7, 2019 16:02
-
-
Save delbetu/21645a6876b828355030992045f5c767 to your computer and use it in GitHub Desktop.
Javascript Mocking fetch with jest
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
jest.mock('node-fetch'); | |
import fetch, {Response} from 'node-fetch'; | |
it('can mock fetch body', () => { | |
fetch.mockImplementation(() => { | |
return new Promise((resolve, reject)=> { resolve( | |
{ | |
json: () => { return new Promise((resolve, reject) => resolve({ name: 'value' })) } | |
} | |
)}) | |
}) | |
const resultData = fetch('url').then((response) => { | |
console.log(response) | |
return response.json() | |
}).then((data)=>{ | |
console.log(data) | |
return data | |
}) | |
expect(resultData).resolves.toEqual({ name: 'value' }) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment