diff --git a/src/core/utils/request/onUnhandledRequest.test.ts b/src/core/utils/request/onUnhandledRequest.test.ts index fc717c952..7cb9826fc 100644 --- a/src/core/utils/request/onUnhandledRequest.test.ts +++ b/src/core/utils/request/onUnhandledRequest.test.ts @@ -7,10 +7,10 @@ import { } from './onUnhandledRequest' const fixtures = { - warningWithoutSuggestions: `\ + warningWithoutSuggestions: (url = `/api`) => `\ [MSW] Warning: intercepted a request without a matching request handler: - • GET /api + • GET ${url} If you still wish to intercept this unhandled request, please create a request handler for it. Read more: https://mswjs.io/docs/getting-started/mocks`, @@ -46,7 +46,9 @@ test('supports the "bypass" request strategy', async () => { test('supports the "warn" request strategy', async () => { await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn') - expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions) + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(), + ) }) test('supports the "error" request strategy', async () => { @@ -103,7 +105,9 @@ test('supports calling default strategies from the custom callback function', as test('does not print any suggestions given no handlers to suggest', async () => { await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn') - expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions) + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(), + ) }) test('throws an exception given unknown request strategy', async () => { @@ -117,3 +121,25 @@ test('throws an exception given unknown request strategy', async () => { '[MSW] Failed to react to an unhandled request: unknown strategy "invalid-strategy". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.', ) }) + +test('prints with a relative URL and search params', async () => { + await onUnhandledRequest( + new Request(new URL('http://localhost/api?foo=boo')), + 'warn', + ) + + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(`/api?foo=boo`), + ) +}) + +test('prints with an absolute URL and search params', async () => { + await onUnhandledRequest( + new Request(new URL('https://mswjs.io/api?foo=boo')), + 'warn', + ) + + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`), + ) +}) diff --git a/src/core/utils/request/onUnhandledRequest.ts b/src/core/utils/request/onUnhandledRequest.ts index 55e7dcef7..fcf4d5055 100644 --- a/src/core/utils/request/onUnhandledRequest.ts +++ b/src/core/utils/request/onUnhandledRequest.ts @@ -22,7 +22,7 @@ export async function onUnhandledRequest( strategy: UnhandledRequestStrategy = 'warn', ): Promise { const url = new URL(request.url) - const publicUrl = toPublicUrl(url) + const publicUrl = toPublicUrl(url) + url.search const unhandledRequestMessage = `intercepted a request without a matching request handler:\n\n \u2022 ${request.method} ${publicUrl}\n\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks`