Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 6101eb1

Browse files
hackedyisaacs
authored andcommitted
assert: put info in err.message, not err.name
4716dc6 made assert.equal() and related functions work better by generating a better toString() from the expected, actual, and operator values passed to fail(). Unfortunately, this was accomplished by putting the generated message into the error's `name` property. When you passed in a custom error message, the error would put the custom error into `name` *and* `message`, resulting in helpful string representations like "AssertionError: Oh no: Oh no". This commit resolves that issue by storing the generated message in the `message` property while leaving the error's name alone and adding a regression test so that this doesn't pop back up later. Closes #5292.
1 parent a835a2f commit 6101eb1

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

lib/assert.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ var assert = module.exports = ok;
3838
// expected: expected })
3939

4040
assert.AssertionError = function AssertionError(options) {
41-
this.message = options.message;
41+
this.name = 'AssertionError';
4242
this.actual = options.actual;
4343
this.expected = options.expected;
4444
this.operator = options.operator;
45+
this.message = options.message || getMessage(this)
4546
var stackStartFunction = options.stackStartFunction || fail;
46-
47-
this.name = getName(this, options.message);
4847
Error.captureStackTrace(this, stackStartFunction);
4948
};
5049

@@ -72,15 +71,10 @@ function truncate(s, n) {
7271
}
7372
}
7473

75-
function getName(self, message) {
76-
if (message) {
77-
return 'AssertionError: ' + message;
78-
} else {
79-
return 'AssertionError: ' +
80-
truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
81-
self.operator + ' ' +
82-
truncate(JSON.stringify(self.expected, replacer), 128);
83-
}
74+
function getMessage(self) {
75+
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
76+
self.operator + ' ' +
77+
truncate(JSON.stringify(self.expected, replacer), 128);
8478
}
8579

8680
// At present only the three keys mentioned above are used and

test/simple/test-assert.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,16 @@ try {
293293
assert.equal(e.message, 'Missing expected exception..');
294294
}
295295
assert.ok(threw);
296+
297+
// #5292
298+
try {
299+
assert.equal(1, 2);
300+
} catch (e) {
301+
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2')
302+
}
303+
304+
try {
305+
assert.equal(1, 2, 'oh no');
306+
} catch (e) {
307+
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no')
308+
}

0 commit comments

Comments
 (0)