55
66import { Writable } from 'node:stream'
77
8- import { beforeEach , describe , expect , it , vi } from 'vitest'
8+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
99
1010import { FileProgress , MultiProgress , Spinner } from './rich-progress.mts'
1111
@@ -37,10 +37,16 @@ describe('rich-progress', () => {
3737 let progress : MultiProgress
3838
3939 beforeEach ( ( ) => {
40+ vi . useFakeTimers ( )
4041 mockStream = new MockWritable ( )
4142 progress = new MultiProgress ( { stream : mockStream , hideCursor : false } )
4243 } )
4344
45+ afterEach ( ( ) => {
46+ vi . restoreAllMocks ( )
47+ vi . useRealTimers ( )
48+ } )
49+
4450 it ( 'should create instance with default options' , ( ) => {
4551 const defaultProgress = new MultiProgress ( )
4652 expect ( defaultProgress ) . toBeInstanceOf ( MultiProgress )
@@ -54,6 +60,7 @@ describe('rich-progress', () => {
5460 it ( 'should update task progress' , ( ) => {
5561 progress . addTask ( 'task1' , 'Processing files' , 100 )
5662 progress . updateTask ( 'task1' , 50 )
63+ vi . advanceTimersByTime ( 100 ) // Trigger render
5764 progress . stop ( )
5865
5966 const output = mockStream . getOutput ( )
@@ -64,6 +71,7 @@ describe('rich-progress', () => {
6471 it ( 'should mark task as done when reaching total' , ( ) => {
6572 progress . addTask ( 'task1' , 'Processing files' , 100 )
6673 progress . updateTask ( 'task1' , 100 )
74+ vi . advanceTimersByTime ( 100 ) // Trigger render
6775 progress . stop ( )
6876
6977 const output = mockStream . getOutput ( )
@@ -74,6 +82,7 @@ describe('rich-progress', () => {
7482 it ( 'should handle task failure' , ( ) => {
7583 progress . addTask ( 'task1' , 'Processing files' , 100 )
7684 progress . failTask ( 'task1' , 'Error occurred' )
85+ vi . advanceTimersByTime ( 100 ) // Trigger render
7786 progress . stop ( )
7887
7988 const output = mockStream . getOutput ( )
@@ -86,6 +95,7 @@ describe('rich-progress', () => {
8695 progress . addTask ( 'task2' , 'Task 2' , 75 )
8796 progress . updateTask ( 'task1' , 25 )
8897 progress . updateTask ( 'task2' , 50 )
98+ vi . advanceTimersByTime ( 100 ) // Trigger render
8999 progress . stop ( )
90100
91101 const output = mockStream . getOutput ( )
@@ -96,6 +106,7 @@ describe('rich-progress', () => {
96106 it ( 'should update task with custom tokens' , ( ) => {
97107 progress . addTask ( 'task1' , 'Download' , 100 )
98108 progress . updateTask ( 'task1' , 50 , { file : 'package.json' } )
109+ vi . advanceTimersByTime ( 100 ) // Trigger render
99110 progress . stop ( )
100111
101112 const output = mockStream . getOutput ( )
@@ -124,6 +135,7 @@ describe('rich-progress', () => {
124135 it ( 'should calculate progress percentage correctly' , ( ) => {
125136 progress . addTask ( 'task1' , 'Test' , 200 )
126137 progress . updateTask ( 'task1' , 50 )
138+ vi . advanceTimersByTime ( 100 ) // Trigger render
127139 progress . stop ( )
128140
129141 const output = mockStream . getOutput ( )
@@ -133,6 +145,7 @@ describe('rich-progress', () => {
133145 it ( 'should handle zero total gracefully' , ( ) => {
134146 progress . addTask ( 'task1' , 'Test' , 0 )
135147 progress . updateTask ( 'task1' , 0 )
148+ vi . advanceTimersByTime ( 100 ) // Trigger render
136149 progress . stop ( )
137150
138151 const output = mockStream . getOutput ( )
@@ -143,11 +156,8 @@ describe('rich-progress', () => {
143156 it ( 'should display elapsed time' , ( ) => {
144157 progress . addTask ( 'task1' , 'Test' , 100 )
145158 progress . updateTask ( 'task1' , 50 )
146- // Wait a bit for time to elapse
147- const start = Date . now ( )
148- while ( Date . now ( ) - start < 50 ) {
149- // Busy wait
150- }
159+ // Advance time to simulate elapsed time
160+ vi . advanceTimersByTime ( 100 ) // Trigger render
151161 progress . stop ( )
152162
153163 const output = mockStream . getOutput ( )
@@ -157,6 +167,7 @@ describe('rich-progress', () => {
157167 it ( 'should render progress bar with correct fill' , ( ) => {
158168 progress . addTask ( 'task1' , 'Test' , 100 )
159169 progress . updateTask ( 'task1' , 50 )
170+ vi . advanceTimersByTime ( 100 ) // Trigger render
160171 progress . stop ( )
161172
162173 const output = mockStream . getOutput ( )
@@ -167,24 +178,28 @@ describe('rich-progress', () => {
167178 it ( 'should display status symbols' , ( ) => {
168179 const p1 = new MultiProgress ( { stream : mockStream } )
169180 p1 . addTask ( 'pending' , 'Pending' , 100 )
181+ vi . advanceTimersByTime ( 100 ) // Trigger render
170182 p1 . stop ( )
171183 mockStream . clear ( )
172184
173185 const p2 = new MultiProgress ( { stream : mockStream } )
174186 p2 . addTask ( 'running' , 'Running' , 100 )
175187 p2 . updateTask ( 'running' , 50 )
188+ vi . advanceTimersByTime ( 100 ) // Trigger render
176189 p2 . stop ( )
177190 mockStream . clear ( )
178191
179192 const p3 = new MultiProgress ( { stream : mockStream } )
180193 p3 . addTask ( 'done' , 'Done' , 100 )
181194 p3 . updateTask ( 'done' , 100 )
195+ vi . advanceTimersByTime ( 100 ) // Trigger render
182196 p3 . stop ( )
183197 mockStream . clear ( )
184198
185199 const p4 = new MultiProgress ( { stream : mockStream } )
186200 p4 . addTask ( 'failed' , 'Failed' , 100 )
187201 p4 . failTask ( 'failed' )
202+ vi . advanceTimersByTime ( 100 ) // Trigger render
188203 p4 . stop ( )
189204
190205 // At least one output should contain status symbols
@@ -436,20 +451,29 @@ describe('rich-progress', () => {
436451 } )
437452
438453 describe ( 'CI and VITEST mode' , ( ) => {
454+ beforeEach ( ( ) => {
455+ vi . useFakeTimers ( )
456+ } )
457+
458+ afterEach ( ( ) => {
459+ vi . restoreAllMocks ( )
460+ vi . useRealTimers ( )
461+ } )
462+
439463 it ( 'should handle progress indicators in test environment' , ( ) => {
440464 // Progress indicators should work in test mode but not display animations
441465 const mockStream = new MockWritable ( )
442466 const progress = new MultiProgress ( { stream : mockStream } )
443467 progress . addTask ( 'test' , 'Test task' , 100 )
444468 progress . updateTask ( 'test' , 50 )
469+ vi . advanceTimersByTime ( 100 ) // Trigger render
445470 progress . stop ( )
446471
447472 // Should not throw and should produce output
448473 expect ( mockStream . getOutput ( ) ) . toBeTruthy ( )
449474 } )
450475
451476 it ( 'should handle spinners in test environment' , ( ) => {
452- vi . useFakeTimers ( )
453477 const mockStream = new MockWritable ( )
454478 const spinner = new Spinner ( 'Test' , mockStream )
455479 spinner . start ( )
@@ -458,7 +482,6 @@ describe('rich-progress', () => {
458482
459483 // Should not throw
460484 expect ( mockStream . getOutput ( ) ) . toBeTruthy ( )
461- vi . restoreAllMocks ( )
462485 } )
463486 } )
464487} )
0 commit comments