From ec70ba929083b9a93b1be1eda4dd99730e73c885 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Wed, 8 May 2024 17:15:46 +0200 Subject: [PATCH] test: add .use() replacement regression tests (#2145) --- .../regression/2129-worker-use.mocks.ts | 17 ++++++++ .../regression/2129-worker-use.test.ts | 17 ++++++++ test/node/regressions/2129-server-use.test.ts | 41 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 test/browser/msw-api/regression/2129-worker-use.mocks.ts create mode 100644 test/browser/msw-api/regression/2129-worker-use.test.ts create mode 100644 test/node/regressions/2129-server-use.test.ts diff --git a/test/browser/msw-api/regression/2129-worker-use.mocks.ts b/test/browser/msw-api/regression/2129-worker-use.mocks.ts new file mode 100644 index 000000000..201b88516 --- /dev/null +++ b/test/browser/msw-api/regression/2129-worker-use.mocks.ts @@ -0,0 +1,17 @@ +import { http, HttpResponse } from 'msw' +import { setupWorker } from 'msw/browser' + +const worker = setupWorker() +worker.start() + +worker.use( + http.get('/v1/issues', () => { + return HttpResponse.text('get-body') + }), +) + +worker.use( + http.post('/v1/issues', () => { + return HttpResponse.text('post-body') + }), +) diff --git a/test/browser/msw-api/regression/2129-worker-use.test.ts b/test/browser/msw-api/regression/2129-worker-use.test.ts new file mode 100644 index 000000000..e74b167f9 --- /dev/null +++ b/test/browser/msw-api/regression/2129-worker-use.test.ts @@ -0,0 +1,17 @@ +/** + * @see https://github.com/mswjs/msw/issues/2129 + */ +import { test, expect } from '../../playwright.extend' + +test('handles a stream response without throwing a timeout error', async ({ + loadExample, + fetch, +}) => { + await loadExample(require.resolve('./2129-worker-use.mocks.ts')) + + const getResponse = await fetch('/v1/issues') + expect(await getResponse.text()).toBe('get-body') + + const postResponse = await fetch('/v1/issues', { method: 'POST' }) + expect(await postResponse.text()).toBe('post-body') +}) diff --git a/test/node/regressions/2129-server-use.test.ts b/test/node/regressions/2129-server-use.test.ts new file mode 100644 index 000000000..18ffac2aa --- /dev/null +++ b/test/node/regressions/2129-server-use.test.ts @@ -0,0 +1,41 @@ +/** + * @vitest-environment node + * @see https://github.com/mswjs/msw/issues/2129 + */ +import { http, HttpResponse } from 'msw' +import { setupServer } from 'msw/node' + +const server = setupServer() + +beforeAll(async () => { + server.listen() +}) + +afterEach(() => { + server.resetHandlers() +}) + +afterAll(async () => { + server.close() +}) + +it('does not override existing handlers when adding override for a different method', async () => { + server.use( + http.get('http://localhost/v1/issues', () => { + return HttpResponse.text('get-body') + }), + ) + server.use( + http.post('http://localhost/v1/issues', () => { + return HttpResponse.text('post-body') + }), + ) + + const geetResponse = await fetch('http://localhost/v1/issues') + expect(await geetResponse.text()).toBe('get-body') + + const postResponse = await fetch('http://localhost/v1/issues', { + method: 'POST', + }) + expect(await postResponse.text()).toBe('post-body') +})