Skip to content

Commit

Permalink
Uses a test HTTP server in "error" unhandled tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 9, 2021
1 parent 31ca245 commit 99646b9
Showing 1 changed file with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
})

Expand All @@ -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`)
Expand All @@ -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()

Expand All @@ -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()

Expand Down

0 comments on commit 99646b9

Please sign in to comment.