diff --git a/test/msw-api/setup-server/scenarios/on-unhandled-request/error.test.ts b/test/msw-api/setup-server/scenarios/on-unhandled-request/error.test.ts index 46249a83f..7f4bc102d 100644 --- a/test/msw-api/setup-server/scenarios/on-unhandled-request/error.test.ts +++ b/test/msw-api/setup-server/scenarios/on-unhandled-request/error.test.ts @@ -61,6 +61,8 @@ test('errors on unhandled request when using the "error" value', async () => { await expect(() => makeRequest()).rejects.toThrow( `request to ${endpointUrl} failed, reason: Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`, ) + + expect(console.error).toHaveBeenCalledTimes(1) expect(console.error) .toHaveBeenCalledWith(`[MSW] Error: captured a request without a matching request handler: diff --git a/test/msw-api/setup-server/scenarios/on-unhandled-request/warn.test.ts b/test/msw-api/setup-server/scenarios/on-unhandled-request/warn.test.ts index f88f1472a..52e661687 100644 --- a/test/msw-api/setup-server/scenarios/on-unhandled-request/warn.test.ts +++ b/test/msw-api/setup-server/scenarios/on-unhandled-request/warn.test.ts @@ -1,35 +1,61 @@ /** * @jest-environment node */ +import * as http from 'http' import fetch from 'node-fetch' import { setupServer } from 'msw/node' -import { rest } from 'msw' +import { ServerApi, createServer, httpsAgent } from '@open-draft/test-server' -const server = setupServer( - rest.get('https://test.mswjs.io/user', (req, res, ctx) => { - return res(ctx.json({ firstName: 'John' })) - }), -) +const server = setupServer() +let httpServer: ServerApi + +beforeAll(async () => { + httpServer = await createServer((app) => { + app.get('/resource', (req, res) => { + res.status(500).end() + }) + }) -beforeAll(() => { server.listen({ onUnhandledRequest: 'warn' }) jest.spyOn(global.console, 'warn').mockImplementation() }) -afterAll(() => { - server.close() +afterEach(() => { + jest.resetAllMocks() +}) + +afterAll(async () => { jest.restoreAllMocks() + server.close() + await httpServer.close() }) -test('warns on unhandled request when using the "warn" value', async () => { - const res = await fetch('https://test.mswjs.io') +test('warns on unhandled request made via "node-fetch"', async () => { + const url = httpServer.https.makeUrl('/resource') + await fetch(url, { agent: httpsAgent }) - expect(res).toHaveProperty('status', 404) - expect(console.warn).toBeCalledWith(`\ + expect(console.warn).toHaveBeenCalledTimes(1) + expect(console.warn).toHaveBeenCalledWith(`\ [MSW] Warning: captured a request without a matching request handler: - • GET https://test.mswjs.io/ + • GET ${url} 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`) }) + +test('warns on unhandled requests made via "http"', (done) => { + const url = httpServer.http.makeUrl('/resource') + http.get(url, () => { + expect(console.warn).toHaveBeenCalledTimes(1) + expect(console.warn).toHaveBeenCalledWith(`\ +[MSW] Warning: captured a request without a matching request handler: + + • GET ${url} + +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`) + + done() + }) +})