1+ import fs from 'node:fs'
2+ import * as path from 'node:path'
3+ import { Writable } from 'node:stream'
4+ import { pipeline } from 'node:stream/promises'
5+
6+ import { NdjsonToMessageStream } from '@cucumber/message-streams'
7+ import { Envelope , TestStepResultStatus } from '@cucumber/messages'
8+ import { expect } from 'chai'
9+ import { globbySync } from 'globby'
10+
11+ import { SummaryPrinter } from './SummaryPrinter.js'
12+ import { CUCUMBER_THEME } from './theme.js'
13+ import type { Options } from './types.js'
14+
15+ describe ( 'SummaryPrinter' , async ( ) => {
16+ const ndjsonFiles = globbySync ( `*.ndjson` , {
17+ cwd : path . join ( import . meta. dirname , '..' , '..' , 'testdata' , 'src' ) ,
18+ absolute : true ,
19+ } )
20+
21+ const variants : ReadonlyArray < { name : string ; options : Options } > = [
22+ {
23+ name : 'cucumber' ,
24+ options : {
25+ attachments : true ,
26+ featuresAndRules : true ,
27+ theme : CUCUMBER_THEME ,
28+ } ,
29+ } ,
30+ {
31+ name : 'plain' ,
32+ options : {
33+ attachments : true ,
34+ featuresAndRules : true ,
35+ theme : {
36+ status : {
37+ icon : {
38+ [ TestStepResultStatus . AMBIGUOUS ] : '✘' ,
39+ [ TestStepResultStatus . FAILED ] : '✘' ,
40+ [ TestStepResultStatus . PASSED ] : '✔' ,
41+ [ TestStepResultStatus . PENDING ] : '■' ,
42+ [ TestStepResultStatus . SKIPPED ] : '↷' ,
43+ [ TestStepResultStatus . UNDEFINED ] : '■' ,
44+ [ TestStepResultStatus . UNKNOWN ] : ' ' ,
45+ } ,
46+ } ,
47+ } ,
48+ } ,
49+ } ,
50+ ]
51+
52+ // just enough so Node.js internals consider it a color-supporting stream
53+ const fakeStream = {
54+ _writableState : { } ,
55+ isTTY : true ,
56+ getColorDepth : ( ) => 3 ,
57+ } as unknown as NodeJS . WritableStream
58+
59+ for ( const { name, options } of variants ) {
60+ describe ( name , ( ) => {
61+ for ( const ndjsonFile of ndjsonFiles ) {
62+ const [ suiteName ] = path . basename ( ndjsonFile ) . split ( '.' )
63+
64+ it ( suiteName , async ( ) => {
65+ let content = ''
66+ const printer = new SummaryPrinter (
67+ fakeStream ,
68+ ( chunk ) => {
69+ content += chunk
70+ } ,
71+ {
72+ attachments : true ,
73+ featuresAndRules : true ,
74+ theme : CUCUMBER_THEME ,
75+ ...options ,
76+ }
77+ )
78+
79+ await pipeline (
80+ fs . createReadStream ( ndjsonFile , { encoding : 'utf-8' } ) ,
81+ new NdjsonToMessageStream ( ) ,
82+ new Writable ( {
83+ objectMode : true ,
84+ write ( envelope : Envelope , _ : BufferEncoding , callback ) {
85+ printer . update ( envelope )
86+ callback ( )
87+ } ,
88+ } )
89+ )
90+
91+ const expectedOutput = fs . readFileSync (
92+ ndjsonFile . replace ( '.ndjson' , `.${ name } .summary.log` ) ,
93+ {
94+ encoding : 'utf-8' ,
95+ }
96+ )
97+
98+ expect ( content ) . to . eq ( expectedOutput )
99+ } )
100+ }
101+ } )
102+ }
103+ } )
0 commit comments