Skip to content

Commit

Permalink
assert: fix actual & expected input
Browse files Browse the repository at this point in the history
This makes sure the actual and expected values on the error thrown
by `assert.throws` etc. are always as they should be.

PR-URL: nodejs#19925
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR committed May 1, 2018
1 parent d2c3fef commit 87917ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function expectsError(stackStartFn, actual, error, message) {
error);
}
message = error;
error = null;
error = undefined;
}

if (actual === NO_EXCEPTION_SENTINEL) {
Expand All @@ -301,7 +301,7 @@ function expectsError(stackStartFn, actual, error, message) {
details += message ? `: ${message}` : '.';
const fnType = stackStartFn === rejects ? 'rejection' : 'exception';
innerFail({
actual,
actual: undefined,
expected: error,
operator: stackStartFn.name,
message: `Missing expected ${fnType}${details}`,
Expand All @@ -319,7 +319,7 @@ function expectsNoError(stackStartFn, actual, error, message) {

if (typeof error === 'string') {
message = error;
error = null;
error = undefined;
}

if (!error || expectedException(actual, error)) {
Expand Down
34 changes: 21 additions & 13 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,40 @@ a.throws(() => thrower(TypeError), (err) => {
const noop = () => {};
assert.throws(
() => { a.throws((noop)); },
common.expectsError({
{
code: 'ERR_ASSERTION',
message: /^Missing expected exception\.$/,
operator: 'throws'
}));
message: 'Missing expected exception.',
operator: 'throws',
actual: undefined,
expected: undefined
});

assert.throws(
() => { a.throws(noop, TypeError); },
common.expectsError({
{
code: 'ERR_ASSERTION',
message: /^Missing expected exception \(TypeError\)\.$/
}));
message: 'Missing expected exception (TypeError).',
actual: undefined,
expected: TypeError
});

assert.throws(
() => { a.throws(noop, 'fhqwhgads'); },
common.expectsError({
{
code: 'ERR_ASSERTION',
message: /^Missing expected exception: fhqwhgads$/
}));
message: 'Missing expected exception: fhqwhgads',
actual: undefined,
expected: undefined
});

assert.throws(
() => { a.throws(noop, TypeError, 'fhqwhgads'); },
common.expectsError({
{
code: 'ERR_ASSERTION',
message: /^Missing expected exception \(TypeError\): fhqwhgads$/
}));
message: 'Missing expected exception (TypeError): fhqwhgads',
actual: undefined,
expected: TypeError
});

let threw = false;
try {
Expand Down

0 comments on commit 87917ca

Please sign in to comment.