File tree 2 files changed +19
-4
lines changed
2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -162,12 +162,18 @@ export class FetchMock {
162
162
matcher ?: RouteMatcher | UserRouteConfig ,
163
163
name ?: RouteName ,
164
164
) : FetchMock {
165
+ const boundFetch = this . config . fetch . bind ( globalThis ) ;
165
166
if ( matcher ) {
166
- //@ts -expect-error TODO findo out how to overload an overload
167
- this . route ( matcher , ( { args } ) => this . config . fetch ( ...args ) , name ) ;
167
+ this . route (
168
+ // @ts -expect-error related to the overloading of .route()
169
+ matcher ,
170
+ // @ts -expect-error this is just args from a fetch call being passed into a bound fetch - no idea why the error
171
+ ( { args } ) => boundFetch ( ...args ) ,
172
+ name ,
173
+ ) ;
168
174
} else {
169
- //@ts -expect-error TODO findo out how to overload an overload
170
- this . catch ( ( { args } ) => this . config . fetch ( ...args ) ) ;
175
+ // @ts -expect-error this is just args from a fetch call being passed into a bound fetch - no idea why the error
176
+ this . catch ( ( { args } ) => boundFetch ( ...args ) ) ;
171
177
}
172
178
173
179
return this ;
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ describe('mock and spy', () => {
59
59
describe ( '.spy()' , ( ) => {
60
60
testChainableMethod ( 'spy' ) ;
61
61
testChainableMethod ( 'spyGlobal' ) ;
62
+
62
63
it ( 'passes all requests through to the network by default' , async ( ) => {
63
64
vi . spyOn ( fm . config , 'fetch' ) ;
64
65
fm . spy ( ) ;
@@ -134,5 +135,13 @@ describe('mock and spy', () => {
134
135
method : 'post' ,
135
136
} ) ;
136
137
} ) ;
138
+
139
+ it ( 'can call actual native fetch without erroring' , async ( ) => {
140
+ fm . spyGlobal ( ) ;
141
+ const isBrowser = Boolean ( globalThis . location ) ;
142
+ // avoids getting caught by a cors error
143
+ const testUrl = isBrowser ? '/' : 'http://example.com/' ;
144
+ await expect ( fm . fetchHandler ( testUrl ) ) . resolves . toBeInstanceOf ( Response ) ;
145
+ } ) ;
137
146
} ) ;
138
147
} ) ;
You can’t perform that action at this time.
0 commit comments