Last active
January 16, 2020 23:54
-
-
Save bendera/559a5fa92cb819e06f9e4a447315e15b to your computer and use it in GitHub Desktop.
Mocking the Fetch API in a Jest/JSDOM environment
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
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