Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
assert: respect assert.doesNotThrow message.
Addresses #2385.
Special handling to detect when user has supplied a custom message.
Added a test for user message.
When testing if `actual` value is an error use
`util.isError` instead of `instanceof`.
  • Loading branch information
Ilya Shaisultanov authored and diversario committed Apr 15, 2016
commit 83ec4b5cdc2004a88d608137a46d01897e2e7f6d
6 changes: 5 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ function _throws(shouldThrow, block, expected, message) {
fail(actual, expected, 'Missing expected exception' + message);
}

if (!shouldThrow && expectedException(actual, expected)) {
if (!shouldThrow &&
actual instanceof Error &&
typeof message === 'string' &&
expectedException(actual, expected) ||
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to see a more liberal use of parens to make it explicit what the evaluation order is that you're using here, mixing && and || without parens makes this very difficult to parse. The number of conditions in here adds to that difficulty and it'd be nice to reduce it with some compacting variables—although not essential, just would be nice to make this less terse.

Copy link
Author

Choose a reason for hiding this comment

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

Breaking out the || part into a var sounds good.

!shouldThrow && actual && !expected) {
fail(actual, expected, 'Got unwanted exception' + message);
}

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ assert.throws(function() {assert.ifError(new Error('test error'));});
assert.doesNotThrow(function() {assert.ifError(null);});
assert.doesNotThrow(function() {assert.ifError();});

try {
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
} catch(e) {
assert.equal(e.message, 'Got unwanted exception. user message',
'a.doesNotThrow ignores user message');
}

Copy link
Member

Choose a reason for hiding this comment

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

Just a suggestion...

assert.throws(() => {
  assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
}, /Got unwanted exception. user message/, 'a.doesNotThrow ignores user message');

// make sure that validating using constructor really works
threw = false;
try {
Expand Down