Skip to content

Commit

Permalink
test: add warn/error/bypass onUnhandledRequest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 9, 2025
1 parent 0d03db2 commit 79ae842
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { SetupServer } from './glossary'
export type { SetupServer, ListenOptions } from './glossary'
export { SetupServerApi } from './SetupServerApi'
export { setupServer } from './setupServer'
export {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// @vitest-environment node
import { http } from 'msw'
import { setupRemoteServer } from 'msw/node'
import { spawnTestApp } from './utils'

const remote = setupRemoteServer()

beforeAll(async () => {
vi.spyOn(console, 'error').mockImplementation(() => {})
await remote.listen({
onUnhandledRequest: 'bypass',
})
})

afterEach(() => {
remote.resetHandlers()
})

afterAll(async () => {
await remote.close()
})

it(
'does not error on the request not handled here and there',
remote.boundary(async () => {
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
onUnhandledRequest: 'bypass',
})

await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/unhandled',
},
})

const unhandledErrorPromise = vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: intercepted a request without a matching request handler:
• GET http://localhost/unhandled
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(unhandledErrorPromise).rejects.toThrow()
expect(console.error).not.toHaveBeenCalled()
}),
)

it(
'does not error on the request handled here',
remote.boundary(async () => {
remote.use(
http.get('http://localhost/handled', () => {
return new Response('handled')
}),
)

await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
onUnhandledRequest: 'bypass',
})

await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/handled',
},
})

const unhandledErrorPromise = vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: 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(unhandledErrorPromise).rejects.toThrow()
expect(console.error).not.toHaveBeenCalled()
}),
)

it(
'does not error on the request handled there',
remote.boundary(async () => {
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
onUnhandledRequest: 'bypass',
})

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

const unhandledErrorPromise = vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: 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(unhandledErrorPromise).rejects.toThrow()
expect(console.error).not.toHaveBeenCalled()
}),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// @vitest-environment node
import { http } from 'msw'
import { setupRemoteServer } from 'msw/node'
import { spawnTestApp } from './utils'

const remote = setupRemoteServer()

beforeAll(async () => {
vi.spyOn(console, 'error').mockImplementation(() => {})
await remote.listen({
onUnhandledRequest: 'error',
})
})

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

afterAll(async () => {
vi.restoreAllMocks()
await remote.close()
})

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

await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/unhandled',
},
})

await vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: intercepted a request without a matching request handler:
• GET http://localhost/unhandled
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 error on the request handled here',
remote.boundary(async () => {
remote.use(
http.get('http://localhost/handled', () => {
return new Response('handled')
}),
)

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

await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/handled',
},
})

const unhandledErrorPromise = vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: 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(unhandledErrorPromise).rejects.toThrow()
expect(console.error).not.toHaveBeenCalled()
}),
)

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

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

const unhandledErrorPromise = vi.waitFor(() => {
expect(console.error).toHaveBeenCalledWith(`\
[MSW] Error: 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(unhandledErrorPromise).rejects.toThrow()
expect(console.error).not.toHaveBeenCalled()
}),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// @vitest-environment node
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({
onUnhandledRequest: 'warn',
})
})

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

afterAll(async () => {
vi.restoreAllMocks()
await remote.close()
})

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

await fetch(new URL('/proxy', testApp.url), {
headers: {
location: 'http://localhost/unhandled',
},
})

await vi.waitFor(() => {
expect(console.warn).toHaveBeenCalledWith(`\
[MSW] Warning: intercepted a request without a matching request handler:
• GET http://localhost/unhandled
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 handled here',
remote.boundary(async () => {
remote.use(
http.get('http://localhost/handled', () => {
return new Response('handled')
}),
)

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

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 handled there',
remote.boundary(async () => {
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))

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

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()
}),
)
5 changes: 5 additions & 0 deletions test/node/msw-api/setup-remote-server/use.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const express = require('express')
const { http, HttpResponse } = require('msw')
const { setupServer } = require('msw/node')

const { SETUP_SERVER_LISTEN_OPTIONS } = process.env

// Enable API mocking as usual.
const server = setupServer(
http.get('https://example.com/resource', () => {
Expand All @@ -11,6 +13,9 @@ const server = setupServer(
)

server.listen({
...(SETUP_SERVER_LISTEN_OPTIONS
? JSON.parse(SETUP_SERVER_LISTEN_OPTIONS)
: {}),
remote: {
enabled: true,
},
Expand Down
8 changes: 6 additions & 2 deletions test/node/msw-api/setup-remote-server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { invariant } from 'outvariant'
import { ChildProcess, spawn } from 'child_process'
import { DeferredPromise } from '@open-draft/deferred-promise'
import { getRemoteEnvironment } from 'msw/node'
import { type ListenOptions, getRemoteEnvironment } from 'msw/node'

export async function spawnTestApp(appSourcePath: string) {
export async function spawnTestApp(
appSourcePath: string,
listenOptions: Partial<ListenOptions> = {},
) {
let url: string | undefined
const spawnPromise = new DeferredPromise<string>().then((resolvedUrl) => {
url = resolvedUrl
Expand All @@ -18,6 +21,7 @@ export async function spawnTestApp(appSourcePath: string) {
env: {
...process.env,
...getRemoteEnvironment(),
SETUP_SERVER_LISTEN_OPTIONS: JSON.stringify(listenOptions),
},
})

Expand Down

0 comments on commit 79ae842

Please sign in to comment.