Skip to content
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
9 changes: 6 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const { inspect } = require('util');

const assert = module.exports = ok;

const NO_EXCEPTION_SENTINEL = {};
Copy link
Member

Choose a reason for hiding this comment

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

Would a symbol work here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's merged now but for posterity: yes, but it doesn't add anything extra and it uses more memory than a plain empty object literal.


// All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
Expand Down Expand Up @@ -253,6 +255,7 @@ function getActual(block) {
} catch (e) {
return e;
}
return NO_EXCEPTION_SENTINEL;
}

// Expected to throw an error.
Expand All @@ -270,7 +273,7 @@ assert.throws = function throws(block, error, message) {
error = null;
}

if (actual === undefined) {
if (actual === NO_EXCEPTION_SENTINEL) {
let details = '';
if (error && error.name) {
details += ` (${error.name})`;
Expand All @@ -291,7 +294,7 @@ assert.throws = function throws(block, error, message) {

assert.doesNotThrow = function doesNotThrow(block, error, message) {
const actual = getActual(block);
if (actual === undefined)
if (actual === NO_EXCEPTION_SENTINEL)
return;

if (typeof error === 'string') {
Expand All @@ -305,7 +308,7 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
actual,
expected: error,
operator: 'doesNotThrow',
message: `Got unwanted exception${details}\n${actual.message}`,
message: `Got unwanted exception${details}\n${actual && actual.message}`,
stackStartFn: doesNotThrow
});
}
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,4 +857,16 @@ common.expectsError(
message: "message: expected '', not 'foo'"
}
);

// eslint-disable-next-line no-throw-literal
assert.throws(() => { throw undefined; }, /undefined/);
common.expectsError(
// eslint-disable-next-line no-throw-literal
() => assert.doesNotThrow(() => { throw undefined; }),
{
type: assert.AssertionError,
code: 'ERR_ASSERTION',
message: 'Got unwanted exception.\nundefined'
}
);
}