Skip to content

Commit

Permalink
fix: preserve search params in "onUnhandledRequest" messages (#2128)
Browse files Browse the repository at this point in the history
Co-authored-by: Nagadev <dev@imac.local>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
3 people committed Apr 17, 2024
1 parent f948d13 commit 64bcae7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/core/utils/request/onUnhandledRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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`),
)
})
2 changes: 1 addition & 1 deletion src/core/utils/request/onUnhandledRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function onUnhandledRequest(
strategy: UnhandledRequestStrategy = 'warn',
): Promise<void> {
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`

Expand Down

0 comments on commit 64bcae7

Please sign in to comment.