11import {
22 createExecutionContext ,
33 env ,
4- fetchMock ,
54 waitOnExecutionContext ,
65} from "cloudflare:test" ;
7- import { afterEach , beforeAll , describe , expect , it } from "vitest" ;
6+ import { HttpResponse , http } from "msw" ;
7+ import { setupServer } from "msw/node" ;
8+ import { afterAll , afterEach , beforeAll , describe , expect , it } from "vitest" ;
89
910import fixtures from "../contract/fixtures.json" ;
1011import worker , { lookupKey } from "../src/index" ;
1112
12- beforeAll ( ( ) => {
13- fetchMock . activate ( ) ;
14- fetchMock . disableNetConnect ( ) ;
15- } ) ;
16- afterEach ( ( ) => fetchMock . assertNoPendingInterceptors ( ) ) ;
13+ // onUnhandledRequest: "error" is the disableNetConnect() of MSW — any
14+ // outbound fetch a test didn't arm via expectOriginFetch fails loudly.
15+ const server = setupServer ( ) ;
16+ beforeAll ( ( ) => server . listen ( { onUnhandledRequest : "error" } ) ) ;
17+ afterEach ( ( ) => server . resetHandlers ( ) ) ;
18+ afterAll ( ( ) => server . close ( ) ) ;
1719
1820/** Stub the next passthrough to origin and tag it so tests can assert
1921 * "this request reached origin" unambiguously. */
@@ -22,7 +24,21 @@ function expectOriginFetch(
2224 path : string | RegExp = / .* / ,
2325 method = "GET" ,
2426) {
25- fetchMock . get ( host ) . intercept ( { path, method } ) . reply ( 200 , "origin-served" ) ;
27+ server . use (
28+ http . all (
29+ `${ host } /*` ,
30+ ( { request } ) => {
31+ const url = new URL ( request . url ) ;
32+ const pathMatches =
33+ typeof path === "string"
34+ ? url . pathname === path
35+ : path . test ( url . pathname ) ;
36+ if ( request . method !== method || ! pathMatches ) return undefined ;
37+ return HttpResponse . text ( "origin-served" ) ;
38+ } ,
39+ { once : true } ,
40+ ) ,
41+ ) ;
2642}
2743
2844// The handler is typed for edge-ingress requests (IncomingRequestCfProperties);
0 commit comments