Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fail individual tests when using "onUnhandledRequest" error strategy #951

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})