@@ -10,25 +10,30 @@ export class BaseCoverageProvider {
10
10
/**
11
11
* Check if current coverage is above configured thresholds and bump the thresholds if needed
12
12
*/
13
- updateThresholds ( { configurationFile, coverageMap, thresholds } : {
13
+ updateThresholds ( { configurationFile, coverageMap, thresholds, perFile } : {
14
14
coverageMap : CoverageMap
15
15
thresholds : Record < Threshold , number | undefined >
16
+ perFile ?: boolean
16
17
configurationFile ?: string
17
18
} ) {
18
19
// Thresholds cannot be updated if there is no configuration file and
19
20
// feature was enabled by CLI, e.g. --coverage.thresholdAutoUpdate
20
21
if ( ! configurationFile )
21
22
throw new Error ( 'Missing configurationFile. The "coverage.thresholdAutoUpdate" can only be enabled when configuration file is used.' )
22
23
23
- const summary = coverageMap . getCoverageSummary ( )
24
- const thresholdsToUpdate : Threshold [ ] = [ ]
24
+ const summaries = perFile
25
+ ? coverageMap . files ( )
26
+ . map ( ( file : string ) => coverageMap . fileCoverageFor ( file ) . toSummary ( ) )
27
+ : [ coverageMap . getCoverageSummary ( ) ]
28
+
29
+ const thresholdsToUpdate : [ Threshold , number ] [ ] = [ ]
25
30
26
31
for ( const key of THRESHOLD_KEYS ) {
27
32
const threshold = thresholds [ key ] || 100
28
- const actual = summary [ key ] . pct
33
+ const actual = Math . min ( ... summaries . map ( summary => summary [ key ] . pct ) )
29
34
30
35
if ( actual > threshold )
31
- thresholdsToUpdate . push ( key )
36
+ thresholdsToUpdate . push ( [ key , actual ] )
32
37
}
33
38
34
39
if ( thresholdsToUpdate . length === 0 )
@@ -37,14 +42,14 @@ export class BaseCoverageProvider {
37
42
const originalConfig = readFileSync ( configurationFile , 'utf8' )
38
43
let updatedConfig = originalConfig
39
44
40
- for ( const threshold of thresholdsToUpdate ) {
45
+ for ( const [ threshold , newValue ] of thresholdsToUpdate ) {
41
46
// Find the exact match from the configuration file and replace the value
42
47
const previousThreshold = ( thresholds [ threshold ] || 100 ) . toString ( )
43
48
const pattern = new RegExp ( `(${ threshold } \\s*:\\s*)${ previousThreshold . replace ( '.' , '\\.' ) } ` )
44
49
const matches = originalConfig . match ( pattern )
45
50
46
51
if ( matches )
47
- updatedConfig = updatedConfig . replace ( matches [ 0 ] , matches [ 1 ] + summary [ threshold ] . pct )
52
+ updatedConfig = updatedConfig . replace ( matches [ 0 ] , matches [ 1 ] + newValue )
48
53
else
49
54
console . error ( `Unable to update coverage threshold ${ threshold } . No threshold found using pattern ${ pattern } ` )
50
55
}
0 commit comments