Skip to content

Commit 9585a8c

Browse files
authored
chore: add comments regarding polyfills for the tests (#30)
And fix the implementation to be closer to the native one.
1 parent ba8fd71 commit 9585a8c

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

test/common/index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ function expectsError (validator, exact) {
103103
}
104104

105105
if (typeof AbortSignal.timeout !== 'function') {
106+
// `AbortSignal.timeout` is not available on Node.js 14.x, we need to polyfill
107+
// it because some tests are using it. End-users don't need to it.
108+
106109
class AbortError extends Error {
107110
constructor (message = 'The operation was aborted', options = undefined) {
108111
super(message, options)
@@ -119,22 +122,22 @@ if (typeof AbortSignal.timeout !== 'function') {
119122
}
120123

121124
if (process.version.startsWith('v14.') || process.version.startsWith('v16.')) {
122-
AbortSignal.abort = () => {
125+
// Implementation of AbortSignal and AbortController differ slightly with the
126+
// v18.x one, creating some difference on the TAP output which makes the tests
127+
// fail. We are overriding the built-ins to make the test pass, however that's
128+
// not necessary to do for the library to work (i.e. end-users don't need it).
129+
130+
const defaultAbortError = new Error('This operation was aborted')
131+
defaultAbortError.code = 20
132+
133+
AbortSignal.abort = function abort (reason = defaultAbortError) {
123134
const controller = new AbortController()
124-
const error = new Error('This operation was aborted')
125-
error.code = 20
126-
controller.abort(error)
135+
controller.abort(reason)
127136
return controller.signal
128137
}
129138
const nativeAbort = AbortController.prototype.abort
130-
AbortController.prototype.abort = function abort (reason) {
131-
if (arguments.length === 0) {
132-
reason = new Error('This operation was aborted')
133-
reason.code = 20
134-
}
135-
if (process.version.startsWith('v14.')) {
136-
this.signal.reason = reason
137-
}
139+
AbortController.prototype.abort = function abort (reason = defaultAbortError) {
140+
this.signal.reason = reason
138141
nativeAbort.call(this, reason)
139142
}
140143
}

0 commit comments

Comments
 (0)