@@ -4,6 +4,7 @@ import type { AfterSuiteRunMeta, CoverageIstanbulOptions, CoverageProvider, Repo
4
4
import { coverageConfigDefaults , defaultExclude , defaultInclude } from 'vitest/config'
5
5
import { BaseCoverageProvider } from 'vitest/coverage'
6
6
import c from 'picocolors'
7
+ import type { ProxifiedModule } from 'magicast'
7
8
import { parseModule } from 'magicast'
8
9
import createDebug from 'debug'
9
10
import libReport from 'istanbul-lib-report'
@@ -238,9 +239,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
238
239
perFile : this . options . thresholds . perFile ,
239
240
configurationFile : {
240
241
write : ( ) => writeFileSync ( configFilePath , configModule . generate ( ) . code , 'utf-8' ) ,
241
- read : ( ) => configModule . exports . default . $type === 'function-call'
242
- ? configModule . exports . default . $args [ 0 ]
243
- : configModule . exports . default ,
242
+ read : ( ) => resolveConfig ( configModule ) ,
244
243
} ,
245
244
} )
246
245
}
@@ -360,3 +359,29 @@ function toSlices<T>(array: T[], size: number): T[][] {
360
359
return chunks
361
360
} , [ ] )
362
361
}
362
+
363
+ function resolveConfig ( configModule : ProxifiedModule < any > ) {
364
+ const mod = configModule . exports . default
365
+
366
+ try {
367
+ // Check for "export default { test: {...} }"
368
+ if ( mod . $type === 'object' )
369
+ return mod
370
+
371
+ if ( mod . $type === 'function-call' ) {
372
+ // "export default defineConfig({ test: {...} })"
373
+ if ( mod . $args [ 0 ] . $type === 'object' )
374
+ return mod . $args [ 0 ]
375
+
376
+ // "export default defineConfig(() => ({ test: {...} }))"
377
+ if ( mod . $args [ 0 ] . $type === 'arrow-function-expression' && mod . $args [ 0 ] . $body . $type === 'object' )
378
+ return mod . $args [ 0 ] . $body
379
+ }
380
+ }
381
+ catch ( error ) {
382
+ // Reduce magicast's verbose errors to readable ones
383
+ throw new Error ( error instanceof Error ? error . message : String ( error ) )
384
+ }
385
+
386
+ throw new Error ( 'Failed to update coverage thresholds. Configuration file is too complex.' )
387
+ }
0 commit comments