diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index a77414834d1e32..691f057ad281e3 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. diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 27421d2373eab1..e317fc31420ae0 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -1401,3 +1401,16 @@ assert.throws( ); assert.doesNotMatch('I will pass', /different$/); } + +{ + const tempColor = inspect.defaultOptions.colors; + assert.throws(() => { + inspect.defaultOptions.colors = true; + // Guarantee the position indicator is placed correctly. + assert.strictEqual(111554n, 11111115); + }, (err) => { + assert.strictEqual(inspect(err).split('\n')[5], ' ^'); + inspect.defaultOptions.colors = tempColor; + return true; + }); +}