Skip to content

Commit e3f4c44

Browse files
committed
test: add warn/error/bypass onUnhandledRequest tests
1 parent b22d00c commit e3f4c44

File tree

5 files changed

+323
-2
lines changed

5 files changed

+323
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// @vitest-environment node
2+
import { http } from 'msw'
3+
import { setupRemoteServer } from 'msw/node'
4+
import { spawnTestApp } from './utils'
5+
6+
const remote = setupRemoteServer()
7+
8+
beforeAll(async () => {
9+
vi.spyOn(console, 'error').mockImplementation(() => {})
10+
await remote.listen({
11+
onUnhandledRequest: 'bypass',
12+
})
13+
})
14+
15+
afterEach(() => {
16+
remote.resetHandlers()
17+
})
18+
19+
afterAll(async () => {
20+
await remote.close()
21+
})
22+
23+
it(
24+
'does not error on the request not handled here and there',
25+
remote.boundary(async () => {
26+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
27+
onUnhandledRequest: 'bypass',
28+
})
29+
30+
await fetch(new URL('/proxy', testApp.url), {
31+
headers: {
32+
location: 'http://localhost/unhandled',
33+
},
34+
})
35+
36+
const unhandledErrorPromise = vi.waitFor(() => {
37+
expect(console.error).toHaveBeenCalledWith(`\
38+
[MSW] Error: intercepted a request without a matching request handler:
39+
40+
• GET http://localhost/unhandled
41+
42+
If you still wish to intercept this unhandled request, please create a request handler for it.
43+
Read more: https://mswjs.io/docs/getting-started/mocks`)
44+
})
45+
46+
await expect(unhandledErrorPromise).rejects.toThrow()
47+
expect(console.error).not.toHaveBeenCalled()
48+
}),
49+
)
50+
51+
it(
52+
'does not error on the request handled here',
53+
remote.boundary(async () => {
54+
remote.use(
55+
http.get('http://localhost/handled', () => {
56+
return new Response('handled')
57+
}),
58+
)
59+
60+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
61+
onUnhandledRequest: 'bypass',
62+
})
63+
64+
await fetch(new URL('/proxy', testApp.url), {
65+
headers: {
66+
location: 'http://localhost/handled',
67+
},
68+
})
69+
70+
const unhandledErrorPromise = vi.waitFor(() => {
71+
expect(console.error).toHaveBeenCalledWith(`\
72+
[MSW] Error: intercepted a request without a matching request handler:
73+
74+
• GET http://localhost/handled
75+
76+
If you still wish to intercept this unhandled request, please create a request handler for it.
77+
Read more: https://mswjs.io/docs/getting-started/mocks`)
78+
})
79+
80+
await expect(unhandledErrorPromise).rejects.toThrow()
81+
expect(console.error).not.toHaveBeenCalled()
82+
}),
83+
)
84+
85+
it(
86+
'does not error on the request handled there',
87+
remote.boundary(async () => {
88+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), {
89+
onUnhandledRequest: 'bypass',
90+
})
91+
92+
await fetch(new URL('/resource', testApp.url))
93+
94+
const unhandledErrorPromise = vi.waitFor(() => {
95+
expect(console.error).toHaveBeenCalledWith(`\
96+
[MSW] Error: intercepted a request without a matching request handler:
97+
98+
• GET https://example.com/resource
99+
100+
If you still wish to intercept this unhandled request, please create a request handler for it.
101+
Read more: https://mswjs.io/docs/getting-started/mocks`)
102+
})
103+
104+
await expect(unhandledErrorPromise).rejects.toThrow()
105+
expect(console.error).not.toHaveBeenCalled()
106+
}),
107+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// @vitest-environment node
2+
import { http } from 'msw'
3+
import { setupRemoteServer } from 'msw/node'
4+
import { spawnTestApp } from './utils'
5+
6+
const remote = setupRemoteServer()
7+
8+
beforeAll(async () => {
9+
vi.spyOn(console, 'error').mockImplementation(() => {})
10+
await remote.listen({
11+
onUnhandledRequest: 'error',
12+
})
13+
})
14+
15+
afterEach(() => {
16+
vi.clearAllMocks()
17+
remote.resetHandlers()
18+
})
19+
20+
afterAll(async () => {
21+
vi.restoreAllMocks()
22+
await remote.close()
23+
})
24+
25+
it(
26+
'errors on the request not handled here and there',
27+
remote.boundary(async () => {
28+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
29+
30+
await fetch(new URL('/proxy', testApp.url), {
31+
headers: {
32+
location: 'http://localhost/unhandled',
33+
},
34+
})
35+
36+
await vi.waitFor(() => {
37+
expect(console.error).toHaveBeenCalledWith(`\
38+
[MSW] Error: intercepted a request without a matching request handler:
39+
40+
• GET http://localhost/unhandled
41+
42+
If you still wish to intercept this unhandled request, please create a request handler for it.
43+
Read more: https://mswjs.io/docs/getting-started/mocks`)
44+
})
45+
}),
46+
)
47+
48+
it(
49+
'does not error on the request handled here',
50+
remote.boundary(async () => {
51+
remote.use(
52+
http.get('http://localhost/handled', () => {
53+
return new Response('handled')
54+
}),
55+
)
56+
57+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
58+
59+
await fetch(new URL('/proxy', testApp.url), {
60+
headers: {
61+
location: 'http://localhost/handled',
62+
},
63+
})
64+
65+
const unhandledErrorPromise = vi.waitFor(() => {
66+
expect(console.error).toHaveBeenCalledWith(`\
67+
[MSW] Error: intercepted a request without a matching request handler:
68+
69+
• GET http://localhost/handled
70+
71+
If you still wish to intercept this unhandled request, please create a request handler for it.
72+
Read more: https://mswjs.io/docs/getting-started/mocks`)
73+
})
74+
75+
await expect(unhandledErrorPromise).rejects.toThrow()
76+
expect(console.error).not.toHaveBeenCalled()
77+
}),
78+
)
79+
80+
it(
81+
'does not error on the request handled there',
82+
remote.boundary(async () => {
83+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
84+
85+
await fetch(new URL('/resource', testApp.url))
86+
87+
const unhandledErrorPromise = vi.waitFor(() => {
88+
expect(console.error).toHaveBeenCalledWith(`\
89+
[MSW] Error: intercepted a request without a matching request handler:
90+
91+
• GET https://example.com/resource
92+
93+
If you still wish to intercept this unhandled request, please create a request handler for it.
94+
Read more: https://mswjs.io/docs/getting-started/mocks`)
95+
})
96+
97+
await expect(unhandledErrorPromise).rejects.toThrow()
98+
expect(console.error).not.toHaveBeenCalled()
99+
}),
100+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// @vitest-environment node
2+
import { http } from 'msw'
3+
import { setupRemoteServer } from 'msw/node'
4+
import { spawnTestApp } from './utils'
5+
6+
const remote = setupRemoteServer()
7+
8+
beforeAll(async () => {
9+
/**
10+
* @note Console warnings from the app's context are forwarded
11+
* as `console.error`. Ignore those for this test.
12+
*/
13+
vi.spyOn(console, 'error').mockImplementation(() => {})
14+
vi.spyOn(console, 'warn').mockImplementation(() => {})
15+
await remote.listen({
16+
onUnhandledRequest: 'warn',
17+
})
18+
})
19+
20+
afterEach(() => {
21+
vi.clearAllMocks()
22+
remote.resetHandlers()
23+
})
24+
25+
afterAll(async () => {
26+
vi.restoreAllMocks()
27+
await remote.close()
28+
})
29+
30+
it(
31+
'warns on the request not handled here and there',
32+
remote.boundary(async () => {
33+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
34+
35+
await fetch(new URL('/proxy', testApp.url), {
36+
headers: {
37+
location: 'http://localhost/unhandled',
38+
},
39+
})
40+
41+
await vi.waitFor(() => {
42+
expect(console.warn).toHaveBeenCalledWith(`\
43+
[MSW] Warning: intercepted a request without a matching request handler:
44+
45+
• GET http://localhost/unhandled
46+
47+
If you still wish to intercept this unhandled request, please create a request handler for it.
48+
Read more: https://mswjs.io/docs/getting-started/mocks`)
49+
})
50+
}),
51+
)
52+
53+
it(
54+
'does not warn on the request handled here',
55+
remote.boundary(async () => {
56+
remote.use(
57+
http.get('http://localhost/handled', () => {
58+
return new Response('handled')
59+
}),
60+
)
61+
62+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
63+
64+
await fetch(new URL('/proxy', testApp.url), {
65+
headers: {
66+
location: 'http://localhost/handled',
67+
},
68+
})
69+
70+
const unhandledWarningPromise = vi.waitFor(() => {
71+
expect(console.warn).toHaveBeenCalledWith(`\
72+
[MSW] Warning: intercepted a request without a matching request handler:
73+
74+
• GET http://localhost/handled
75+
76+
If you still wish to intercept this unhandled request, please create a request handler for it.
77+
Read more: https://mswjs.io/docs/getting-started/mocks`)
78+
})
79+
80+
await expect(unhandledWarningPromise).rejects.toThrow()
81+
expect(console.warn).not.toHaveBeenCalled()
82+
}),
83+
)
84+
85+
it(
86+
'does not warn on the request handled there',
87+
remote.boundary(async () => {
88+
await using testApp = await spawnTestApp(require.resolve('./use.app.js'))
89+
90+
await fetch(new URL('/resource', testApp.url))
91+
92+
const unhandledWarningPromise = vi.waitFor(() => {
93+
expect(console.warn).toHaveBeenCalledWith(`\
94+
[MSW] Warning: intercepted a request without a matching request handler:
95+
96+
• GET https://example.com/resource
97+
98+
If you still wish to intercept this unhandled request, please create a request handler for it.
99+
Read more: https://mswjs.io/docs/getting-started/mocks`)
100+
})
101+
102+
await expect(unhandledWarningPromise).rejects.toThrow()
103+
expect(console.warn).not.toHaveBeenCalled()
104+
}),
105+
)

test/node/msw-api/setup-remote-server/use.app.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const express = require('express')
33
const { http, HttpResponse } = require('msw')
44
const { setupServer } = require('msw/node')
55

6+
const { SETUP_SERVER_LISTEN_OPTIONS } = process.env
7+
68
// Enable API mocking as usual.
79
const server = setupServer(
810
http.get('https://example.com/resource', () => {
@@ -11,6 +13,9 @@ const server = setupServer(
1113
)
1214

1315
server.listen({
16+
...(SETUP_SERVER_LISTEN_OPTIONS
17+
? JSON.parse(SETUP_SERVER_LISTEN_OPTIONS)
18+
: {}),
1419
remote: {
1520
enabled: true,
1621
},

test/node/msw-api/setup-remote-server/utils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { invariant } from 'outvariant'
22
import { ChildProcess, spawn } from 'child_process'
33
import { DeferredPromise } from '@open-draft/deferred-promise'
4-
import { getRemoteEnvironment } from 'msw/node'
4+
import { type ListenOptions, getRemoteEnvironment } from 'msw/node'
55

6-
export async function spawnTestApp(appSourcePath: string) {
6+
export async function spawnTestApp(
7+
appSourcePath: string,
8+
listenOptions: Partial<ListenOptions> = {},
9+
) {
710
let url: string | undefined
811
const spawnPromise = new DeferredPromise<string>().then((resolvedUrl) => {
912
url = resolvedUrl
@@ -18,6 +21,7 @@ export async function spawnTestApp(appSourcePath: string) {
1821
env: {
1922
...process.env,
2023
...getRemoteEnvironment(),
24+
SETUP_SERVER_LISTEN_OPTIONS: JSON.stringify(listenOptions),
2125
},
2226
})
2327

0 commit comments

Comments
 (0)