1
- import { describe , expect , it } from 'bun:test'
1
+ import { describe , expect , it , beforeEach , afterEach , spyOn } from 'bun:test'
2
2
import {
3
3
generateComposerUpdates ,
4
4
isComposerFile ,
@@ -227,39 +227,36 @@ describe('Composer Parser', () => {
227
227
} ,
228
228
} , null , 2 )
229
229
230
- // We'll mock fs.readFileSync inline in each test for better control
231
-
232
230
it ( 'should generate updates for composer.json files' , async ( ) => {
233
- // Create a mock composer.json file for this test
231
+ // Mock fs readFileSync using spyOn
234
232
const fs = await import ( 'node:fs' )
235
- const path = await import ( 'node:path' )
236
- const testComposerPath = path . join ( process . cwd ( ) , 'test-updates-composer.json' )
233
+ const readFileSpy = spyOn ( fs , 'readFileSync' )
237
234
238
- // Write test file
239
- fs . writeFileSync ( testComposerPath , mockComposerContent )
235
+ readFileSpy . mockImplementation ( ( ( filePath : any ) => {
236
+ if ( String ( filePath ) . endsWith ( 'composer.json' ) ) {
237
+ return mockComposerContent
238
+ }
239
+ throw new Error ( `File not found: ${ filePath } ` )
240
+ } ) as any )
240
241
241
242
try {
242
243
const updates = [
243
- { name : 'laravel/framework' , newVersion : '10.16.0' , file : 'test-updates- composer.json' } ,
244
- { name : 'phpunit/phpunit' , newVersion : '10.3.0' , file : 'test-updates- composer.json' } ,
244
+ { name : 'laravel/framework' , newVersion : '10.16.0' , file : 'composer.json' } ,
245
+ { name : 'phpunit/phpunit' , newVersion : '10.3.0' , file : 'composer.json' } ,
245
246
]
246
247
247
248
const result = await generateComposerUpdates ( updates )
248
249
249
250
expect ( result ) . toHaveLength ( 1 )
250
- expect ( result [ 0 ] . path ) . toBe ( 'test-updates- composer.json' )
251
+ expect ( result [ 0 ] . path ) . toBe ( 'composer.json' )
251
252
expect ( result [ 0 ] . type ) . toBe ( 'update' )
252
253
253
254
const updatedContent = result [ 0 ] . content
254
255
expect ( updatedContent ) . toContain ( '^10.16.0' ) // laravel/framework updated
255
256
expect ( updatedContent ) . toContain ( '^10.3.0' ) // phpunit/phpunit updated
256
257
expect ( updatedContent ) . toContain ( '^6.0.0' ) // symfony/console unchanged
257
- }
258
- finally {
259
- // Clean up test file
260
- if ( fs . existsSync ( testComposerPath ) ) {
261
- fs . unlinkSync ( testComposerPath )
262
- }
258
+ } finally {
259
+ readFileSpy . mockRestore ( )
263
260
}
264
261
} )
265
262
@@ -271,18 +268,21 @@ describe('Composer Parser', () => {
271
268
} ,
272
269
} , null , 2 )
273
270
274
- // Create a mock composer.json file for this test
271
+ // Mock fs readFileSync using spyOn
275
272
const fs = await import ( 'node:fs' )
276
- const path = await import ( 'node:path' )
277
- const testComposerPath = path . join ( process . cwd ( ) , 'test-constraints-composer.json' )
273
+ const readFileSpy = spyOn ( fs , 'readFileSync' )
278
274
279
- // Write test file
280
- fs . writeFileSync ( testComposerPath , constraintComposerContent )
275
+ readFileSpy . mockImplementation ( ( ( filePath : any ) => {
276
+ if ( String ( filePath ) . endsWith ( 'composer.json' ) ) {
277
+ return constraintComposerContent
278
+ }
279
+ throw new Error ( `File not found: ${ filePath } ` )
280
+ } ) as any )
281
281
282
282
try {
283
283
const updates = [
284
- { name : 'laravel/framework' , newVersion : '10.16.0' , file : 'test-constraints- composer.json' } ,
285
- { name : 'symfony/console' , newVersion : '6.3.0' , file : 'test-constraints- composer.json' } ,
284
+ { name : 'laravel/framework' , newVersion : '10.16.0' , file : 'composer.json' } ,
285
+ { name : 'symfony/console' , newVersion : '6.3.0' , file : 'composer.json' } ,
286
286
]
287
287
288
288
const result = await generateComposerUpdates ( updates )
@@ -292,12 +292,8 @@ describe('Composer Parser', () => {
292
292
293
293
expect ( updatedContent ) . toContain ( '~10.16.0' ) // Preserves ~ constraint
294
294
expect ( updatedContent ) . toContain ( '>=6.3.0,<7.0' ) // Preserves complex constraint
295
- }
296
- finally {
297
- // Clean up test file
298
- if ( fs . existsSync ( testComposerPath ) ) {
299
- fs . unlinkSync ( testComposerPath )
300
- }
295
+ } finally {
296
+ readFileSpy . mockRestore ( )
301
297
}
302
298
} )
303
299
@@ -316,17 +312,20 @@ describe('Composer Parser', () => {
316
312
} )
317
313
318
314
it ( 'should handle missing packages gracefully' , async ( ) => {
319
- // Create a mock composer.json file for this test
315
+ // Mock fs readFileSync using spyOn
320
316
const fs = await import ( 'node:fs' )
321
- const path = await import ( 'node:path' )
322
- const testComposerPath = path . join ( process . cwd ( ) , 'test-missing-composer.json' )
317
+ const readFileSpy = spyOn ( fs , 'readFileSync' )
323
318
324
- // Write test file
325
- fs . writeFileSync ( testComposerPath , mockComposerContent )
319
+ readFileSpy . mockImplementation ( ( ( filePath : any ) => {
320
+ if ( String ( filePath ) . endsWith ( 'composer.json' ) ) {
321
+ return mockComposerContent
322
+ }
323
+ throw new Error ( `File not found: ${ filePath } ` )
324
+ } ) as any )
326
325
327
326
try {
328
327
const updates = [
329
- { name : 'non-existent/package' , newVersion : '1.0.0' , file : 'test-missing- composer.json' } ,
328
+ { name : 'non-existent/package' , newVersion : '1.0.0' , file : 'composer.json' } ,
330
329
]
331
330
332
331
const result = await generateComposerUpdates ( updates )
@@ -336,12 +335,8 @@ describe('Composer Parser', () => {
336
335
const updatedContent = result [ 0 ] . content
337
336
expect ( updatedContent ) . toContain ( '"laravel/framework": "^10.0.0"' ) // Original content preserved
338
337
expect ( updatedContent ) . not . toContain ( 'non-existent/package' )
339
- }
340
- finally {
341
- // Clean up test file
342
- if ( fs . existsSync ( testComposerPath ) ) {
343
- fs . unlinkSync ( testComposerPath )
344
- }
338
+ } finally {
339
+ readFileSpy . mockRestore ( )
345
340
}
346
341
} )
347
342
} )
0 commit comments