@@ -16,6 +16,7 @@ let libSourceMaps;
16
16
let CoverageReporter ;
17
17
let istanbulApi ;
18
18
19
+ import { CoverageSummary } from 'istanbul-lib-coverage/lib/file' ;
19
20
import path from 'path' ;
20
21
import mock from 'mock-fs' ;
21
22
@@ -32,6 +33,9 @@ beforeEach(() => {
32
33
33
34
const fileTree = { } ;
34
35
fileTree [ process . cwd ( ) + '/path-test-files' ] = {
36
+ '000pc_coverage_file.js' : '' ,
37
+ '050pc_coverage_file.js' : '' ,
38
+ '100pc_coverage_file.js' : '' ,
35
39
'full_path_file.js' : '' ,
36
40
'glob-path' : {
37
41
'file1.js' : '' ,
@@ -68,37 +72,54 @@ describe('onRunComplete', () => {
68
72
} ;
69
73
70
74
libCoverage . createCoverageMap = jest . fn ( ( ) => {
71
- const files = [
72
- './path-test-files/covered_file_without_threshold.js' ,
73
- './path-test-files/full_path_file.js' ,
74
- './path-test-files/relative_path_file.js' ,
75
- './path-test-files/glob-path/file1.js' ,
76
- './path-test-files/glob-path/file2.js' ,
77
- ] . map ( p => path . resolve ( p ) ) ;
75
+ const covSummary = {
76
+ branches : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
77
+ functions : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
78
+ lines : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
79
+ statements : { covered : 5 , pct : 50 , skipped : 0 , total : 10 } ,
80
+ } ;
81
+ const fileCoverage = [
82
+ [ './path-test-files/covered_file_without_threshold.js' ] ,
83
+ [ './path-test-files/full_path_file.js' ] ,
84
+ [ './path-test-files/relative_path_file.js' ] ,
85
+ [ './path-test-files/glob-path/file1.js' ] ,
86
+ [ './path-test-files/glob-path/file2.js' ] ,
87
+ [
88
+ './path-test-files/000pc_coverage_file.js' ,
89
+ { statements : { covered : 0 , pct : 0 , total : 10 } } ,
90
+ ] ,
91
+ [
92
+ './path-test-files/050pc_coverage_file.js' ,
93
+ { statements : { covered : 5 , pct : 50 , total : 10 } } ,
94
+ ] ,
95
+ [
96
+ './path-test-files/100pc_coverage_file.js' ,
97
+ { statements : { covered : 10 , pct : 100 , total : 10 } } ,
98
+ ] ,
99
+ ] . reduce ( ( c , f ) => {
100
+ const file = path . resolve ( f [ 0 ] ) ;
101
+ const override = f [ 1 ] ;
102
+ c [ file ] = new CoverageSummary ( {
103
+ ...covSummary ,
104
+ ...override ,
105
+ } ) ;
106
+ return c ;
107
+ } , { } ) ;
78
108
79
109
return {
80
110
fileCoverageFor ( path ) {
81
- if ( files . indexOf ( path ) !== - 1 ) {
82
- const covSummary = {
83
- branches : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
84
- functions : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
85
- lines : { covered : 0 , pct : 0 , skipped : 0 , total : 0 } ,
86
- merge ( other ) {
87
- return covSummary ;
88
- } ,
89
- statements : { covered : 0 , pct : 50 , skipped : 0 , total : 0 } ,
90
- } ;
111
+ if ( fileCoverage [ path ] ) {
91
112
return {
92
113
toSummary ( ) {
93
- return covSummary ;
114
+ return fileCoverage [ path ] ;
94
115
} ,
95
116
} ;
96
117
} else {
97
118
return undefined ;
98
119
}
99
120
} ,
100
121
files ( ) {
101
- return files ;
122
+ return Object . keys ( fileCoverage ) ;
102
123
} ,
103
124
} ;
104
125
} ) ;
@@ -281,4 +302,125 @@ describe('onRunComplete', () => {
281
302
expect ( testReporter . getLastError ( ) . message . split ( '\n' ) ) . toHaveLength ( 1 ) ;
282
303
} ) ;
283
304
} ) ;
305
+
306
+ test ( `getLastError() returns 'undefined' when global threshold group
307
+ is empty because PATH and GLOB threshold groups have matched all the
308
+ files in the coverage data.` , ( ) => {
309
+ const testReporter = new CoverageReporter (
310
+ {
311
+ collectCoverage : true ,
312
+ coverageThreshold : {
313
+ './path-test-files/' : {
314
+ statements : 50 ,
315
+ } ,
316
+ global : {
317
+ statements : 100 ,
318
+ } ,
319
+ } ,
320
+ } ,
321
+ {
322
+ maxWorkers : 2 ,
323
+ } ,
324
+ ) ;
325
+ testReporter . log = jest . fn ( ) ;
326
+ return testReporter
327
+ . onRunComplete ( new Set ( ) , { } , mockAggResults )
328
+ . then ( ( ) => {
329
+ expect ( testReporter . getLastError ( ) ) . toBeUndefined ( ) ;
330
+ } ) ;
331
+ } ) ;
332
+
333
+ test ( `getLastError() returns 'undefined' when file and directory path
334
+ threshold groups overlap` , ( ) => {
335
+ const covThreshold = { } ;
336
+ [
337
+ './path-test-files/' ,
338
+ './path-test-files/covered_file_without_threshold.js' ,
339
+ './path-test-files/full_path_file.js' ,
340
+ './path-test-files/relative_path_file.js' ,
341
+ './path-test-files/glob-path/file1.js' ,
342
+ './path-test-files/glob-path/file2.js' ,
343
+ './path-test-files/*.js' ,
344
+ ] . forEach ( path => {
345
+ covThreshold [ path ] = {
346
+ statements : 0 ,
347
+ } ;
348
+ } ) ;
349
+
350
+ const testReporter = new CoverageReporter (
351
+ {
352
+ collectCoverage : true ,
353
+ coverageThreshold : covThreshold ,
354
+ } ,
355
+ {
356
+ maxWorkers : 2 ,
357
+ } ,
358
+ ) ;
359
+ testReporter . log = jest . fn ( ) ;
360
+ return testReporter
361
+ . onRunComplete ( new Set ( ) , { } , mockAggResults )
362
+ . then ( ( ) => {
363
+ expect ( testReporter . getLastError ( ) ) . toBeUndefined ( ) ;
364
+ } ) ;
365
+ } ) ;
366
+
367
+ test ( `that if globs or paths are specified alongside global, coverage
368
+ data for matching paths will be subtracted from overall coverage
369
+ and thresholds will be applied independently` , ( ) => {
370
+ const testReporter = new CoverageReporter (
371
+ {
372
+ collectCoverage : true ,
373
+ coverageThreshold : {
374
+ './path-test-files/100pc_coverage_file.js' : {
375
+ statements : 100 ,
376
+ } ,
377
+ global : {
378
+ statements : 50 ,
379
+ } ,
380
+ } ,
381
+ } ,
382
+ {
383
+ maxWorkers : 2 ,
384
+ } ,
385
+ ) ;
386
+ testReporter . log = jest . fn ( ) ;
387
+ // 100% coverage file is removed from overall coverage so
388
+ // coverage drops to < 50%
389
+ return testReporter
390
+ . onRunComplete ( new Set ( ) , { } , mockAggResults )
391
+ . then ( ( ) => {
392
+ expect ( testReporter . getLastError ( ) . message . split ( '\n' ) ) . toHaveLength ( 1 ) ;
393
+ } ) ;
394
+ } ) ;
395
+
396
+ test ( `that files are matched by all matching threshold groups` , ( ) => {
397
+ const testReporter = new CoverageReporter (
398
+ {
399
+ collectCoverage : true ,
400
+ coverageThreshold : {
401
+ './path-test-files/' : {
402
+ statements : 50 ,
403
+ } ,
404
+ './path-test-files/050pc_coverage_file.js' : {
405
+ statements : 50 ,
406
+ } ,
407
+ './path-test-files/100pc_coverage_*.js' : {
408
+ statements : 100 ,
409
+ } ,
410
+ './path-test-files/100pc_coverage_file.js' : {
411
+ statements : 100 ,
412
+ } ,
413
+ } ,
414
+ } ,
415
+ {
416
+ maxWorkers : 2 ,
417
+ } ,
418
+ ) ;
419
+ testReporter . log = jest . fn ( ) ;
420
+ return testReporter
421
+ . onRunComplete ( new Set ( ) , { } , mockAggResults )
422
+ . then ( ( ) => {
423
+ expect ( testReporter . getLastError ( ) ) . toBeUndefined ( ) ;
424
+ } ) ;
425
+ } ) ;
284
426
} ) ;
0 commit comments