Skip to content

Commit

Permalink
test: add onUnhandledRequest default behavior test
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 9, 2025
1 parent a70b518 commit 0d03db2
Showing 1 changed file with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// @vitest-environment node
import { HttpResponse, http } from 'msw'
import { http } from 'msw'
import { setupRemoteServer } from 'msw/node'
import { spawnTestApp } from './utils'

const remote = setupRemoteServer()

beforeAll(async () => {
/**
* @note Console warnings from the app's context are forwarded
* as `console.error`. Ignore those for this test.
*/
vi.spyOn(console, 'error').mockImplementation(() => {})
vi.spyOn(console, 'warn').mockImplementation(() => {})
await remote.listen()
})
Expand All @@ -25,23 +30,80 @@ it(
remote.boundary(async () => {
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))

await fetch(new URL('/resource', testApp.url))
// Hit a special endpoint that will perform a request to "Location"
// in the application's context. Neither party handles this request.
await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/unhandled',
},
})

// Awaiting the unhandled life-cycle event from the app process takes time.
await vi.waitFor(() => {
// Must print a warning since nobody has handled the request.
expect(console.warn).toHaveBeenCalledWith(`\
[MSW] Warning: intercepted a request without a matching request handler:
• GET http://localhost/unhandled
// Must print a warning since nobody has handled the request.
expect(console.warn).toHaveBeenCalledWith('')
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`)
})
}),
)

it(
'does not warn on the request not handled here but handled there',
'does not warn on the request handled here',
remote.boundary(async () => {
throw new Error('Complete this')
remote.use(
http.get('http://localhost/handled', () => {
return new Response('handled')
}),
)

await using testApp = await spawnTestApp(require.resolve('./use.app.js'))

// Hit a special endpoint that will perform a request to "Location"
// in the application's context. Neither party handles this request.
await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/handled',
},
})

const unhandledWarningPromise = vi.waitFor(() => {
expect(console.warn).toHaveBeenCalledWith(`\
[MSW] Warning: intercepted a request without a matching request handler:
• GET http://localhost/handled
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`)
})

await expect(unhandledWarningPromise).rejects.toThrow()
expect(console.warn).not.toHaveBeenCalled()
}),
)

it(
'does not warn on the request not handled here but handled there',
remote.boundary(async () => {
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))

await fetch(new URL('/resource', testApp.url))

// Must print a warning since nobody has handled the request.
expect(console.warn).toHaveBeenCalledWith('')
const unhandledWarningPromise = vi.waitFor(() => {
expect(console.warn).toHaveBeenCalledWith(`\
[MSW] Warning: intercepted a request without a matching request handler:
• GET https://example.com/resource
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`)
})

await expect(unhandledWarningPromise).rejects.toThrow()
expect(console.warn).not.toHaveBeenCalled()
}),
)

0 comments on commit 0d03db2

Please sign in to comment.