|
| 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 | +) |
0 commit comments