Skip to content

Commit 64bcae7

Browse files
nagadevkrishnaNagadevkettanaito
authoredApr 17, 2024··
fix: preserve search params in "onUnhandledRequest" messages (#2128)
Co-authored-by: Nagadev <dev@imac.local> Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent f948d13 commit 64bcae7

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed
 

‎src/core/utils/request/onUnhandledRequest.test.ts

+30-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
} from './onUnhandledRequest'
88

99
const fixtures = {
10-
warningWithoutSuggestions: `\
10+
warningWithoutSuggestions: (url = `/api`) => `\
1111
[MSW] Warning: intercepted a request without a matching request handler:
1212
13-
• GET /api
13+
• GET ${url}
1414
1515
If you still wish to intercept this unhandled request, please create a request handler for it.
1616
Read more: https://mswjs.io/docs/getting-started/mocks`,
@@ -46,7 +46,9 @@ test('supports the "bypass" request strategy', async () => {
4646
test('supports the "warn" request strategy', async () => {
4747
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')
4848

49-
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
49+
expect(console.warn).toHaveBeenCalledWith(
50+
fixtures.warningWithoutSuggestions(),
51+
)
5052
})
5153

5254
test('supports the "error" request strategy', async () => {
@@ -103,7 +105,9 @@ test('supports calling default strategies from the custom callback function', as
103105
test('does not print any suggestions given no handlers to suggest', async () => {
104106
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')
105107

106-
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
108+
expect(console.warn).toHaveBeenCalledWith(
109+
fixtures.warningWithoutSuggestions(),
110+
)
107111
})
108112

109113
test('throws an exception given unknown request strategy', async () => {
@@ -117,3 +121,25 @@ test('throws an exception given unknown request strategy', async () => {
117121
'[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.',
118122
)
119123
})
124+
125+
test('prints with a relative URL and search params', async () => {
126+
await onUnhandledRequest(
127+
new Request(new URL('http://localhost/api?foo=boo')),
128+
'warn',
129+
)
130+
131+
expect(console.warn).toHaveBeenCalledWith(
132+
fixtures.warningWithoutSuggestions(`/api?foo=boo`),
133+
)
134+
})
135+
136+
test('prints with an absolute URL and search params', async () => {
137+
await onUnhandledRequest(
138+
new Request(new URL('https://mswjs.io/api?foo=boo')),
139+
'warn',
140+
)
141+
142+
expect(console.warn).toHaveBeenCalledWith(
143+
fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`),
144+
)
145+
})

‎src/core/utils/request/onUnhandledRequest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function onUnhandledRequest(
2222
strategy: UnhandledRequestStrategy = 'warn',
2323
): Promise<void> {
2424
const url = new URL(request.url)
25-
const publicUrl = toPublicUrl(url)
25+
const publicUrl = toPublicUrl(url) + url.search
2626

2727
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`
2828

0 commit comments

Comments
 (0)
Please sign in to comment.