Created
June 23, 2022 09:14
-
-
Save JosePedroDias/266e63176055f07d9a6f86ea7d5f307a to your computer and use it in GitHub Desktop.
fetch for 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
// to capture actual responses | |
let i = 0; | |
global.fetch = jest.fn(async (url, options) => { | |
console.log(`#${i++}: ${options.method || 'GET'} ${url}`); | |
const resp = await originalFetch(url, options); | |
const body = await resp.json(); | |
console.log(` status: ${resp.status}, body: ${JSON.stringify(body, null, 2)}`); | |
return body; | |
}); | |
// to serve static responses in jest/etc | |
global.fetch = jest.fn((url, options) => | |
Promise.resolve({ | |
status: 200, | |
json: () => Promise.resolve(okTokenPayload), | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment