-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fetch): await response listener before resolving the response pro…
…mise (#658) Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
343cc5a
commit 261f581
Showing
3 changed files
with
95 additions
and
13 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
68 changes: 68 additions & 0 deletions
68
test/modules/fetch/response/fetch-await-response-event.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,68 @@ | ||
// @vitest-environment node | ||
import { vi, it, expect, beforeAll, afterAll, afterEach } from 'vitest' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { FetchInterceptor } from '../../../../src/interceptors/fetch' | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.get('/resource', (req, res) => { | ||
res.send('original response') | ||
}) | ||
}) | ||
|
||
const interceptor = new FetchInterceptor() | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('awaits response listener promise before resolving the mocked response promise', async () => { | ||
const markStep = vi.fn<[number]>() | ||
|
||
interceptor.on('request', ({ controller }) => { | ||
controller.respondWith(new Response('hello world')) | ||
}) | ||
|
||
interceptor.on('response', async ({ response }) => { | ||
markStep(2) | ||
await response.text() | ||
markStep(3) | ||
}) | ||
|
||
markStep(1) | ||
await fetch('http://localhost/') | ||
markStep(4) | ||
|
||
expect(markStep).toHaveBeenNthCalledWith(1, 1) | ||
expect(markStep).toHaveBeenNthCalledWith(2, 2) | ||
expect(markStep).toHaveBeenNthCalledWith(3, 3) | ||
expect(markStep).toHaveBeenNthCalledWith(4, 4) | ||
}) | ||
|
||
it('awaits response listener promise before resolving the original response promise', async () => { | ||
const markStep = vi.fn<[number]>() | ||
|
||
interceptor.on('response', async ({ response }) => { | ||
markStep(2) | ||
await response.text() | ||
markStep(3) | ||
}) | ||
|
||
markStep(1) | ||
await fetch(httpServer.http.url('/resource')) | ||
markStep(4) | ||
|
||
expect(markStep).toHaveBeenNthCalledWith(1, 1) | ||
expect(markStep).toHaveBeenNthCalledWith(2, 2) | ||
expect(markStep).toHaveBeenNthCalledWith(3, 3) | ||
expect(markStep).toHaveBeenNthCalledWith(4, 4) | ||
}) |
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