Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make ttype return boolean
  • Loading branch information
Sosuke Suzuki committed Dec 31, 2023
commit a874a48881c518f3d59b51a626603a445485a0e1
16 changes: 8 additions & 8 deletions test/node-test/abort-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
abortController.abort()

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})

Expand Down Expand Up @@ -74,7 +74,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
})

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})

abortController.abort()
Expand All @@ -99,7 +99,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})

Expand All @@ -123,7 +123,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})

Expand All @@ -150,7 +150,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
abortController.abort()
})
response.body.on('error', err => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})
})
Expand All @@ -175,7 +175,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})
await p.completed
Expand Down Expand Up @@ -205,7 +205,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})
await p.completed
Expand Down Expand Up @@ -238,7 +238,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
abortController.abort()
})
response.body.on('error', err => {
ttype(p, err, errors.RequestAbortedError)
p.ok(ttype(err, errors.RequestAbortedError))
})
})
})
Expand Down
12 changes: 6 additions & 6 deletions test/utils/node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* A port of tap's `t.type` that can be used with `node:assert`
* https://github.com/tapjs/tapjs/blob/511019b2ac0fa014370154c3a341a0e632f50b19/src/asserts/src/index.ts#L199
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the license of that file if copied over.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, 88d2c96

function ttype (plan, obj, klass) {
function ttype (obj, klass) {
const name =
typeof klass === 'function'
? klass.name || '(anonymous constructor)'
: klass

if (obj === klass) {
return plan.ok(1)
return true
}

const tof = typeof obj
Expand All @@ -31,13 +31,13 @@ function ttype (plan, obj, klass) {
(klass === 'array' && Array.isArray(obj)) ||
(type === 'symbol' && klass === Symbol)
) {
return plan.ok(1)
return true
}

// simplest case, it literally is the same thing
if (type === 'object' && klass !== 'object') {
if (typeof klass === 'function') {
return plan.ok(obj instanceof klass)
return obj instanceof klass
}

// check prototype chain for name
Expand All @@ -47,12 +47,12 @@ function ttype (plan, obj, klass) {
for (let p = obj; p; p = Object.getPrototypeOf(p)) {
const ctor = p.constructor && p.constructor.name
if (p === klass || ctor === name) {
return plan.ok(1)
return true
}
}
}

return plan.strictEqual(type, name)
return type === name
}

module.exports = {
Expand Down