Skip to content

Commit

Permalink
assert: fix rejects stack trace and operator
Browse files Browse the repository at this point in the history
This makes sure the stack trace is not removed due to a wrong stack
start function being used. It also fixes the wrong operator caused
by the same reason. This only applies in case an validation object
was used to validate the rejection passed to `assert.reject()` as
first argument.

PR-URL: #27047
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR committed Apr 7, 2019
1 parent eb2d416 commit 75463a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class Comparison {
}
}

function compareExceptionKey(actual, expected, key, message, keys) {
function compareExceptionKey(actual, expected, key, message, keys, fn) {
if (!(key in actual) || !isDeepStrictEqual(actual[key], expected[key])) {
if (!message) {
// Create placeholder objects to create a nice output.
Expand All @@ -513,24 +513,24 @@ function compareExceptionKey(actual, expected, key, message, keys) {
actual: a,
expected: b,
operator: 'deepStrictEqual',
stackStartFn: assert.throws
stackStartFn: fn
});
err.actual = actual;
err.expected = expected;
err.operator = 'throws';
err.operator = fn.name;
throw err;
}
innerFail({
actual,
expected,
message,
operator: 'throws',
stackStartFn: assert.throws
operator: fn.name,
stackStartFn: fn
});
}
}

function expectedException(actual, expected, msg) {
function expectedException(actual, expected, msg, fn) {
if (typeof expected !== 'function') {
if (isRegExp(expected))
return expected.test(actual);
Expand All @@ -548,9 +548,9 @@ function expectedException(actual, expected, msg) {
expected,
message: msg,
operator: 'deepStrictEqual',
stackStartFn: assert.throws
stackStartFn: fn
});
err.operator = 'throws';
err.operator = fn.name;
throw err;
}

Expand All @@ -570,7 +570,7 @@ function expectedException(actual, expected, msg) {
expected[key].test(actual[key])) {
continue;
}
compareExceptionKey(actual, expected, key, msg, keys);
compareExceptionKey(actual, expected, key, msg, keys, fn);
}
return true;
}
Expand Down Expand Up @@ -676,7 +676,7 @@ function expectsError(stackStartFn, actual, error, message) {
stackStartFn
});
}
if (error && expectedException(actual, error, message) === false) {
if (error && !expectedException(actual, error, message, stackStartFn)) {
throw actual;
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ promises.push(assert.rejects(
assert.strictEqual(err.message,
'Got unwanted rejection.\nActual message: "Failed"');
assert.strictEqual(err.operator, 'doesNotReject');
assert.ok(err.stack);
assert.ok(!err.stack.includes('at Function.doesNotReject'));
return true;
};
Expand All @@ -121,5 +122,29 @@ promises.push(assert.rejects(
}
));

{
const handler = (generated, actual, err) => {
assert.strictEqual(err.generatedMessage, generated);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert.strictEqual(err.actual, actual);
assert.strictEqual(err.operator, 'rejects');
assert(/rejects/.test(err.stack));
return true;
};
const err = new Error();
promises.push(assert.rejects(
assert.rejects(Promise.reject(null), { code: 'FOO' }),
handler.bind(null, true, null)
));
promises.push(assert.rejects(
assert.rejects(Promise.reject(5), { code: 'FOO' }, 'AAAAA'),
handler.bind(null, false, 5)
));
promises.push(assert.rejects(
assert.rejects(Promise.reject(err), { code: 'FOO' }, 'AAAAA'),
handler.bind(null, false, err)
));
}

// Make sure all async code gets properly executed.
Promise.all(promises).then(common.mustCall());

0 comments on commit 75463a9

Please sign in to comment.