Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/core/utils/request/onUnhandledRequest.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// @vitest-environment node
import { onUnhandledRequest } from './onUnhandledRequest'

const fixtures = {
warningWithoutSuggestions: (url = `/api`) => `\
[MSW] Warning: intercepted a request without a matching request handler:

• GET ${url}

If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/http/intercepting-requests`,
warningWithResponseBody: (url = `/api`) => `\
[MSW] Warning: intercepted a request without a matching request handler:

• POST ${url}

• Request body: {"variables":{"id":"abc-123"},"query":"query UserName($id: String!) { user(id: $id) { name } }"}

If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/http/intercepting-requests`,

errorWithoutSuggestions: `\
[MSW] Error: intercepted a request without a matching request handler:

• GET /api

If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/http/intercepting-requests`,
}

beforeAll(() => {
vi.spyOn(console, 'warn').mockImplementation(() => void 0)
vi.spyOn(console, 'error').mockImplementation(() => void 0)
})

afterEach(() => {
vi.clearAllMocks()
})

afterAll(() => {
vi.restoreAllMocks()
})

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`),
)

await onUnhandledRequest(
new Request(new URL('http://localhost/api?foo=boo')),
'warn',
)

expect(console.warn).toHaveBeenCalledWith(
fixtures.warningWithoutSuggestions(`http://localhost/api?foo=boo`),
)
})
17 changes: 17 additions & 0 deletions src/core/utils/request/toPublicUrl.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @vitest-environment node
import { toPublicUrl } from './toPublicUrl'

test('returns an absolute request URL without search params', () => {
expect(toPublicUrl(new URL('https://test.mswjs.io/path'))).toBe(
'https://test.mswjs.io/path',
)

expect(toPublicUrl(new URL('http://192.168.0.10/path'))).toBe(
'http://192.168.0.10/path',
)

expect(
toPublicUrl(new URL('http://localhost/path?foo=bar')),
'Must not return relative URL in Node.js',
).toBe('http://localhost/path')
})
4 changes: 1 addition & 3 deletions src/core/utils/request/toPublicUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @vitest-environment jsdom
*/
// @vitest-environment jsdom
import { toPublicUrl } from './toPublicUrl'

test('returns an absolute request URL without search params', () => {
Expand Down
15 changes: 8 additions & 7 deletions src/core/utils/request/toPublicUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* to the current origin. Otherwise returns an absolute URL.
*/
export function toPublicUrl(url: string | URL): string {
if (typeof location === 'undefined') {
return url.toString()
}

const urlInstance = url instanceof URL ? url : new URL(url)

return urlInstance.origin === location.origin
? urlInstance.pathname
: urlInstance.origin + urlInstance.pathname
if (
typeof location !== 'undefined' &&
urlInstance.origin === location.origin
) {
return urlInstance.pathname
}

return urlInstance.origin + urlInstance.pathname
}
Loading