Skip to content

Commit 52d3913

Browse files
committed
refactor(tests): replace mock fetch with spy for improved test accuracy
1 parent 17e19e4 commit 52d3913

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

tests/proxy-fallback.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import {
99
beforeEach,
1010
jest,
1111
afterAll,
12-
mock,
12+
spyOn,
1313
} from "bun:test"
1414
import { FetchProxy } from "../src/proxy"
1515

16-
// Mock fetch for testing
17-
const mockFetch = jest.fn()
18-
;(global as any).fetch = mockFetch
16+
// Spy on fetch for testing
17+
let fetchSpy: ReturnType<typeof spyOn>
1918

2019
afterAll(() => {
21-
mock.restore()
20+
fetchSpy?.mockRestore()
2221
})
2322

2423
describe("Proxy Fallback Response", () => {
@@ -29,13 +28,14 @@ describe("Proxy Fallback Response", () => {
2928
base: "https://api.example.com",
3029
timeout: 5000,
3130
})
32-
mockFetch.mockClear()
31+
fetchSpy = spyOn(global, "fetch")
32+
fetchSpy.mockClear()
3333
})
3434

3535
describe("onError Hook Fallback", () => {
3636
it("should return fallback response when onError hook provides one", async () => {
3737
// Mock a network error
38-
mockFetch.mockRejectedValue(new Error("Network error"))
38+
fetchSpy.mockRejectedValue(new Error("Network error"))
3939

4040
const fallbackResponse = new Response(
4141
JSON.stringify({
@@ -68,7 +68,7 @@ describe("Proxy Fallback Response", () => {
6868
})
6969

7070
it("should handle async fallback response generation", async () => {
71-
mockFetch.mockRejectedValue(new Error("Timeout error"))
71+
fetchSpy.mockRejectedValue(new Error("Timeout error"))
7272

7373
const onErrorHook = jest.fn().mockImplementation(async (req, error) => {
7474
// Simulate async fallback logic
@@ -108,7 +108,7 @@ describe("Proxy Fallback Response", () => {
108108
})
109109

110110
it("should fallback to default error response when onError hook returns void", async () => {
111-
mockFetch.mockRejectedValue(new Error("Network error"))
111+
fetchSpy.mockRejectedValue(new Error("Network error"))
112112

113113
const onErrorHook = jest.fn().mockResolvedValue(undefined)
114114

@@ -147,7 +147,7 @@ describe("Proxy Fallback Response", () => {
147147
]
148148

149149
for (const testCase of testCases) {
150-
mockFetch.mockRejectedValue(testCase.error)
150+
fetchSpy.mockRejectedValue(testCase.error)
151151

152152
const onErrorHook = jest.fn().mockResolvedValue(
153153
new Response(JSON.stringify({ message: testCase.fallbackMessage }), {
@@ -179,7 +179,7 @@ describe("Proxy Fallback Response", () => {
179179
})
180180

181181
// First request fails to trigger circuit breaker
182-
mockFetch.mockRejectedValue(new Error("Service error"))
182+
fetchSpy.mockRejectedValue(new Error("Service error"))
183183

184184
const onErrorHook = jest
185185
.fn()
@@ -241,7 +241,7 @@ describe("Proxy Fallback Response", () => {
241241

242242
it("should pass correct request and error objects to onError hook", async () => {
243243
const networkError = new Error("ECONNREFUSED")
244-
mockFetch.mockRejectedValue(networkError)
244+
fetchSpy.mockRejectedValue(networkError)
245245

246246
const onErrorHook = jest
247247
.fn()
@@ -269,7 +269,7 @@ describe("Proxy Fallback Response", () => {
269269
})
270270

271271
it("should handle multiple concurrent requests with fallback", async () => {
272-
mockFetch.mockRejectedValue(new Error("Service unavailable"))
272+
fetchSpy.mockRejectedValue(new Error("Service unavailable"))
273273

274274
const onErrorHook = jest.fn().mockImplementation(async (req, error) => {
275275
return new Response(
@@ -312,7 +312,7 @@ describe("Proxy Fallback Response", () => {
312312
})
313313

314314
it("should handle onError hook that throws an error", async () => {
315-
mockFetch.mockRejectedValue(new Error("Network error"))
315+
fetchSpy.mockRejectedValue(new Error("Network error"))
316316

317317
const onErrorHook = jest.fn().mockImplementation(async () => {
318318
throw new Error("Hook error")
@@ -333,7 +333,7 @@ describe("Proxy Fallback Response", () => {
333333
})
334334

335335
it("should handle fallback response with custom headers", async () => {
336-
mockFetch.mockRejectedValue(new Error("Service error"))
336+
fetchSpy.mockRejectedValue(new Error("Service error"))
337337

338338
const onErrorHook = jest.fn().mockResolvedValue(
339339
new Response(JSON.stringify({ fallback: true }), {
@@ -359,7 +359,7 @@ describe("Proxy Fallback Response", () => {
359359
})
360360

361361
it("should handle streaming fallback response", async () => {
362-
mockFetch.mockRejectedValue(new Error("Streaming error"))
362+
fetchSpy.mockRejectedValue(new Error("Streaming error"))
363363

364364
const onErrorHook = jest.fn().mockImplementation(async (req, error) => {
365365
const stream = new ReadableStream({
@@ -397,7 +397,7 @@ describe("Proxy Fallback Response", () => {
397397

398398
describe("Integration with Other Features", () => {
399399
it("should work with beforeRequest and afterResponse hooks", async () => {
400-
mockFetch.mockRejectedValue(new Error("Network error"))
400+
fetchSpy.mockRejectedValue(new Error("Network error"))
401401

402402
const beforeRequestHook = jest.fn()
403403
const afterResponseHook = jest.fn()
@@ -420,7 +420,7 @@ describe("Proxy Fallback Response", () => {
420420
})
421421

422422
it("should work with custom headers and query parameters", async () => {
423-
mockFetch.mockRejectedValue(new Error("Network error"))
423+
fetchSpy.mockRejectedValue(new Error("Network error"))
424424

425425
const onErrorHook = jest
426426
.fn()

0 commit comments

Comments
 (0)