-
-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(onUnhandledRequest): convert "next" to "print"; add tests
- Loading branch information
1 parent
6247e45
commit 57f6451
Showing
5 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/msw-api/setup-worker/start/on-unhandled-request/callback-print.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { setupWorker, rest } from 'msw' | ||
|
||
const worker = setupWorker( | ||
rest.get('/user', (req, res, ctx) => { | ||
return res(ctx.json({ firstName: 'John' })) | ||
}), | ||
) | ||
|
||
worker.start({ | ||
onUnhandledRequest(req, print) { | ||
console.log(`Oops, unhandled ${req.method} ${req.url.href}`) | ||
|
||
if (req.url.pathname.includes('/use-warn')) { | ||
// Using "print" allows you to execute the default strategy. | ||
print.warning() | ||
} else { | ||
print.error() | ||
} | ||
}, | ||
}) |
64 changes: 64 additions & 0 deletions
64
test/msw-api/setup-worker/start/on-unhandled-request/callback-print.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
|
||
test('executes a default "warn" strategy in a custom callback', async () => { | ||
const { request, consoleSpy } = await pageWith({ | ||
example: path.resolve(__dirname, 'callback-print.mocks.ts'), | ||
}) | ||
|
||
const res = await request('https://mswjs.io/use-warn') | ||
const status = res.status() | ||
|
||
// Request is performed as-is. | ||
expect(status).toBe(404) | ||
|
||
// Custom callback executed. | ||
expect(consoleSpy.get('log')).toContain( | ||
'Oops, unhandled GET https://mswjs.io/use-warn', | ||
) | ||
expect(consoleSpy.get('error')).toBeUndefined() | ||
|
||
// Prints the unhandled request warning upon `print.warning()`. | ||
expect(consoleSpy.get('warning')).toEqual( | ||
expect.arrayContaining([ | ||
expect.stringContaining(`\ | ||
[MSW] Warning: captured a request without a matching request handler: | ||
• GET https://mswjs.io/use-warn | ||
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('executes a default "error" strategy in a custom callback', async () => { | ||
const { request, consoleSpy } = await pageWith({ | ||
example: path.resolve(__dirname, 'callback-print.mocks.ts'), | ||
}) | ||
|
||
const res = await request('https://mswjs.io/use-error') | ||
const status = res.status() | ||
|
||
// Request is performed as-is. | ||
expect(status).toBe(500) | ||
|
||
// Custom callback executed. | ||
expect(consoleSpy.get('log')).toContain( | ||
'Oops, unhandled GET https://mswjs.io/use-error', | ||
) | ||
expect(consoleSpy.get('warning')).toBeUndefined() | ||
|
||
// Prints the unhandled request error upon `print.error()`. | ||
expect(consoleSpy.get('error')).toEqual( | ||
expect.arrayContaining([ | ||
expect.stringContaining(`\ | ||
[MSW] Error: captured a request without a matching request handler: | ||
• GET https://mswjs.io/use-error | ||
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`), | ||
]), | ||
) | ||
}) |