Skip to content

Commit c4cb389

Browse files
authored
fix: update spec filter depending on the mask (#173)
1 parent bd09462 commit c4cb389

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

support.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77
const sendCoverage = (coverage, pathname = '/') => {
88
logMessage(`Saving code coverage for **${pathname}**`)
9+
10+
const appCoverageOnly = filterSpecsFromCoverage(coverage)
911
// stringify coverage object for speed
10-
cy.task('combineCoverage', JSON.stringify(coverage), {
12+
cy.task('combineCoverage', JSON.stringify(appCoverageOnly), {
1113
log: false
1214
})
1315
}
@@ -21,6 +23,30 @@ const logMessage = s => {
2123
cy.log(`${s} \`[@cypress/code-coverage]\``)
2224
}
2325

26+
const filterSpecsFromCoverage = totalCoverage => {
27+
// remove coverage for the spec files themselves,
28+
// only keep "external" application source file coverage
29+
const integrationFolder = Cypress.config('integrationFolder')
30+
const supportFile = Cypress.config('supportFile')
31+
const testFilePattern = Cypress.config('testFiles')
32+
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
33+
34+
const isInIntegrationFolder = filename =>
35+
filename.startsWith(integrationFolder)
36+
const isTestFile = filename => Cypress.minimatch(filename, testFilePattern)
37+
const isSupportFile = filename => filename === supportFile
38+
39+
const isA = (fileCoverge, filename) =>
40+
isInIntegrationFolder(filename) || isSupportFile(filename)
41+
const isB = (fileCoverge, filename) =>
42+
isTestFile(filename) || isSupportFile(filename)
43+
44+
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
45+
46+
const coverage = Cypress._.omitBy(totalCoverage, isTestFileFilter)
47+
return coverage
48+
}
49+
2450
// to disable code coverage commands and save time
2551
// pass environment variable coverage=false
2652
// cypress run --env coverage=false
@@ -141,16 +167,7 @@ if (Cypress.env('coverage') === false) {
141167
// the coverage information only once after all tests have finished
142168
const unitTestCoverage = window.__coverage__
143169
if (unitTestCoverage) {
144-
// remove coverage for the spec files themselves,
145-
// only keep "external" application source file coverage
146-
const supportFile = Cypress.config('supportFile')
147-
const testFilePattern = Cypress.config('testFiles')
148-
149-
const isTestFile = (fileCoverage, filename) =>
150-
Cypress.minimatch(filename, testFilePattern) || filename === supportFile
151-
152-
const coverage = Cypress._.omitBy(window.__coverage__, isTestFile)
153-
sendCoverage(coverage, 'unit')
170+
sendCoverage(unitTestCoverage, 'unit')
154171
}
155172
})
156173

0 commit comments

Comments
 (0)