diff --git a/test/node/msw-api/setup-remote-server/on-unhandled-request-default.test.ts b/test/node/msw-api/setup-remote-server/on-unhandled-request-default.test.ts index 96eb940e6..7e15b1fc9 100644 --- a/test/node/msw-api/setup-remote-server/on-unhandled-request-default.test.ts +++ b/test/node/msw-api/setup-remote-server/on-unhandled-request-default.test.ts @@ -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() }) @@ -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() }), )