Last active
November 26, 2024 16:36
-
-
Save KonnorRogers/11cbf7bc92143880f60d1453d81c6e17 to your computer and use it in GitHub Desktop.
fetch-mocking
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
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