-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(onUnhandledRequest): assert number of warnings/errors
- Loading branch information
1 parent
11bb244
commit 1631ea3
Showing
2 changed files
with
42 additions
and
14 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
54 changes: 40 additions & 14 deletions
54
test/msw-api/setup-server/scenarios/on-unhandled-request/warn.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 |
---|---|---|
@@ -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() | ||
}) | ||
}) |