Skip to content

Instantly share code, notes, and snippets.

@delbetu
Created July 7, 2019 16:02
Show Gist options
  • Save delbetu/21645a6876b828355030992045f5c767 to your computer and use it in GitHub Desktop.
Save delbetu/21645a6876b828355030992045f5c767 to your computer and use it in GitHub Desktop.
Javascript Mocking fetch with jest
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