1- import { readdir , rmSync , writeFileSync } from 'node:fs' ;
1+ import { existsSync , readdir , rmSync , writeFileSync } from 'node:fs' ;
22import { afterAll , afterEach , beforeEach , describe , expect , test , vi } from 'vitest' ;
33
44import { createDir } from '../index' ;
@@ -42,28 +42,43 @@ describe('copyfiles', () => {
4242 // Mock process.exit so it doesn't kill the test runner
4343 // @ts -ignore
4444 const exitSpy = vi . spyOn ( process , 'exit' ) . mockImplementation ( ( code ?: string | number | null | undefined ) => {
45- // Do nothing
45+ if ( code && code !== 0 ) {
46+ exitSpy . mockRestore ( ) ;
47+ done ( new Error ( `process.exit called with code ${ code } ` ) ) ;
48+ }
49+ // Do nothing for code 0
4650 } ) ;
4751
4852 import ( '../cli' )
4953 . then ( ( ) => {
50- // Wait a tick to ensure file writes are complete
51- setTimeout ( ( ) => {
54+ // Wait until output2/input2 exists, then check files
55+ const start = Date . now ( ) ;
56+ const check = ( ) => {
57+ if ( ! existsSync ( 'output2/input2' ) ) {
58+ if ( Date . now ( ) - start > 55 ) {
59+ exitSpy . mockRestore ( ) ;
60+ return done ( new Error ( 'Timeout: output2/input2 was not created' ) ) ;
61+ }
62+ setTimeout ( check , 50 ) ;
63+ return ;
64+ }
5265 readdir ( 'output2/input2' , ( err , files ) => {
53- expect ( files ) . toEqual ( [ 'a.txt' , 'b.txt' ] ) ;
54- exitSpy . mockRestore ( ) ;
55- done ( ) ;
66+ try {
67+ expect ( err ) . toBeNull ( ) ;
68+ expect ( files ) . toEqual ( [ 'a.txt' , 'b.txt' ] ) ;
69+ exitSpy . mockRestore ( ) ;
70+ done ( ) ;
71+ } catch ( e ) {
72+ exitSpy . mockRestore ( ) ;
73+ done ( e ) ;
74+ }
5675 } ) ;
57- } , 100 ) ; // 100ms delay to allow async file writes
76+ } ;
77+ check ( ) ;
5878 } )
5979 . catch ( e => {
60- setTimeout ( ( ) => {
61- readdir ( 'output2/input2' , ( err , files ) => {
62- expect ( files ) . toEqual ( [ 'a.txt' , 'b.txt' ] ) ;
63- exitSpy . mockRestore ( ) ;
64- done ( ) ;
65- } ) ;
66- } , 100 ) ;
80+ exitSpy . mockRestore ( ) ;
81+ done ( e ) ;
6782 } ) ;
68- } ) )
83+ } ) , 250 ) ;
6984} )
0 commit comments