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

assert: validate the block return type #19886

Closed
wants to merge 2 commits into from
Closed
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
Next Next commit
assert: validate the block return type
This makes sure the returned value when calling `block` is actually
of type promise in case `assert.rejects` or `assert.doesNotReject`
is called.
  • Loading branch information
BridgeAR committed Apr 16, 2018
commit 386f664dd81e023c9244f3c5d691dcbb1a775826
14 changes: 11 additions & 3 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,13 @@ function and awaits the returned promise to complete. It will then check that
the promise is not rejected.

If `block` is a function and it throws an error synchronously,
`assert.doesNotReject()` will return a rejected Promise with that error without
checking the error handler.
`assert.doesNotReject()` will return a rejected Promise with that error. If the
function does not return a promise, `assert.doesNotReject()` will return a
rejected Promise with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases
without checking the error handler.
Copy link
Contributor

Choose a reason for hiding this comment

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

This sentence fragment seems a bit unexpected. Should we add a verb to it?


If `block` is a function and it does not return a promise, an
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this redundant with the previous paragraph?

[`ERR_INVALID_RETURN_VALUE`][] error is going to be rejected.

Please note: Using `assert.doesNotReject()` is actually not useful because there
is little benefit by catching a rejection and then rejecting it again. Instead,
Expand Down Expand Up @@ -929,7 +934,9 @@ function and awaits the returned promise to complete. It will then check that
the promise is rejected.

If `block` is a function and it throws an error synchronously,
`assert.rejects()` will return a rejected Promise with that error without
`assert.rejects()` will return a rejected Promise with that error. If the
function does not return a promise, `assert.rejects()` will return a rejected
Promise with an [`ERR_INVALID_RETURN_VALUE`][] error. In both cases without
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

checking the error handler.

Besides the async nature to await the completion behaves identically to
Expand Down Expand Up @@ -1138,6 +1145,7 @@ assert.throws(throwingFirst, /Second$/);
Due to the confusing notation, it is recommended not to use a string as the
second argument. This might lead to difficult-to-spot errors.

[`ERR_INVALID_RETURN_VALUE`]: errors.html#errors_err_invalid_return_value
[`Class`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt
[`Error`]: errors.html#errors_class_error
Expand Down
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ An invalid `options.protocol` was passed.
Both `breakEvalOnSigint` and `eval` options were set in the REPL config, which
is not supported.

<a id="ERR_INVALID_RETURN_VALUE"></a>
### ERR_INVALID_RETURN_VALUE

Thrown in case a function option does not return a expected value on execution.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: an expected value

For example when a function is expected to return a promise.

<a id="ERR_INVALID_SYNC_FORK_INPUT"></a>
### ERR_INVALID_SYNC_FORK_INPUT

Expand Down
8 changes: 7 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const {
errorCache,
codes: {
ERR_AMBIGUOUS_ARGUMENT,
ERR_INVALID_ARG_TYPE
ERR_INVALID_ARG_TYPE,
ERR_INVALID_RETURN_VALUE
}
} = require('internal/errors');
const { openSync, closeSync, readSync } = require('fs');
Expand Down Expand Up @@ -455,6 +456,11 @@ async function waitForActual(block) {
if (typeof block === 'function') {
// Return a rejected promise if `block` throws synchronously.
resultPromise = block();
// Fail in case no promise is returned.
if (!checkIsPromise(resultPromise)) {
throw new ERR_INVALID_RETURN_VALUE('instance of Promise',
'block', resultPromise);
}
} else if (checkIsPromise(block)) {
resultPromise = block;
} else {
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,16 @@ E('ERR_INVALID_PERFORMANCE_MARK',
E('ERR_INVALID_PROTOCOL', 'Protocol "%s" not supported. Expected "%s"', Error);
E('ERR_INVALID_REPL_EVAL_CONFIG',
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError);
E('ERR_INVALID_RETURN_VALUE', (input, name, value) => {
let type;
if (value && value.constructor && value.constructor.name) {
type = `instance of ${value.constructor.name}`;
} else {
type = `type ${typeof value}`;
}
return `Expected ${input} to be returned from the "${name}"` +
` function but got ${type}.`;
}, TypeError);
E('ERR_INVALID_SYNC_FORK_INPUT',
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s',
TypeError);
Expand Down
37 changes: 24 additions & 13 deletions test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ const promises = [];
`${err.name} is not instance of AssertionError`);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert.strictEqual(err.message,
'Missing expected rejection (handler).');
'Missing expected rejection (mustNotCall).');
assert.strictEqual(err.operator, 'rejects');
assert.ok(!err.stack.includes('at Function.rejects'));
return true;
};

let promise = assert.rejects(async () => {}, handler);
promises.push(assert.rejects(promise, handler));
let promise = assert.rejects(async () => {}, common.mustNotCall());
promises.push(assert.rejects(promise, common.mustCall(handler)));

promise = assert.rejects(() => {}, handler);
promises.push(assert.rejects(promise, handler));
promise = assert.rejects(() => {}, common.mustNotCall());
promises.push(assert.rejects(promise, {
name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
code: 'ERR_INVALID_RETURN_VALUE',
message: 'Expected instance of Promise to be returned ' +
'from the "block" function but got type undefined.'
}));

promise = assert.rejects(Promise.resolve(), handler);
promises.push(assert.rejects(promise, handler));
promise = assert.rejects(Promise.resolve(), common.mustNotCall());
promises.push(assert.rejects(promise, common.mustCall(handler)));
}

{
Expand All @@ -67,7 +72,13 @@ promises.push(assert.rejects(
// Check `assert.doesNotReject`.
{
// `assert.doesNotReject` accepts a function or a promise as first argument.
promises.push(assert.doesNotReject(() => {}));
const promise = assert.doesNotReject(() => new Map(), common.mustNotCall());
promises.push(assert.rejects(promise, {
message: 'Expected instance of Promise to be returned ' +
'from the "block" function but got instance of Map.',
code: 'ERR_INVALID_RETURN_VALUE',
name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
}));
promises.push(assert.doesNotReject(async () => {}));
promises.push(assert.doesNotReject(Promise.resolve()));
}
Expand All @@ -93,14 +104,14 @@ promises.push(assert.rejects(

const rejectingFn = async () => assert.fail();

let promise = assert.doesNotReject(rejectingFn, handler1);
promises.push(assert.rejects(promise, handler2));
let promise = assert.doesNotReject(rejectingFn, common.mustCall(handler1));
promises.push(assert.rejects(promise, common.mustCall(handler2)));

promise = assert.doesNotReject(rejectingFn(), handler1);
promises.push(assert.rejects(promise, handler2));
promise = assert.doesNotReject(rejectingFn(), common.mustCall(handler1));
promises.push(assert.rejects(promise, common.mustCall(handler2)));

promise = assert.doesNotReject(() => assert.fail(), common.mustNotCall());
promises.push(assert.rejects(promise, handler1));
promises.push(assert.rejects(promise, common.mustCall(handler1)));
}

promises.push(assert.rejects(
Expand Down