Skip to content

Commit

Permalink
test(onUnhandledRequest): assert number of warnings/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 26, 2021
1 parent 11bb244 commit 1631ea3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 1631ea3

Please sign in to comment.