[fetch-utils] Tests updates (#20280)

* [fetch-utils] Check when `aborted` is thrown

* [fetch-utils] Rename `AbortError` to `abortErrorMessage`

* [fetch-utils] Don't test `caughtAbortedErr`

Partially reverts 87604105c4fd66e548d917d6d93f1603977689be

GitOrigin-RevId: fd91f952793d6727e3efcbb58867c95642d04983
This commit is contained in:
Antoine Clausse 2024-09-05 13:52:45 +02:00 committed by Copybot
parent abc1aff3a1
commit 9bc1085566

View file

@ -15,7 +15,7 @@ const {
CustomHttpsAgent, CustomHttpsAgent,
} = require('../..') } = require('../..')
const AbortError = 'The user aborted a request' const abortErrorMessage = 'The user aborted a request'
const HTTP_PORT = 30001 const HTTP_PORT = 30001
const HTTPS_PORT = 30002 const HTTPS_PORT = 30002
@ -107,7 +107,7 @@ describe('fetch-utils', function () {
it('supports abort signals', async function () { it('supports abort signals', async function () {
await expect( await expect(
fetchJson(this.url('/hang'), { signal: AbortSignal.timeout(10) }) fetchJson(this.url('/hang'), { signal: AbortSignal.timeout(10) })
).to.be.rejectedWith(AbortError) ).to.be.rejectedWith(abortErrorMessage)
await expectRequestAborted(this.server.lastReq) await expectRequestAborted(this.server.lastReq)
}) })
@ -148,7 +148,7 @@ describe('fetch-utils', function () {
body: stream, body: stream,
}) })
stream.destroy() stream.destroy()
await expect(promise).to.be.rejectedWith(AbortError) await expect(promise).to.be.rejectedWith(abortErrorMessage)
await wait(80) await wait(80)
expect(this.server.lastReq).to.be.undefined expect(this.server.lastReq).to.be.undefined
}) })
@ -162,7 +162,7 @@ describe('fetch-utils', function () {
}) })
await once(this.server.events, 'request-received') await once(this.server.events, 'request-received')
stream.destroy() stream.destroy()
await expect(promise).to.be.rejectedWith(AbortError) await expect(promise).to.be.rejectedWith(abortErrorMessage)
await expectRequestAborted(this.server.lastReq) await expectRequestAborted(this.server.lastReq)
}) })
@ -176,7 +176,7 @@ describe('fetch-utils', function () {
it('supports abort signals', async function () { it('supports abort signals', async function () {
await expect( await expect(
fetchStream(this.url('/hang'), { signal: AbortSignal.timeout(10) }) fetchStream(this.url('/hang'), { signal: AbortSignal.timeout(10) })
).to.be.rejectedWith(AbortError) ).to.be.rejectedWith(abortErrorMessage)
await expectRequestAborted(this.server.lastReq) await expectRequestAborted(this.server.lastReq)
}) })
@ -188,7 +188,7 @@ describe('fetch-utils', function () {
body: stream, body: stream,
signal: AbortSignal.timeout(10), signal: AbortSignal.timeout(10),
}) })
).to.be.rejectedWith(AbortError) ).to.be.rejectedWith(abortErrorMessage)
expect(stream.destroyed).to.be.true expect(stream.destroyed).to.be.true
}) })
}) })
@ -206,7 +206,7 @@ describe('fetch-utils', function () {
body: stream, body: stream,
}) })
stream.destroy() stream.destroy()
await expect(promise).to.be.rejectedWith(AbortError) await expect(promise).to.be.rejectedWith(abortErrorMessage)
expect(this.server.lastReq).to.be.undefined expect(this.server.lastReq).to.be.undefined
}) })
@ -219,7 +219,7 @@ describe('fetch-utils', function () {
}) })
await once(this.server.events, 'request-received') await once(this.server.events, 'request-received')
stream.destroy() stream.destroy()
await expect(promise).to.be.rejectedWith(AbortError) await expect(promise).to.be.rejectedWith(abortErrorMessage)
await wait(80) await wait(80)
await expectRequestAborted(this.server.lastReq) await expectRequestAborted(this.server.lastReq)
}) })
@ -239,7 +239,7 @@ describe('fetch-utils', function () {
it('supports abort signals', async function () { it('supports abort signals', async function () {
await expect( await expect(
fetchNothing(this.url('/hang'), { signal: AbortSignal.timeout(10) }) fetchNothing(this.url('/hang'), { signal: AbortSignal.timeout(10) })
).to.be.rejectedWith(AbortError) ).to.be.rejectedWith(abortErrorMessage)
await expectRequestAborted(this.server.lastReq) await expectRequestAborted(this.server.lastReq)
}) })
@ -251,7 +251,7 @@ describe('fetch-utils', function () {
body: stream, body: stream,
signal: AbortSignal.timeout(10), signal: AbortSignal.timeout(10),
}) })
).to.be.rejectedWith(AbortError) ).to.be.rejectedWith(abortErrorMessage)
expect(stream.destroyed).to.be.true expect(stream.destroyed).to.be.true
}) })
}) })
@ -370,8 +370,11 @@ async function expectRequestAborted(req) {
try { try {
await once(req, 'close') await once(req, 'close')
} catch (err) { } catch (err) {
// `once` throws if req emits an 'error' event // `once` throws if req emits an 'error' event.
// For example, with `Error: aborted` when the request is aborted. // We ignore `Error: aborted` when the request is aborted.
if (err.message !== 'aborted') {
throw err
}
} }
expect(req.destroyed).to.be.true expect(req.destroyed).to.be.true
} }