Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Last active November 26, 2024 16:36
Show Gist options
  • Save KonnorRogers/11cbf7bc92143880f60d1453d81c6e17 to your computer and use it in GitHub Desktop.
Save KonnorRogers/11cbf7bc92143880f60d1453d81c6e17 to your computer and use it in GitHub Desktop.
fetch-mocking
const originalFetch = window.fetch
window.fetch = function (url, options = {}) {
if (url === "http://example.net/locale") {
return new Response(JSON.stringify({
locale: "en-US",
timeZone: "UTC"
}))
} else {
throw Error(`${url} is not mocked.`)
}
}
const resp = await fetch("http://example.net/locale")
const json = await resp.json()
console.log(json)
// {
// "locale": "en-US",
// "timeZone": "UTC"
// }
window.fetch = originalFetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment