Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 24, 2025
1 parent 750c2a8 commit a5fb3de
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
75 changes: 65 additions & 10 deletions src/core/utils/request/onUnhandledRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @vitest-environment jsdom
*/
// @vitest-environment jsdom
import {
onUnhandledRequest,
UnhandledRequestCallback,
Expand Down Expand Up @@ -90,7 +88,7 @@ test('supports the "error" request strategy', async () => {
})

test('supports a custom callback function', async () => {
const callback = vi.fn<Parameters<UnhandledRequestCallback>>((request) => {
const callback = vi.fn<UnhandledRequestCallback>((request) => {
console.warn(`callback: ${request.method} ${request.url}`)
})
const request = new Request(new URL('/user', 'http://localhost:3000'))
Expand All @@ -109,12 +107,10 @@ test('supports a custom callback function', async () => {
})

test('supports calling default strategies from the custom callback function', async () => {
const callback = vi.fn<Parameters<UnhandledRequestCallback>>(
(request, print) => {
// Call the default "error" strategy.
print.error()
},
)
const callback = vi.fn<UnhandledRequestCallback>((request, print) => {
// Call the default "error" strategy.
print.error()
})
const request = new Request(new URL('http://localhost/api'))
await expect(onUnhandledRequest(request, callback)).rejects.toThrow(
`[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
Expand Down Expand Up @@ -171,3 +167,62 @@ test('prints with an absolute URL and search params', async () => {
fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`),
)
})

test('ignores common static assets when using the "warn" strategy', async () => {
await Promise.allSettled([
onUnhandledRequest(
new Request(new URL('https://example.com/main.css')),
'warn',
),
onUnhandledRequest(
new Request(new URL('https://example.com/index.mjs')),
'warn',
),
onUnhandledRequest(
new Request(new URL('https://example.com/node_modules/abc-123')),
'warn',
),
onUnhandledRequest(
new Request(new URL('https://fonts.googleapis.com/some-font')),
'warn',
),
])

expect(console.warn).not.toHaveBeenCalled()
})

test('ignores common static assets when using the "error" strategy', async () => {
await Promise.allSettled([
onUnhandledRequest(
new Request(new URL('https://example.com/main.css')),
'error',
),
onUnhandledRequest(
new Request(new URL('https://example.com/index.mjs')),
'error',
),
onUnhandledRequest(
new Request(new URL('https://example.com/node_modules/abc-123')),
'error',
),
onUnhandledRequest(
new Request(new URL('https://fonts.googleapis.com/some-font')),
'error',
),
])

expect(console.error).not.toHaveBeenCalled()
})

test('exposes common static assets to the explicit callback', async () => {
let callbackRequest!: Request
await onUnhandledRequest(
new Request(new URL('https://example.com/main.css')),
(request) => {
callbackRequest = request
},
)

expect(callbackRequest).toBeInstanceOf(Request)
expect(callbackRequest.url).toBe('https://example.com/main.css')
})
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ afterAll(async () => {
await httpServer.close()
})

test('errors on unhandled request when using the "error" value', async () => {
test('errors on unhandled request when using the "error" strategy', async () => {
const endpointUrl = httpServer.http.url('/')
const makeRequest = () => {
return fetch(endpointUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'

const server = setupServer(
http.get('https://test.mswjs.io/user', () => {
http.get('https://example.com/user', () => {
return HttpResponse.json({ firstName: 'John' })
}),
)
Expand All @@ -20,14 +20,14 @@ afterAll(() => {
vi.restoreAllMocks()
})

test('warns on unhandled request when using the "warn" value', async () => {
const res = await fetch('https://test.mswjs.io')
test('warns on unhandled request when using the "warn" strategy', async () => {
const response = await fetch('https://example.com')

expect(res).toHaveProperty('status', 404)
expect(response).toHaveProperty('status', 404)

Check failure on line 26 in test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts

View workflow job for this annotation

GitHub Actions / test (node.js) (20)

test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts > warns on unhandled request when using the "warn" strategy

AssertionError: expected Response{ …(2) } to have property "status" with value 404 - Expected + Received - 404 + 200 ❯ test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts:26:20

Check failure on line 26 in test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts

View workflow job for this annotation

GitHub Actions / test (node.js) (18)

test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts > warns on unhandled request when using the "warn" strategy

AssertionError: expected Response{ [Symbol(realm)]: null, …(2) } to have property "status" with value 404 - Expected + Received - 404 + 200 ❯ test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts:26:20
expect(console.warn).toBeCalledWith(`\
[MSW] Warning: intercepted a request without a matching request handler:
• GET https://test.mswjs.io/
• GET https://example.com/
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

0 comments on commit a5fb3de

Please sign in to comment.