@@ -21,15 +21,12 @@ const { inspect } = require('internal/util/inspect');
2121const {
2222 removeColors,
2323} = require ( 'internal/util' ) ;
24+ const colors = require ( 'internal/util/colors' ) ;
2425const {
2526 validateObject,
2627} = require ( 'internal/validators' ) ;
2728const { isErrorStackTraceLimitWritable } = require ( 'internal/errors' ) ;
2829
29- let blue = '' ;
30- let green = '' ;
31- let red = '' ;
32- let white = '' ;
3330
3431const kReadableOperator = {
3532 deepStrictEqual : 'Expected values to be strictly deep-equal:' ,
@@ -169,7 +166,7 @@ function createErrDiff(actual, expected, operator) {
169166 // Only remove lines in case it makes sense to collapse those.
170167 // TODO: Accept env to always show the full error.
171168 if ( actualLines . length > 50 ) {
172- actualLines [ 46 ] = `${ blue } ...${ white } ` ;
169+ actualLines [ 46 ] = `${ colors . blue } ...${ colors . white } ` ;
173170 while ( actualLines . length > 47 ) {
174171 ArrayPrototypePop ( actualLines ) ;
175172 }
@@ -182,7 +179,7 @@ function createErrDiff(actual, expected, operator) {
182179 // There were at least five identical lines at the end. Mark a couple of
183180 // skipped.
184181 if ( i >= 5 ) {
185- end = `\n${ blue } ...${ white } ${ end } ` ;
182+ end = `\n${ colors . blue } ...${ colors . white } ${ end } ` ;
186183 skipped = true ;
187184 }
188185 if ( other !== '' ) {
@@ -193,15 +190,15 @@ function createErrDiff(actual, expected, operator) {
193190 let printedLines = 0 ;
194191 let identical = 0 ;
195192 const msg = kReadableOperator [ operator ] +
196- `\n${ green } + actual${ white } ${ red } - expected${ white } ` ;
197- const skippedMsg = ` ${ blue } ...${ white } Lines skipped` ;
193+ `\n${ colors . green } + actual${ colors . white } ${ colors . red } - expected${ colors . white } ` ;
194+ const skippedMsg = ` ${ colors . blue } ...${ colors . white } Lines skipped` ;
198195
199196 let lines = actualLines ;
200- let plusMinus = `${ green } +${ white } ` ;
197+ let plusMinus = `${ colors . green } +${ colors . white } ` ;
201198 let maxLength = expectedLines . length ;
202199 if ( actualLines . length < maxLines ) {
203200 lines = expectedLines ;
204- plusMinus = `${ red } -${ white } ` ;
201+ plusMinus = `${ colors . red } -${ colors . white } ` ;
205202 maxLength = actualLines . length ;
206203 }
207204
@@ -216,7 +213,7 @@ function createErrDiff(actual, expected, operator) {
216213 res += `\n ${ lines [ i - 3 ] } ` ;
217214 printedLines ++ ;
218215 } else {
219- res += `\n${ blue } ...${ white } ` ;
216+ res += `\n${ colors . blue } ...${ colors . white } ` ;
220217 skipped = true ;
221218 }
222219 }
@@ -272,7 +269,7 @@ function createErrDiff(actual, expected, operator) {
272269 res += `\n ${ actualLines [ i - 3 ] } ` ;
273270 printedLines ++ ;
274271 } else {
275- res += `\n${ blue } ...${ white } ` ;
272+ res += `\n${ colors . blue } ...${ colors . white } ` ;
276273 skipped = true ;
277274 }
278275 }
@@ -286,8 +283,8 @@ function createErrDiff(actual, expected, operator) {
286283 identical = 0 ;
287284 // Add the actual line to the result and cache the expected diverging
288285 // line so consecutive diverging lines show up as +++--- and not +-+-+-.
289- res += `\n${ green } +${ white } ${ actualLine } ` ;
290- other += `\n${ red } -${ white } ${ expectedLine } ` ;
286+ res += `\n${ colors . green } +${ colors . white } ${ actualLine } ` ;
287+ other += `\n${ colors . red } -${ colors . white } ${ expectedLine } ` ;
291288 printedLines += 2 ;
292289 // Lines are identical
293290 } else {
@@ -306,8 +303,8 @@ function createErrDiff(actual, expected, operator) {
306303 }
307304 // Inspected object to big (Show ~50 rows max)
308305 if ( printedLines > 50 && i < maxLines - 2 ) {
309- return `${ msg } ${ skippedMsg } \n${ res } \n${ blue } ...${ white } ${ other } \n` +
310- `${ blue } ...${ white } ` ;
306+ return `${ msg } ${ skippedMsg } \n${ res } \n${ colors . blue } ...${ colors . white } ${ other } \n` +
307+ `${ colors . blue } ...${ colors . white } ` ;
311308 }
312309 }
313310
@@ -347,21 +344,9 @@ class AssertionError extends Error {
347344 if ( message != null ) {
348345 super ( String ( message ) ) ;
349346 } else {
350- if ( process . stderr . isTTY ) {
351- // Reset on each call to make sure we handle dynamically set environment
352- // variables correct.
353- if ( process . stderr . hasColors ( ) ) {
354- blue = '\u001b[34m' ;
355- green = '\u001b[32m' ;
356- white = '\u001b[39m' ;
357- red = '\u001b[31m' ;
358- } else {
359- blue = '' ;
360- green = '' ;
361- white = '' ;
362- red = '' ;
363- }
364- }
347+ // Reset colors on each call to make sure we handle dynamically set environment
348+ // variables correct.
349+ colors . refresh ( ) ;
365350 // Prevent the error stack from being visible by duplicating the error
366351 // in a very close way to the original in case both sides are actually
367352 // instances of Error.
@@ -393,7 +378,7 @@ class AssertionError extends Error {
393378 // Only remove lines in case it makes sense to collapse those.
394379 // TODO: Accept env to always show the full error.
395380 if ( res . length > 50 ) {
396- res [ 46 ] = `${ blue } ...${ white } ` ;
381+ res [ 46 ] = `${ colors . blue } ...${ colors . white } ` ;
397382 while ( res . length > 47 ) {
398383 ArrayPrototypePop ( res ) ;
399384 }
0 commit comments