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 rejects stack trace and operator #27047

Closed
wants to merge 2 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
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');
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
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());