From 8899b84200ccab2e0b186b2b113fd7cdbd3e7385 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 1 Apr 2018 08:41:48 +0200 Subject: [PATCH 1/3] assert: add `rawMessage` to error messages The `rawMessage` provides the message with a guarantee not to include any colors. --- lib/internal/errors.js | 1 + test/parallel/test-assert-fail.js | 6 ++++-- test/pseudo-tty/test-assert-colors.js | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index f8073d15fe8d49..3fe74d9e5c42ed 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -386,6 +386,7 @@ class AssertionError extends Error { this.actual = actual; this.expected = expected; this.operator = operator; + this.rawMessage = lazyInternalUtil().removeColors(this.message); Error.captureStackTrace(this, stackStartFn); } } diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js index e8336e8f2169ef..fb99075196d3fb 100644 --- a/test/parallel/test-assert-fail.js +++ b/test/parallel/test-assert-fail.js @@ -12,7 +12,8 @@ assert.throws( message: 'Failed', operator: undefined, actual: undefined, - expected: undefined + expected: undefined, + rawMessage: 'Failed' } ); @@ -25,7 +26,8 @@ assert.throws(() => { message: 'custom message', operator: undefined, actual: undefined, - expected: undefined + expected: undefined, + rawMessage: 'custom message' }); // One arg = Error diff --git a/test/pseudo-tty/test-assert-colors.js b/test/pseudo-tty/test-assert-colors.js index 7c5845bdaa271a..2cc298ff8091ba 100644 --- a/test/pseudo-tty/test-assert-colors.js +++ b/test/pseudo-tty/test-assert-colors.js @@ -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. @@ -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); } From 47ab44c8910623fbb081602f5a1e30ca8f1b21ed Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 2 Apr 2018 18:25:27 +0200 Subject: [PATCH 2/3] fixup: use suggestion --- lib/internal/errors.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 3fe74d9e5c42ed..45d5ff17e1d633 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -386,9 +386,21 @@ class AssertionError extends Error { this.actual = actual; this.expected = expected; this.operator = operator; - this.rawMessage = lazyInternalUtil().removeColors(this.message); 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 From 4fbb209fa1ef6c21394297f40048317f770d8840 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 4 Apr 2018 15:29:47 +0200 Subject: [PATCH 3/3] fixup: add doc --- doc/api/assert.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/assert.md b/doc/api/assert.md index ff06c9250f7da3..b3321897fdbfa7 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -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');