@@ -9,18 +9,17 @@ import {
9
9
beforeEach ,
10
10
jest ,
11
11
afterAll ,
12
- mock ,
12
+ spyOn ,
13
13
} from "bun:test"
14
14
import { FetchProxy } from "../src/proxy"
15
15
import { CircuitState } from "../src/types"
16
16
import type { ProxyRequestOptions , CircuitBreakerResult } from "../src/types"
17
17
18
- // Mock fetch for testing
19
- const mockFetch = jest . fn ( )
20
- ; ( global as any ) . fetch = mockFetch
18
+ // Spy on fetch for testing
19
+ let fetchSpy : ReturnType < typeof spyOn >
21
20
22
21
afterAll ( ( ) => {
23
- mock . restore ( )
22
+ fetchSpy ?. mockRestore ( )
24
23
} )
25
24
26
25
describe ( "Enhanced Hook Naming Conventions" , ( ) => {
@@ -39,8 +38,9 @@ describe("Enhanced Hook Naming Conventions", () => {
39
38
headers : new Headers ( { "content-type" : "application/json" } ) ,
40
39
} )
41
40
42
- mockFetch . mockClear ( )
43
- mockFetch . mockResolvedValue ( mockResponse )
41
+ fetchSpy = spyOn ( global , "fetch" )
42
+ fetchSpy . mockClear ( )
43
+ fetchSpy . mockResolvedValue ( mockResponse )
44
44
} )
45
45
46
46
describe ( "beforeRequest Hook" , ( ) => {
@@ -56,7 +56,7 @@ describe("Enhanced Hook Naming Conventions", () => {
56
56
57
57
expect ( beforeRequestHook ) . toHaveBeenCalledTimes ( 1 )
58
58
expect ( beforeRequestHook ) . toHaveBeenCalledWith ( request , options )
59
- expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
59
+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 )
60
60
} )
61
61
62
62
it ( "should handle async beforeRequest hooks" , async ( ) => {
@@ -139,7 +139,7 @@ describe("Enhanced Hook Naming Conventions", () => {
139
139
const request = new Request ( "https://example.com/test" )
140
140
const error = new Error ( "Network error" )
141
141
142
- mockFetch . mockRejectedValueOnce ( error )
142
+ fetchSpy . mockRejectedValueOnce ( error )
143
143
144
144
const options : ProxyRequestOptions = {
145
145
afterCircuitBreakerExecution : afterCircuitBreakerHook ,
@@ -166,7 +166,7 @@ describe("Enhanced Hook Naming Conventions", () => {
166
166
const request = new Request ( "https://example.com/test" )
167
167
168
168
// Add some delay to the fetch
169
- mockFetch . mockImplementationOnce (
169
+ fetchSpy . mockImplementationOnce (
170
170
( ) =>
171
171
new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( mockResponse ) , 50 ) ) ,
172
172
)
@@ -296,8 +296,8 @@ describe("Enhanced Hook Naming Conventions", () => {
296
296
await proxy . proxy ( request , undefined , options )
297
297
298
298
// Verify the mock was called (we can't easily verify exact headers due to internal processing)
299
- expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
300
- expect ( mockFetch ) . toHaveBeenCalledWith (
299
+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 )
300
+ expect ( fetchSpy ) . toHaveBeenCalledWith (
301
301
expect . any ( String ) ,
302
302
expect . objectContaining ( {
303
303
headers : expect . any ( Headers ) ,
@@ -315,7 +315,7 @@ describe("Enhanced Hook Naming Conventions", () => {
315
315
} ,
316
316
} )
317
317
318
- mockFetch . mockResolvedValueOnce ( originalResponse )
318
+ fetchSpy . mockResolvedValueOnce ( originalResponse )
319
319
320
320
const request = new Request ( "https://example.com/test" )
321
321
0 commit comments