@@ -31,6 +31,22 @@ const UNKNOWN_FILE_SECONDS = 6
3131const MIN_FILE_SECONDS = 1
3232const DURATIONS_FILE = 'vitest.integration-durations.json'
3333
34+ /**
35+ * The committed weights, or none when the file is absent. A file that does not parse stops the run
36+ * with the fix in the message rather than a bare SyntaxError.
37+ */
38+ function readDurations ( file : string ) : Record < string , unknown > {
39+ if ( ! existsSync ( file ) ) return { }
40+ try {
41+ return JSON . parse ( readFileSync ( file , 'utf8' ) )
42+ } catch ( error ) {
43+ throw new Error (
44+ `${ DURATIONS_FILE } is not valid JSON. Regenerate it with scripts/update-integration-durations.ts.` ,
45+ { cause : error }
46+ )
47+ }
48+ }
49+
3450function findRepoRoot ( from : string ) : string {
3551 let dir = from
3652 while ( ! existsSync ( path . join ( dir , 'bun.lock' ) ) ) {
@@ -58,18 +74,14 @@ class DurationBalancedSequencer extends BaseSequencer {
5874 override async shard ( files : TestSpecification [ ] ) : Promise < TestSpecification [ ] > {
5975 const { index, count } = this . ctx . config . shard ?? { index : 1 , count : 1 }
6076 const root = findRepoRoot ( this . ctx . config . root )
61- const durationsPath = path . join ( root , DURATIONS_FILE )
62- const durations : Record < string , number > = existsSync ( durationsPath )
63- ? JSON . parse ( readFileSync ( durationsPath , 'utf8' ) )
64- : { }
77+ const durations = readDurations ( path . join ( root , DURATIONS_FILE ) )
6578
6679 const weighted = files . map ( ( spec ) => {
6780 const key = path . relative ( root , spec . moduleId ) . split ( path . sep ) . join ( '/' )
68- return {
69- spec,
70- key,
71- seconds : Math . max ( durations [ key ] ?? UNKNOWN_FILE_SECONDS , MIN_FILE_SECONDS ) ,
72- }
81+ const recorded = durations [ key ]
82+ const seconds =
83+ typeof recorded === 'number' && Number . isFinite ( recorded ) ? recorded : UNKNOWN_FILE_SECONDS
84+ return { spec, key, seconds : Math . max ( seconds , MIN_FILE_SECONDS ) }
7385 } )
7486 weighted . sort ( ( a , b ) => b . seconds - a . seconds || ( a . key < b . key ? - 1 : a . key > b . key ? 1 : 0 ) )
7587
0 commit comments