7
7
const sendCoverage = ( coverage , pathname = '/' ) => {
8
8
logMessage ( `Saving code coverage for **${ pathname } **` )
9
9
10
- const appCoverageOnly = filterSpecsFromCoverage ( coverage )
10
+ const withoutSpecs = filterSpecsFromCoverage ( coverage )
11
+ const appCoverageOnly = filterSupportFilesFromCoverage ( withoutSpecs )
12
+
11
13
// stringify coverage object for speed
12
14
cy . task ( 'combineCoverage' , JSON . stringify ( appCoverageOnly ) , {
13
15
log : false
@@ -23,23 +25,49 @@ const logMessage = s => {
23
25
cy . log ( `${ s } \`[@cypress/code-coverage]\`` )
24
26
}
25
27
26
- const filterSpecsFromCoverage = totalCoverage => {
27
- // remove coverage for the spec files themselves,
28
- // only keep "external" application source file coverage
28
+ /**
29
+ * Removes support file from the coverage object.
30
+ * If there are more files loaded from support folder, also removes them
31
+ */
32
+ const filterSupportFilesFromCoverage = totalCoverage => {
29
33
const integrationFolder = Cypress . config ( 'integrationFolder' )
30
34
const supportFile = Cypress . config ( 'supportFile' )
35
+ const supportFolder = Cypress . config ( 'supportFolder' )
36
+
37
+ const isSupportFile = filename => filename === supportFile
38
+
39
+ let coverage = Cypress . _ . omitBy ( totalCoverage , ( fileCoverage , filename ) =>
40
+ isSupportFile ( filename )
41
+ )
42
+
43
+ // check the edge case
44
+ // if we have files from support folder AND the support folder is not same
45
+ // as the integration, or its prefix (this might remove all app source files)
46
+ // then remove all files from the support folder
47
+ if ( ! integrationFolder . startsWith ( supportFolder ) ) {
48
+ // remove all covered files from support folder
49
+ coverage = Cypress . _ . omitBy ( totalCoverage , ( fileCoverage , filename ) =>
50
+ filename . startsWith ( supportFolder )
51
+ )
52
+ }
53
+ return coverage
54
+ }
55
+
56
+ /**
57
+ * remove coverage for the spec files themselves,
58
+ * only keep "external" application source file coverage
59
+ */
60
+ const filterSpecsFromCoverage = totalCoverage => {
61
+ const integrationFolder = Cypress . config ( 'integrationFolder' )
31
62
const testFilePattern = Cypress . config ( 'testFiles' )
32
63
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
33
64
34
65
const isInIntegrationFolder = filename =>
35
66
filename . startsWith ( integrationFolder )
36
67
const isTestFile = filename => Cypress . minimatch ( filename , testFilePattern )
37
- const isSupportFile = filename => filename === supportFile
38
68
39
- const isA = ( fileCoverge , filename ) =>
40
- isInIntegrationFolder ( filename ) || isSupportFile ( filename )
41
- const isB = ( fileCoverge , filename ) =>
42
- isTestFile ( filename ) || isSupportFile ( filename )
69
+ const isA = ( fileCoverge , filename ) => isInIntegrationFolder ( filename )
70
+ const isB = ( fileCoverge , filename ) => isTestFile ( filename )
43
71
44
72
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
45
73
0 commit comments