Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add constructor test to async-hooks #13096

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ function fatalError(e) {

class AsyncHook {
constructor({ init, before, after, destroy }) {
if (init && typeof init !== 'function')
if (init !== undefined && typeof init !== 'function')
throw new TypeError('init must be a function');
if (before && typeof before !== 'function')
if (before !== undefined && typeof before !== 'function')
throw new TypeError('before must be a function');
if (after && typeof after !== 'function')
if (after !== undefined && typeof after !== 'function')
throw new TypeError('after must be a function');
if (destroy && typeof destroy !== 'function')
if (destroy !== undefined && typeof destroy !== 'function')
throw new TypeError('destroy must be a function');

this[init_symbol] = init;
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-async-wrap-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
require('../common');
const assert = require('assert');
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a couple of lines describing what this is testing
https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md

const async_hooks = require('async_hooks');

for (const badArg of [0, 1, false, true, null, 'hello']) {
for (const field of ['init', 'before', 'after', 'destroy']) {
assert.throws(() => {
async_hooks.createHook({ [field]: badArg });
}, new RegExp(`^TypeError: ${field} must be a function$`));
}
}