Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve search params in "onUnhandledRequest" messages #2128

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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