Skip to content

Commit

Permalink
onUnhandledRequest: Adds request handler declaration suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 17, 2020
1 parent d6f0cd0 commit de0f583
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
13 changes: 12 additions & 1 deletion src/onUnhandledRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MockedRequest } from './handlers/requestHandler'
import { getPublicUrlFromRequest } from './utils/getPublicUrlFromRequest'

type UnhandledRequestCallback = (req: MockedRequest) => void

Expand All @@ -17,7 +18,17 @@ export function onUnhandledRequest(
return
}

const message = `captured a ${request.method} ${request.url} request without a corresponding request handler.`
const publicUrl = getPublicUrlFromRequest(request)

const message = `captured a ${request.method} ${
request.url
} request without a corresponding request handler.
If you wish to intercept this request, consider creating a request handler for it:
rest.${request.method.toLowerCase()}('${publicUrl}', (req, res, ctx) => {
return res(ctx.text('body'))
})`

switch (onUnhandledRequest) {
case 'error': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ const server = setupServer(
}),
)

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
afterAll(() => server.close())
beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' })
})

afterAll(() => {
server.close()
})

test('errors on unhandled request when using the "error" value', async () => {
const getResponse = () => fetch('https://test.mswjs.io')

await expect(getResponse()).rejects.toThrow(
'request to https://test.mswjs.io/ failed, reason: [MSW] Error: captured a GET https://test.mswjs.io/ request without a corresponding request handler.',
)
await expect(getResponse()).rejects.toThrow(`\
request to https://test.mswjs.io/ failed, reason: [MSW] Error: captured a GET https://test.mswjs.io/ request without a corresponding request handler.
If you wish to intercept this request, consider creating a request handler for it:
rest.get('https://test.mswjs.io/', (req, res, ctx) => {
return res(ctx.text('body'))
})`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const server = setupServer(
}),
)

beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }))
beforeAll(() => {
server.listen({ onUnhandledRequest: 'warn' })
})

afterAll(() => {
server.close()
jest.restoreAllMocks()
Expand All @@ -23,7 +26,12 @@ test('warns on unhandled request when using the "warn" value', async () => {
const res = await fetch('https://test.mswjs.io')

expect(res).toHaveProperty('status', 404)
expect(console.warn).toBeCalledWith(
'[MSW] Warning: captured a GET https://test.mswjs.io/ request without a corresponding request handler.',
)
expect(console.warn).toBeCalledWith(`\
[MSW] Warning: captured a GET https://test.mswjs.io/ request without a corresponding request handler.
If you wish to intercept this request, consider creating a request handler for it:
rest.get('https://test.mswjs.io/', (req, res, ctx) => {
return res(ctx.text('body'))
})`)
})
46 changes: 41 additions & 5 deletions test/msw-api/setup-worker/start/on-unhandled-request/warn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {

let runtime: TestAPI

beforeAll(async () => {
beforeEach(async () => {
runtime = await runBrowserWith(path.resolve(__dirname, 'warn.mocks.ts'))
})

afterEach(async () => {
await runtime.cleanup()
})

test('warns on an unhandled request when using the "warn" option', async () => {
test('warns on an absolute unhandled request', async () => {
const { messages } = captureConsole(runtime.page)

const res = await runtime.request({
Expand All @@ -29,7 +29,43 @@ test('warns on an unhandled request when using the "warn" option', async () => {
const libraryWarnings = messages.warning.filter(filterLibraryLogs)

expect(libraryErrors).toHaveLength(0)
expect(libraryWarnings).toContain(
'[MSW] Warning: captured a GET https://mswjs.io/non-existing-page request without a corresponding request handler.',
)
expect(libraryWarnings).toContain(`\
[MSW] Warning: captured a GET https://mswjs.io/non-existing-page request without a corresponding request handler.
If you wish to intercept this request, consider creating a request handler for it:
rest.get('https://mswjs.io/non-existing-page', (req, res, ctx) => {
return res(ctx.text('body'))
})`)
})

test('warns on a relative unhandled request', async () => {
const { messages } = captureConsole(runtime.page)

const url = `${runtime.origin}/user-details`
const res = await runtime.request({
url,
})
const status = res.status()

expect(status).toBe(404)

const libraryErrors = messages.error.filter(filterLibraryLogs)
const libraryWarnings = messages.warning.filter(filterLibraryLogs)
const requestHandlerWarning = libraryWarnings.find((text) => {
return /\[MSW\] Warning: captured a GET .+? request without a corresponding request handler/.test(
text,
)
})

expect(libraryErrors).toHaveLength(0)
expect(requestHandlerWarning).toBeTruthy()
expect(requestHandlerWarning).toMatch(`\
[MSW] Warning: captured a GET ${url} request without a corresponding request handler.
If you wish to intercept this request, consider creating a request handler for it:
rest.get('/user-details', (req, res, ctx) => {
return res(ctx.text('body'))
})`)
})

0 comments on commit de0f583

Please sign in to comment.