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 .throws and .doesNotThrow stack frames #17703

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/assert.js
Expand Up @@ -242,7 +242,7 @@ function innerThrows(shouldThrow, block, expected, message) {
expected,
operator: 'throws',
message: `Missing expected exception${details}`,
stackStartFn: innerThrows
stackStartFn: assert.throws
});
}
if (expected && expectedException(actual, expected) === false) {
Expand All @@ -256,7 +256,7 @@ function innerThrows(shouldThrow, block, expected, message) {
expected,
operator: 'doesNotThrow',
message: `Got unwanted exception${details}\n${actual.message}`,
stackStartFn: innerThrows
stackStartFn: assert.doesNotThrow
});
}
throw actual;
Expand Down
56 changes: 20 additions & 36 deletions test/parallel/test-assert.js
Expand Up @@ -476,33 +476,21 @@ common.expectsError(
}
);

{
let threw = false;
try {
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
} catch (e) {
threw = true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Got unwanted exception: user message\n\[object Object\]/
})(e);
common.expectsError(
() => assert.doesNotThrow(makeBlock(thrower, Error), 'user message'),
{
code: 'ERR_ASSERTION',
message: /Got unwanted exception: user message\n\[object Object\]/
}
assert.ok(threw);
}
);

{
let threw = false;
try {
assert.doesNotThrow(makeBlock(thrower, Error));
} catch (e) {
threw = true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Got unwanted exception\.\n\[object Object\]/
})(e);
common.expectsError(
() => assert.doesNotThrow(makeBlock(thrower, Error)),
{
code: 'ERR_ASSERTION',
message: /Got unwanted exception\.\n\[object Object\]/
}
assert.ok(threw);
}
);

// make sure that validating using constructor really works
{
Expand Down Expand Up @@ -634,6 +622,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
} catch (e) {
threw = true;
assert.strictEqual(e.message, 'Missing expected exception.');
assert.ok(!e.stack.includes('throws'), e.stack);
}
assert.ok(threw);
}
Expand Down Expand Up @@ -678,6 +667,7 @@ try {
threw = true;
assert.ok(e.message.includes(rangeError.message));
assert.ok(e instanceof assert.AssertionError);
assert.ok(!e.stack.includes('doesNotThrow'), e.stack);
}
assert.ok(threw);
}
Expand All @@ -689,21 +679,15 @@ try {
}

const testBlockTypeError = (method, block) => {
let threw = true;

try {
method(block);
threw = false;
} catch (e) {
common.expectsError({
common.expectsError(
() => method(block),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "block" argument must be of type Function. Received ' +
`type ${typeName(block)}`
})(e);
}

assert.ok(threw);
`type ${typeName(block)}`
}
);
};

testBlockTypeError(assert.throws, 'string');
Expand Down