Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created June 23, 2022 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/266e63176055f07d9a6f86ea7d5f307a to your computer and use it in GitHub Desktop.
Save JosePedroDias/266e63176055f07d9a6f86ea7d5f307a to your computer and use it in GitHub Desktop.
fetch for jest
// 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