Skip to content

Commit

Permalink
test: add .use() replacement regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed May 8, 2024
1 parent 7cf34c1 commit a9f0924
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 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')
}),
)
17 changes: 17 additions & 0 deletions 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')
})
41 changes: 41 additions & 0 deletions 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')
})

0 comments on commit a9f0924

Please sign in to comment.