@@ -2,38 +2,52 @@ import test from 'node:test';
22import assert from 'node:assert/strict' ;
33import { formatSnapshotDiffText } from '../output.ts' ;
44
5- test ( 'formatSnapshotDiffText renders unified diff lines with summary' , ( ) => {
6- const text = formatSnapshotDiffText ( {
7- baselineInitialized : false ,
8- summary : { additions : 2 , removals : 2 , unchanged : 4 } ,
9- lines : [
10- { kind : 'unchanged' , text : '@e0 [application]' } ,
11- { kind : 'unchanged' , text : '@e2 [window]' } ,
12- { kind : 'removed' , text : ' @e3 [other] "67"' } ,
13- { kind : 'removed' , text : ' @e4 [text] "67"' } ,
14- { kind : 'added' , text : ' @e3 [other] "134"' } ,
15- { kind : 'added' , text : ' @e4 [text] "134"' } ,
16- { kind : 'unchanged' , text : ' @e5 [button] "Increment"' } ,
17- { kind : 'unchanged' , text : ' @e6 [text] "Footer"' } ,
18- ] ,
19- } ) ;
5+ const DIFF_DATA = {
6+ mode : 'snapshot' ,
7+ baselineInitialized : false ,
8+ summary : { additions : 1 , removals : 1 , unchanged : 1 } ,
9+ lines : [
10+ { kind : 'unchanged' , text : '@e2 [window]' } ,
11+ { kind : 'removed' , text : ' @e3 [text] "67"' } ,
12+ { kind : 'added' , text : ' @e3 [text] "134"' } ,
13+ ] ,
14+ } as const ;
2015
21- assert . doesNotMatch ( text , / ^ @ e 0 \[ a p p l i c a t i o n \] $ / m) ;
22- assert . match ( text , / ^ @ e 2 \[ w i n d o w \] / m) ;
23- assert . match ( text , / ^ - @ e 3 \[ o t h e r \] " 6 7 " $ / m) ;
24- assert . match ( text , / ^ \+ @ e 3 \[ o t h e r \] " 1 3 4 " $ / m) ;
25- assert . match ( text , / ^ @ e 5 \[ b u t t o n \] " I n c r e m e n t " $ / m) ;
26- assert . doesNotMatch ( text , / ^ @ e 6 \[ t e x t \] " F o o t e r " $ / m) ;
27- assert . match ( text , / 2 a d d i t i o n s , 2 r e m o v a l s , 4 u n c h a n g e d / ) ;
16+ test ( 'formatSnapshotDiffText renders plain text when color is disabled' , ( ) => {
17+ const originalForceColor = process . env . FORCE_COLOR ;
18+ const originalNoColor = process . env . NO_COLOR ;
19+ process . env . FORCE_COLOR = '0' ;
20+ delete process . env . NO_COLOR ;
21+ try {
22+ const text = formatSnapshotDiffText ( { ...DIFF_DATA } ) ;
23+ assert . match ( text , / ^ @ e 2 \[ w i n d o w \] / m) ;
24+ assert . match ( text , / ^ - @ e 3 \[ t e x t \] " 6 7 " $ / m) ;
25+ assert . match ( text , / ^ \+ @ e 3 \[ t e x t \] " 1 3 4 " $ / m) ;
26+ assert . match ( text , / 1 a d d i t i o n s , 1 r e m o v a l s , 1 u n c h a n g e d / ) ;
27+ assert . equal ( text . includes ( '\x1b[' ) , false ) ;
28+ } finally {
29+ if ( typeof originalForceColor === 'string' ) process . env . FORCE_COLOR = originalForceColor ;
30+ else delete process . env . FORCE_COLOR ;
31+ if ( typeof originalNoColor === 'string' ) process . env . NO_COLOR = originalNoColor ;
32+ else delete process . env . NO_COLOR ;
33+ }
2834} ) ;
2935
30- test ( 'formatSnapshotDiffText renders baseline initialization text' , ( ) => {
31- const text = formatSnapshotDiffText ( {
32- baselineInitialized : true ,
33- summary : { additions : 0 , removals : 0 , unchanged : 5 } ,
34- lines : [ ] ,
35- } ) ;
36-
37- assert . match ( text , / B a s e l i n e i n i t i a l i z e d \( 5 l i n e s \) \. / ) ;
38- assert . doesNotMatch ( text , / a d d i t i o n s | r e m o v a l s | u n c h a n g e d / ) ;
36+ test ( 'formatSnapshotDiffText renders ANSI colors when forced' , ( ) => {
37+ const originalForceColor = process . env . FORCE_COLOR ;
38+ const originalNoColor = process . env . NO_COLOR ;
39+ process . env . FORCE_COLOR = '1' ;
40+ delete process . env . NO_COLOR ;
41+ try {
42+ const text = formatSnapshotDiffText ( { ...DIFF_DATA } ) ;
43+ assert . equal ( text . includes ( '\x1b[31m' ) , true ) ;
44+ assert . equal ( text . includes ( '\x1b[32m' ) , true ) ;
45+ assert . equal ( text . includes ( '\x1b[2m' ) , true ) ;
46+ assert . match ( text , / \x1b \[ [ 0 - 9 ; ] + m / ) ;
47+ } finally {
48+ if ( typeof originalForceColor === 'string' ) process . env . FORCE_COLOR = originalForceColor ;
49+ else delete process . env . FORCE_COLOR ;
50+ if ( typeof originalNoColor === 'string' ) process . env . NO_COLOR = originalNoColor ;
51+ else delete process . env . NO_COLOR ;
52+ }
3953} ) ;
0 commit comments