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 3b792df9d..46249a83f 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 @@ -2,25 +2,40 @@ * @jest-environment node */ import fetch from 'node-fetch' -import { setupServer } from 'msw/node' +import { createServer, ServerApi } from '@open-draft/test-server' import { rest } from 'msw' +import { setupServer } from 'msw/node' -const server = setupServer( - rest.get('https://test.mswjs.io/user', (req, res, ctx) => { - return res(ctx.json({ mocked: true })) - }), - rest.post('https://test.mswjs.io/explicit-return', () => { - // Short-circuiting in a handler makes it perform the request as-is, - // but still treats this request as handled. - return - }), - rest.post('https://test.mswjs.io/implicit-return', () => { - // The handler that has no return also performs the request as-is, - // still treating this request as handled. - }), -) +let httpServer: ServerApi +const server = setupServer() -beforeAll(() => { +beforeAll(async () => { + httpServer = await createServer((app) => { + app.get('/user', (req, res) => { + res.status(200).json({ original: true }) + }) + app.post('/explicit-return', (req, res) => { + res.status(500).end() + }) + app.post('/implicit-return', (req, res) => { + res.status(500).end() + }) + }) + + server.use( + rest.get(httpServer.http.makeUrl('/user'), (req, res, ctx) => { + return res(ctx.json({ mocked: true })) + }), + rest.post(httpServer.http.makeUrl('/explicit-return'), () => { + // Short-circuiting in a handler makes it perform the request as-is, + // but still treats this request as handled. + return + }), + rest.post(httpServer.http.makeUrl('/implicit-return'), () => { + // The handler that has no return also performs the request as-is, + // still treating this request as handled. + }), + ) server.listen({ onUnhandledRequest: 'error' }) }) @@ -33,21 +48,23 @@ afterEach(() => { jest.resetAllMocks() }) -afterAll(() => { +afterAll(async () => { jest.restoreAllMocks() server.close() + await httpServer.close() }) test('errors on unhandled request when using the "error" value', async () => { - const makeRequest = () => fetch('https://test.mswjs.io') + const endpointUrl = httpServer.http.makeUrl('/') + const makeRequest = () => fetch(endpointUrl) await expect(() => makeRequest()).rejects.toThrow( - 'request to https://test.mswjs.io/ failed, reason: Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.', + `request to ${endpointUrl} failed, reason: Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`, ) expect(console.error) .toHaveBeenCalledWith(`[MSW] Error: captured a request without a matching request handler: - • GET https://test.mswjs.io/ + • GET ${endpointUrl} 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`) @@ -56,7 +73,9 @@ Read more: https://mswjs.io/docs/getting-started/mocks`) test('does not error on request which handler explicitly returns no mocked response', async () => { const makeRequest = () => { - return fetch('https://test.mswjs.io/explicit-return', { method: 'POST' }) + return fetch(httpServer.http.makeUrl('/explicit-return'), { + method: 'POST', + }) } await makeRequest() @@ -65,7 +84,9 @@ test('does not error on request which handler explicitly returns no mocked respo test('does not error on request which handler implicitly returns no mocked response', async () => { const makeRequest = () => { - return fetch('https://test.mswjs.io/implicit-return', { method: 'POST' }) + return fetch(httpServer.http.makeUrl('/implicit-return'), { + method: 'POST', + }) } await makeRequest()