Skip to content

Commit

Permalink
assert: provide info about actual error
Browse files Browse the repository at this point in the history
In case a error is caught in `assert.doesNotThrow` or
`assert.doesNotReject` it will now also indicate what the real
error message was.

PR-URL: #19884
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
BridgeAR authored and jasnell committed Apr 16, 2018
1 parent 974d07d commit 9743e75
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ function expectsNoError(stackStartFn, actual, error, message) {
actual,
expected: error,
operator: stackStartFn.name,
message: `Got unwanted ${fnType}${details}\n${actual && actual.message}`,
message: `Got unwanted ${fnType}${details}\n` +
`Actual message: "${actual && actual.message}"`,
stackStartFn
});
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ common.crashOnUnhandledRejection();
assert(err instanceof assert.AssertionError,
`${err.name} is not instance of AssertionError`);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert(/^Got unwanted rejection\.\n$/.test(err.message));
assert.strictEqual(err.message,
'Got unwanted rejection.\nActual message: ""');
assert.strictEqual(err.operator, 'doesNotReject');
assert.ok(!err.stack.includes('at Function.doesNotReject'));
return true;
Expand Down
25 changes: 9 additions & 16 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,22 @@ assert.throws(() => thrower(TypeError));
assert.ok(threw, 'a.doesNotThrow is not catching type matching errors');
}

common.expectsError(
assert.throws(
() => a.doesNotThrow(() => thrower(Error), 'user message'),
{
type: a.AssertionError,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
operator: 'doesNotThrow',
message: 'Got unwanted exception: user message\n[object Object]'
message: 'Got unwanted exception: user message\n' +
'Actual message: "[object Object]"'
}
);

common.expectsError(
() => a.doesNotThrow(() => thrower(Error), 'user message'),
{
code: 'ERR_ASSERTION',
message: /Got unwanted exception: user message\n\[object Object\]/
}
);

common.expectsError(
assert.throws(
() => a.doesNotThrow(() => thrower(Error)),
{
code: 'ERR_ASSERTION',
message: /Got unwanted exception\.\n\[object Object\]/
message: 'Got unwanted exception.\nActual message: "[object Object]"'
}
);

Expand Down Expand Up @@ -833,13 +826,13 @@ common.expectsError(

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

0 comments on commit 9743e75

Please sign in to comment.