zedevs
zedevs
CDCloudflare Developers
Created by zedevs on 7/10/2023 in #workers-help
Mocking fetch
These are the approaches I've tried 1. jest-fetch-mock - https://www.npmjs.com/package/jest-fetch-mock
// src/helpers.test.ts
import fetch from 'jest-fetch-mock';
import { sendToApi } from './helpers'
beforeEach(() => {
fetch.resetMocks();
});

describe('helpers', () => {
describe('sendToSuperglue', () => {
// the line bellow acutally makes a net call...
await sendToApi(url, {}, env, request, ctx)).status)
});
});
// src/helpers.test.ts
import fetch from 'jest-fetch-mock';
import { sendToApi } from './helpers'
beforeEach(() => {
fetch.resetMocks();
});

describe('helpers', () => {
describe('sendToSuperglue', () => {
// the line bellow acutally makes a net call...
await sendToApi(url, {}, env, request, ctx)).status)
});
});
2. Using getMiniflareFetchMock(): I dug up some documentation and also read https://discord.com/channels/595317990191398933/1078852838102417470/1078852838102417470 , but I get Cannot find name 'getMiniflareFetchMock'. I might have to configure wrangler/miniflare types, but I can't find any resource on that. I would appreciate any help. Thank you!
3 replies