Skip to content

Instantly share code, notes, and snippets.

@bendera
Last active January 16, 2020 23:54
Show Gist options
  • Save bendera/559a5fa92cb819e06f9e4a447315e15b to your computer and use it in GitHub Desktop.
Save bendera/559a5fa92cb819e06f9e4a447315e15b to your computer and use it in GitHub Desktop.
Mocking the Fetch API in a Jest/JSDOM environment
global.fetch = jest.fn().mockImplementation((url) => {
return Promise.resolve({
json: () => {
if (url === 'foo/bar') {
// json successfully parsed
return Promise.resolve({success: true});
}
// json parse error
return Promise.reject(new Error('JSON parse error));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment