diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2020-01-20 22:44:38 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2020-02-09 15:39:54 +0300 |
commit | 36f328e21fe19f9767e1bc04481bdd921ab49615 (patch) | |
tree | b0f32ab6a9b7623a5e7cc7d6b03f6707ae5abba6 /lib/internal/assert | |
parent | 1450ea7bf608a54640863e5ba23c6f0ce430e14f (diff) |
assert: align character indicators properly
This makes sure color codes are not taken into account in case
util.inspect's default value was changed.
PR-URL: https://github.com/nodejs/node/pull/31429
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/internal/assert')
-rw-r--r-- | lib/internal/assert/assertion_error.js | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index a77414834d1..691f057ad28 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -13,6 +13,9 @@ const { inspect } = require('internal/util/inspect'); const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors'); +const { + removeColors, +} = require('internal/util'); let blue = ''; let green = ''; @@ -93,7 +96,12 @@ function createErrDiff(actual, expected, operator) { // equal, check further special handling. if (actualLines.length === 1 && expectedLines.length === 1 && actualLines[0] !== expectedLines[0]) { - const inputLength = actualLines[0].length + expectedLines[0].length; + // Check for the visible length using the `removeColors()` function, if + // appropriate. + const c = inspect.defaultOptions.colors; + const actualRaw = c ? removeColors(actualLines[0]) : actualLines[0]; + const expectedRaw = c ? removeColors(expectedLines[0]) : expectedLines[0]; + const inputLength = actualRaw.length + expectedRaw.length; // If the character length of "actual" and "expected" together is less than // kMaxShortLength and if neither is an object and at least one of them is // not `zero`, use the strict equal comparison to visualize the output. @@ -110,7 +118,7 @@ function createErrDiff(actual, expected, operator) { // not a tty, use a default value of 80 characters. const maxLength = process.stderr.isTTY ? process.stderr.columns : 80; if (inputLength < maxLength) { - while (actualLines[0][i] === expectedLines[0][i]) { + while (actualRaw[i] === expectedRaw[i]) { i++; } // Ignore the first characters. |