Skip to content

Commit

Permalink
test: remove await for request listener
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jul 6, 2024
1 parent 5184240 commit 0c65c9f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/interceptors/ClientRequest/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect, beforeAll, afterAll } from 'vitest'
import http from 'http'
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import http from 'node:http'
import { HttpServer } from '@open-draft/test-server/http'
import { DeferredPromise } from '@open-draft/deferred-promise'
import { ClientRequestInterceptor } from '.'
Expand All @@ -21,6 +21,10 @@ beforeAll(async () => {
await httpServer.listen()
})

afterEach(() => {
interceptor.removeAllListeners()
})

afterAll(async () => {
interceptor.dispose()
await httpServer.close()
Expand Down Expand Up @@ -60,25 +64,23 @@ it('forbids calling "respondWith" multiple times for the same request', async ()
it('abort the request if the abort signal is emitted', async () => {
const requestUrl = httpServer.http.url('/')

const requestEmitted = new DeferredPromise<void>()
interceptor.on('request', async function delayedResponse({ request }) {
requestEmitted.resolve()
await sleep(10_000)
await sleep(1_000)
request.respondWith(new Response())
})

const abortController = new AbortController()
const request = http.get(requestUrl, { signal: abortController.signal })

await requestEmitted

abortController.abort()

const requestAborted = new DeferredPromise<void>()
request.on('error', function (err) {
expect(err.name).toEqual('AbortError')
requestAborted.resolve()
const abortErrorPromise = new DeferredPromise<Error>()
request.on('error', function (error) {
abortErrorPromise.resolve(error)
})

await requestAborted
const abortError = await abortErrorPromise
expect(abortError.name).toEqual('AbortError')

expect(request.destroyed).toBe(true)
})

0 comments on commit 0c65c9f

Please sign in to comment.