-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(setupServer): reapply interception after calling `server.listen()…
…` after `server.close()` (#2383)
- Loading branch information
1 parent
e8d748e
commit 00da9ca
Showing
2 changed files
with
64 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @see https://github.com/mswjs/msw/issues/2370 | ||
*/ | ||
// @vitest-environment node | ||
import { http, HttpResponse } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
|
||
const server = setupServer() | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/resource', (_req, res) => { | ||
res.send('original') | ||
}) | ||
}) | ||
|
||
beforeAll(async () => { | ||
server.listen() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
server.close() | ||
await httpServer.close() | ||
}) | ||
|
||
it('intercepts a request once `server.listen()` is called after `server.close()`', async () => { | ||
const requestUrl = httpServer.http.url('/resource') | ||
|
||
server.use( | ||
http.get(requestUrl, () => { | ||
return HttpResponse.text('mocked') | ||
}), | ||
) | ||
|
||
// Must respond with a mocked response while MSW is active. | ||
{ | ||
const response = await fetch(requestUrl) | ||
await expect(response.text()).resolves.toBe('mocked') | ||
} | ||
|
||
server.close() | ||
|
||
// Must respond with the original response once MSW is closed. | ||
{ | ||
const response = await fetch(requestUrl) | ||
await expect(response.text()).resolves.toBe('original') | ||
} | ||
|
||
server.listen() | ||
|
||
// Must respond with the mocked response once MSW is active again. | ||
{ | ||
const response = await fetch(requestUrl) | ||
await expect(response.text()).resolves.toBe('mocked') | ||
} | ||
}) |