Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: add rawMessage to error messages #19723

Closed
wants to merge 3 commits into from
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
1 change: 1 addition & 0 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and:
* `code` {string} This is always set to the string `ERR_ASSERTION` to indicate
that the error is actually an assertion error.
* `operator` {string} Set to the passed in operator value.
* `rawMessage` {string} Identical to the message, but without any colors.

```js
const assert = require('assert');
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ class AssertionError extends Error {
this.operator = operator;
Error.captureStackTrace(this, stackStartFn);
}

get rawMessage() {
return lazyInternalUtil().removeColors(this.message);
}

set rawMessage(value) {
Object.defineProperty(this, 'rawMessage', {
value,
enumerable: true,
writable: true,
configurable: true
});
}
}

// This is defined here instead of using the assert module to avoid a
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ assert.throws(
message: 'Failed',
operator: undefined,
actual: undefined,
expected: undefined
expected: undefined,
rawMessage: 'Failed'
}
);

Expand All @@ -25,7 +26,8 @@ assert.throws(() => {
message: 'custom message',
operator: undefined,
actual: undefined,
expected: undefined
expected: undefined,
rawMessage: 'custom message'
});

// One arg = Error
Expand Down
4 changes: 4 additions & 0 deletions test/pseudo-tty/test-assert-colors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
// Flags: --expose-internals
require('../common');
const assert = require('assert').strict;
const util = require('internal/util');

try {
// Activate colors even if the tty does not support colors.
Expand All @@ -15,4 +17,6 @@ try {
' 2\n' +
' ]';
assert.strictEqual(err.message, expected);
assert.strictEqual(err.rawMessage, util.removeColors(expected));
assert.notStrictEqual(err.message, err.rawMessage);
}