jpbrown84
jpbrown84
CDCloudflare Developers
Created by jpbrown84 on 12/29/2023 in #workers-help
Mocking fetch with Jest for a Cloudlare Worker
Hi all - i'm trying to figure out something that potentially could be very simple. I've got a Worker running on Cloudflare that pulls data from an external API, via fetch. Inside my 'fetchData.ts' I have the following.
const response = await fetch(env.ENDPOINT, {
method: "POST",
headers: {
Authorization: `Basic ${btoa(
`someKey`
)}]`,
"Content-Type": "application/x-www-form-urlencoded"
},
body: "someBody"
});
const response = await fetch(env.ENDPOINT, {
method: "POST",
headers: {
Authorization: `Basic ${btoa(
`someKey`
)}]`,
"Content-Type": "application/x-www-form-urlencoded"
},
body: "someBody"
});
I'm trying to write a 'testData.test.ts' file to check that fetch was called with the correct args. If I was working with a node environment i'd just mock out node-fetch and check what the mock was called with. With the worker, fetch is global, so I have no idea what to mock. Any help with this would be much appreciated
5 replies