Skip to content

Commit 2bee799

Browse files
committed
assert: provide info about actual error
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>
1 parent 5a8fcd0 commit 2bee799

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

lib/assert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ function expectsNoError(stackStartFn, actual, error, message) {
510510
actual,
511511
expected: error,
512512
operator: stackStartFn.name,
513-
message: `Got unwanted ${fnType}${details}\n${actual && actual.message}`,
513+
message: `Got unwanted ${fnType}${details}\n` +
514+
`Actual message: "${actual && actual.message}"`,
514515
stackStartFn
515516
});
516517
}

test/parallel/test-assert-async.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ common.crashOnUnhandledRejection();
3434
assert(err instanceof assert.AssertionError,
3535
`${err.name} is not instance of AssertionError`);
3636
assert.strictEqual(err.code, 'ERR_ASSERTION');
37-
assert(/^Got unwanted rejection\.\n$/.test(err.message));
37+
assert.strictEqual(err.message,
38+
'Got unwanted rejection.\nActual message: ""');
3839
assert.strictEqual(err.operator, 'doesNotReject');
3940
assert.ok(!err.stack.includes('at Function.doesNotReject'));
4041
return true;

test/parallel/test-assert.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,22 @@ assert.throws(() => thrower(TypeError));
124124
assert.ok(threw, 'a.doesNotThrow is not catching type matching errors');
125125
}
126126

127-
common.expectsError(
127+
assert.throws(
128128
() => a.doesNotThrow(() => thrower(Error), 'user message'),
129129
{
130-
type: a.AssertionError,
130+
name: 'AssertionError [ERR_ASSERTION]',
131131
code: 'ERR_ASSERTION',
132132
operator: 'doesNotThrow',
133-
message: 'Got unwanted exception: user message\n[object Object]'
133+
message: 'Got unwanted exception: user message\n' +
134+
'Actual message: "[object Object]"'
134135
}
135136
);
136137

137-
common.expectsError(
138-
() => a.doesNotThrow(() => thrower(Error), 'user message'),
139-
{
140-
code: 'ERR_ASSERTION',
141-
message: /Got unwanted exception: user message\n\[object Object\]/
142-
}
143-
);
144-
145-
common.expectsError(
138+
assert.throws(
146139
() => a.doesNotThrow(() => thrower(Error)),
147140
{
148141
code: 'ERR_ASSERTION',
149-
message: /Got unwanted exception\.\n\[object Object\]/
142+
message: 'Got unwanted exception.\nActual message: "[object Object]"'
150143
}
151144
);
152145

@@ -833,13 +826,13 @@ common.expectsError(
833826

834827
// eslint-disable-next-line no-throw-literal
835828
assert.throws(() => { throw undefined; }, /undefined/);
836-
common.expectsError(
829+
assert.throws(
837830
// eslint-disable-next-line no-throw-literal
838831
() => a.doesNotThrow(() => { throw undefined; }),
839832
{
840-
type: assert.AssertionError,
833+
name: 'AssertionError [ERR_ASSERTION]',
841834
code: 'ERR_ASSERTION',
842-
message: 'Got unwanted exception.\nundefined'
835+
message: 'Got unwanted exception.\nActual message: "undefined"'
843836
}
844837
);
845838
}

0 commit comments

Comments
 (0)