Skip to content
Merged
Changes from all commits
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
34 changes: 34 additions & 0 deletions test/parallel/test-util-callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,37 @@ const values = [
})
);
}

{
// Verify that non-function inputs throw.
['foo', null, undefined, false, 0, {}, Symbol(), []].forEach((value) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny nit: could use values.filter(v => typeof v !== 'function')

assert.throws(() => {
callbackify(value);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "original" argument must be of type function'
}));
});
}

{
async function asyncFn() {
return await Promise.resolve(42);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: the return itself is already sufficient as the return value is implicitly wrapped in a promise. Therefore the await can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, but that's the way to other tests are written (e.g. https://github.com/nodejs/node/blob/master/test/parallel/test-util-callbackify.js#L31)
The rationale was to make them actually async in timing, not just in signature.

}

const cb = callbackify(asyncFn);
const args = [];

// Verify that the last argument to the callbackified function is a function.
['foo', null, undefined, false, 0, {}, Symbol(), []].forEach((value) => {
args.push(value);
assert.throws(() => {
cb(...args);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "last argument" argument must be of type function'
Copy link
Contributor

Choose a reason for hiding this comment

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

this output is a tiny bit unfortunate 🤷‍♂️ I should have covered this

}));
});
}