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: fix internal logic judgment #27743

Closed
wants to merge 6 commits 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
13 changes: 6 additions & 7 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,12 @@ changes:
> functions instead.

If `message` is falsy, the error message is set as the values of `actual` and
`expected` separated by the provided `operator`. If just the two `actual` and
`expected` arguments are provided, `operator` will default to `'!='`. If
`message` is provided as third argument it will be used as the error message and
the other arguments will be stored as properties on the thrown object. If
`stackStartFn` is provided, all stack frames above that function will be
removed from stacktrace (see [`Error.captureStackTrace`]). If no arguments are
given, the default message `Failed` will be used.
`expected` separated by the provided `operator`. If `operator` is falsy and no
message is set, it will default to `'!='`. If `message` is truthy, the `operator`
will default to `'fail'` and `message` will be used for the error message. Other
arguments will be stored as properties on the thrown object. If `stackStartFn`
is provided, all stack frames above that function will be removed from
stacktrace (see [`Error.captureStackTrace`][]).
Comment on lines +636 to +641
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to change the operator description above to explain when it's set to 'fail' and when to '!='.

I would also move the stackStartFn description to the part above. That way the total description below concentrates on the general behavior instead of describing each individual option setting.

Right now we also miss to document that assert.fail(1, 2, new Error() throws the passed through error. That could be described in the message property above.


```js
const assert = require('assert').strict;
Expand Down
33 changes: 21 additions & 12 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ const meta = [
const escapeFn = (str) => meta[str.charCodeAt(0)];

let warned = false;
function showMulParamDeprecation() {
if (warned === false) {
warned = true;
process.emitWarning(
'assert.fail() with more than one argument is deprecated. ' +
'Please use assert.strictEqual() instead or only pass a message.',
'DeprecationWarning',
'DEP0094'
);
}
}

// The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
Expand Down Expand Up @@ -100,25 +111,23 @@ function fail(actual, expected, message, operator, stackStartFn) {
message = actual;
actual = undefined;
} else {
if (warned === false) {
warned = true;
process.emitWarning(
'assert.fail() with more than one argument is deprecated. ' +
'Please use assert.strictEqual() instead or only pass a message.',
'DeprecationWarning',
'DEP0094'
);
}
if (argsLen === 2)
operator = '!=';
showMulParamDeprecation();
}

if (message instanceof Error) throw message;

if (operator === undefined) {
if (message) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (message) {
if (message != null) {

operator = 'fail';
} else {
operator = '!=';
}
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved

const errArgs = {
actual,
expected,
operator: operator === undefined ? 'fail' : operator,
operator,
stackStartFn: stackStartFn || fail,
message
};
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-assert-fail-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ assert.throws(
function foo() { assert.fail('first', 'second', 'message', '!==', foo); },
(err) => !/^\s*at\sfoo\b/m.test(err.stack)
);

// Actual undefined = Error
assert.throws(() => {
assert.fail(undefined);
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'Failed',
operator: 'fail',
actual: undefined,
expected: undefined,
Comment on lines +67 to +74
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.fail(undefined);
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'Failed',
operator: 'fail',
actual: undefined,
expected: undefined,
assert.fail(1, 2, undefined, undefined);
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: '1 != 2',
operator: '!=',
actual: 1,
expected: 2,

Ideally, another identical test could be added that sets message to null.

generatedMessage: true
});
Copy link
Member

Choose a reason for hiding this comment

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

This test currently passes without the other changes in this PR. Can we please add a test case that currently fails?