-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add warn/error/bypass
onUnhandledRequest
tests
- Loading branch information
1 parent
0d03db2
commit 79ae842
Showing
6 changed files
with
324 additions
and
3 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
107 changes: 107 additions & 0 deletions
107
test/node/msw-api/setup-remote-server/on-unhandled-request-bypass.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,107 @@ | ||
// @vitest-environment node | ||
import { http } from 'msw' | ||
import { setupRemoteServer } from 'msw/node' | ||
import { spawnTestApp } from './utils' | ||
|
||
const remote = setupRemoteServer() | ||
|
||
beforeAll(async () => { | ||
vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
await remote.listen({ | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
remote.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
await remote.close() | ||
}) | ||
|
||
it( | ||
'does not error on the request not handled here and there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), { | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/unhandled', | ||
}, | ||
}) | ||
|
||
const unhandledErrorPromise = vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET http://localhost/unhandled | ||
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`) | ||
}) | ||
|
||
await expect(unhandledErrorPromise).rejects.toThrow() | ||
expect(console.error).not.toHaveBeenCalled() | ||
}), | ||
) | ||
|
||
it( | ||
'does not error on the request handled here', | ||
remote.boundary(async () => { | ||
remote.use( | ||
http.get('http://localhost/handled', () => { | ||
return new Response('handled') | ||
}), | ||
) | ||
|
||
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), { | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/handled', | ||
}, | ||
}) | ||
|
||
const unhandledErrorPromise = vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET http://localhost/handled | ||
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`) | ||
}) | ||
|
||
await expect(unhandledErrorPromise).rejects.toThrow() | ||
expect(console.error).not.toHaveBeenCalled() | ||
}), | ||
) | ||
|
||
it( | ||
'does not error on the request handled there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js'), { | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
|
||
await fetch(new URL('/resource', testApp.url)) | ||
|
||
const unhandledErrorPromise = vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET https://example.com/resource | ||
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`) | ||
}) | ||
|
||
await expect(unhandledErrorPromise).rejects.toThrow() | ||
expect(console.error).not.toHaveBeenCalled() | ||
}), | ||
) |
100 changes: 100 additions & 0 deletions
100
test/node/msw-api/setup-remote-server/on-unhandled-request-error.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,100 @@ | ||
// @vitest-environment node | ||
import { http } from 'msw' | ||
import { setupRemoteServer } from 'msw/node' | ||
import { spawnTestApp } from './utils' | ||
|
||
const remote = setupRemoteServer() | ||
|
||
beforeAll(async () => { | ||
vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
await remote.listen({ | ||
onUnhandledRequest: 'error', | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
remote.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
vi.restoreAllMocks() | ||
await remote.close() | ||
}) | ||
|
||
it( | ||
'errors on the request not handled here and there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/unhandled', | ||
}, | ||
}) | ||
|
||
await vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET http://localhost/unhandled | ||
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`) | ||
}) | ||
}), | ||
) | ||
|
||
it( | ||
'does not error on the request handled here', | ||
remote.boundary(async () => { | ||
remote.use( | ||
http.get('http://localhost/handled', () => { | ||
return new Response('handled') | ||
}), | ||
) | ||
|
||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/handled', | ||
}, | ||
}) | ||
|
||
const unhandledErrorPromise = vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET http://localhost/handled | ||
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`) | ||
}) | ||
|
||
await expect(unhandledErrorPromise).rejects.toThrow() | ||
expect(console.error).not.toHaveBeenCalled() | ||
}), | ||
) | ||
|
||
it( | ||
'does not error on the request handled there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/resource', testApp.url)) | ||
|
||
const unhandledErrorPromise = vi.waitFor(() => { | ||
expect(console.error).toHaveBeenCalledWith(`\ | ||
[MSW] Error: intercepted a request without a matching request handler: | ||
• GET https://example.com/resource | ||
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`) | ||
}) | ||
|
||
await expect(unhandledErrorPromise).rejects.toThrow() | ||
expect(console.error).not.toHaveBeenCalled() | ||
}), | ||
) |
105 changes: 105 additions & 0 deletions
105
test/node/msw-api/setup-remote-server/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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// @vitest-environment node | ||
import { http } from 'msw' | ||
import { setupRemoteServer } from 'msw/node' | ||
import { spawnTestApp } from './utils' | ||
|
||
const remote = setupRemoteServer() | ||
|
||
beforeAll(async () => { | ||
/** | ||
* @note Console warnings from the app's context are forwarded | ||
* as `console.error`. Ignore those for this test. | ||
*/ | ||
vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
await remote.listen({ | ||
onUnhandledRequest: 'warn', | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
remote.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
vi.restoreAllMocks() | ||
await remote.close() | ||
}) | ||
|
||
it( | ||
'warns on the request not handled here and there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/unhandled', | ||
}, | ||
}) | ||
|
||
await vi.waitFor(() => { | ||
expect(console.warn).toHaveBeenCalledWith(`\ | ||
[MSW] Warning: intercepted a request without a matching request handler: | ||
• GET http://localhost/unhandled | ||
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`) | ||
}) | ||
}), | ||
) | ||
|
||
it( | ||
'does not warn on the request handled here', | ||
remote.boundary(async () => { | ||
remote.use( | ||
http.get('http://localhost/handled', () => { | ||
return new Response('handled') | ||
}), | ||
) | ||
|
||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/proxy', testApp.url), { | ||
headers: { | ||
location: 'http://localhost/handled', | ||
}, | ||
}) | ||
|
||
const unhandledWarningPromise = vi.waitFor(() => { | ||
expect(console.warn).toHaveBeenCalledWith(`\ | ||
[MSW] Warning: intercepted a request without a matching request handler: | ||
• GET http://localhost/handled | ||
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`) | ||
}) | ||
|
||
await expect(unhandledWarningPromise).rejects.toThrow() | ||
expect(console.warn).not.toHaveBeenCalled() | ||
}), | ||
) | ||
|
||
it( | ||
'does not warn on the request handled there', | ||
remote.boundary(async () => { | ||
await using testApp = await spawnTestApp(require.resolve('./use.app.js')) | ||
|
||
await fetch(new URL('/resource', testApp.url)) | ||
|
||
const unhandledWarningPromise = vi.waitFor(() => { | ||
expect(console.warn).toHaveBeenCalledWith(`\ | ||
[MSW] Warning: intercepted a request without a matching request handler: | ||
• GET https://example.com/resource | ||
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`) | ||
}) | ||
|
||
await expect(unhandledWarningPromise).rejects.toThrow() | ||
expect(console.warn).not.toHaveBeenCalled() | ||
}), | ||
) |
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