@@ -21,15 +21,12 @@ const { inspect } = require('internal/util/inspect');
21
21
const {
22
22
removeColors,
23
23
} = require ( 'internal/util' ) ;
24
+ const colors = require ( 'internal/util/colors' ) ;
24
25
const {
25
26
validateObject,
26
27
} = require ( 'internal/validators' ) ;
27
28
const { isErrorStackTraceLimitWritable } = require ( 'internal/errors' ) ;
28
29
29
- let blue = '' ;
30
- let green = '' ;
31
- let red = '' ;
32
- let white = '' ;
33
30
34
31
const kReadableOperator = {
35
32
deepStrictEqual : 'Expected values to be strictly deep-equal:' ,
@@ -169,7 +166,7 @@ function createErrDiff(actual, expected, operator) {
169
166
// Only remove lines in case it makes sense to collapse those.
170
167
// TODO: Accept env to always show the full error.
171
168
if ( actualLines . length > 50 ) {
172
- actualLines [ 46 ] = `${ blue } ...${ white } ` ;
169
+ actualLines [ 46 ] = `${ colors . blue } ...${ colors . white } ` ;
173
170
while ( actualLines . length > 47 ) {
174
171
ArrayPrototypePop ( actualLines ) ;
175
172
}
@@ -182,7 +179,7 @@ function createErrDiff(actual, expected, operator) {
182
179
// There were at least five identical lines at the end. Mark a couple of
183
180
// skipped.
184
181
if ( i >= 5 ) {
185
- end = `\n${ blue } ...${ white } ${ end } ` ;
182
+ end = `\n${ colors . blue } ...${ colors . white } ${ end } ` ;
186
183
skipped = true ;
187
184
}
188
185
if ( other !== '' ) {
@@ -193,15 +190,15 @@ function createErrDiff(actual, expected, operator) {
193
190
let printedLines = 0 ;
194
191
let identical = 0 ;
195
192
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` ;
198
195
199
196
let lines = actualLines ;
200
- let plusMinus = `${ green } +${ white } ` ;
197
+ let plusMinus = `${ colors . green } +${ colors . white } ` ;
201
198
let maxLength = expectedLines . length ;
202
199
if ( actualLines . length < maxLines ) {
203
200
lines = expectedLines ;
204
- plusMinus = `${ red } -${ white } ` ;
201
+ plusMinus = `${ colors . red } -${ colors . white } ` ;
205
202
maxLength = actualLines . length ;
206
203
}
207
204
@@ -216,7 +213,7 @@ function createErrDiff(actual, expected, operator) {
216
213
res += `\n ${ lines [ i - 3 ] } ` ;
217
214
printedLines ++ ;
218
215
} else {
219
- res += `\n${ blue } ...${ white } ` ;
216
+ res += `\n${ colors . blue } ...${ colors . white } ` ;
220
217
skipped = true ;
221
218
}
222
219
}
@@ -272,7 +269,7 @@ function createErrDiff(actual, expected, operator) {
272
269
res += `\n ${ actualLines [ i - 3 ] } ` ;
273
270
printedLines ++ ;
274
271
} else {
275
- res += `\n${ blue } ...${ white } ` ;
272
+ res += `\n${ colors . blue } ...${ colors . white } ` ;
276
273
skipped = true ;
277
274
}
278
275
}
@@ -286,8 +283,8 @@ function createErrDiff(actual, expected, operator) {
286
283
identical = 0 ;
287
284
// Add the actual line to the result and cache the expected diverging
288
285
// 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 } ` ;
291
288
printedLines += 2 ;
292
289
// Lines are identical
293
290
} else {
@@ -306,8 +303,8 @@ function createErrDiff(actual, expected, operator) {
306
303
}
307
304
// Inspected object to big (Show ~50 rows max)
308
305
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 } ` ;
311
308
}
312
309
}
313
310
@@ -347,21 +344,9 @@ class AssertionError extends Error {
347
344
if ( message != null ) {
348
345
super ( String ( message ) ) ;
349
346
} 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 ( ) ;
365
350
// Prevent the error stack from being visible by duplicating the error
366
351
// in a very close way to the original in case both sides are actually
367
352
// instances of Error.
@@ -393,7 +378,7 @@ class AssertionError extends Error {
393
378
// Only remove lines in case it makes sense to collapse those.
394
379
// TODO: Accept env to always show the full error.
395
380
if ( res . length > 50 ) {
396
- res [ 46 ] = `${ blue } ...${ white } ` ;
381
+ res [ 46 ] = `${ colors . blue } ...${ colors . white } ` ;
397
382
while ( res . length > 47 ) {
398
383
ArrayPrototypePop ( res ) ;
399
384
}
0 commit comments