1- /**
2- * @vitest -environment jsdom
3- */
1+ // @vitest -environment jsdom
42import {
53 onUnhandledRequest ,
64 UnhandledRequestCallback ,
@@ -90,7 +88,7 @@ test('supports the "error" request strategy', async () => {
9088} )
9189
9290test ( 'supports a custom callback function' , async ( ) => {
93- const callback = vi . fn < Parameters < UnhandledRequestCallback > > ( ( request ) => {
91+ const callback = vi . fn < UnhandledRequestCallback > ( ( request ) => {
9492 console . warn ( `callback: ${ request . method } ${ request . url } ` )
9593 } )
9694 const request = new Request ( new URL ( '/user' , 'http://localhost:3000' ) )
@@ -109,12 +107,10 @@ test('supports a custom callback function', async () => {
109107} )
110108
111109test ( 'supports calling default strategies from the custom callback function' , async ( ) => {
112- const callback = vi . fn < Parameters < UnhandledRequestCallback > > (
113- ( request , print ) => {
114- // Call the default "error" strategy.
115- print . error ( )
116- } ,
117- )
110+ const callback = vi . fn < UnhandledRequestCallback > ( ( request , print ) => {
111+ // Call the default "error" strategy.
112+ print . error ( )
113+ } )
118114 const request = new Request ( new URL ( 'http://localhost/api' ) )
119115 await expect ( onUnhandledRequest ( request , callback ) ) . rejects . toThrow (
120116 `[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.` ,
@@ -171,3 +167,62 @@ test('prints with an absolute URL and search params', async () => {
171167 fixtures . warningWithoutSuggestions ( `https://mswjs.io/api?foo=boo` ) ,
172168 )
173169} )
170+
171+ test ( 'ignores common static assets when using the "warn" strategy' , async ( ) => {
172+ await Promise . allSettled ( [
173+ onUnhandledRequest (
174+ new Request ( new URL ( 'https://example.com/main.css' ) ) ,
175+ 'warn' ,
176+ ) ,
177+ onUnhandledRequest (
178+ new Request ( new URL ( 'https://example.com/index.mjs' ) ) ,
179+ 'warn' ,
180+ ) ,
181+ onUnhandledRequest (
182+ new Request ( new URL ( 'https://example.com/node_modules/abc-123' ) ) ,
183+ 'warn' ,
184+ ) ,
185+ onUnhandledRequest (
186+ new Request ( new URL ( 'https://fonts.googleapis.com/some-font' ) ) ,
187+ 'warn' ,
188+ ) ,
189+ ] )
190+
191+ expect ( console . warn ) . not . toHaveBeenCalled ( )
192+ } )
193+
194+ test ( 'ignores common static assets when using the "error" strategy' , async ( ) => {
195+ await Promise . allSettled ( [
196+ onUnhandledRequest (
197+ new Request ( new URL ( 'https://example.com/main.css' ) ) ,
198+ 'error' ,
199+ ) ,
200+ onUnhandledRequest (
201+ new Request ( new URL ( 'https://example.com/index.mjs' ) ) ,
202+ 'error' ,
203+ ) ,
204+ onUnhandledRequest (
205+ new Request ( new URL ( 'https://example.com/node_modules/abc-123' ) ) ,
206+ 'error' ,
207+ ) ,
208+ onUnhandledRequest (
209+ new Request ( new URL ( 'https://fonts.googleapis.com/some-font' ) ) ,
210+ 'error' ,
211+ ) ,
212+ ] )
213+
214+ expect ( console . error ) . not . toHaveBeenCalled ( )
215+ } )
216+
217+ test ( 'exposes common static assets to the explicit callback' , async ( ) => {
218+ let callbackRequest ! : Request
219+ await onUnhandledRequest (
220+ new Request ( new URL ( 'https://example.com/main.css' ) ) ,
221+ ( request ) => {
222+ callbackRequest = request
223+ } ,
224+ )
225+
226+ expect ( callbackRequest ) . toBeInstanceOf ( Request )
227+ expect ( callbackRequest . url ) . toBe ( 'https://example.com/main.css' )
228+ } )
0 commit comments