Skip to content

fix: update spec filter depending on the mask #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/
const sendCoverage = (coverage, pathname = '/') => {
logMessage(`Saving code coverage for **${pathname}**`)

const appCoverageOnly = filterSpecsFromCoverage(coverage)
// stringify coverage object for speed
cy.task('combineCoverage', JSON.stringify(coverage), {
cy.task('combineCoverage', JSON.stringify(appCoverageOnly), {
log: false
})
}
Expand All @@ -21,6 +23,30 @@ const logMessage = s => {
cy.log(`${s} \`[@cypress/code-coverage]\``)
}

const filterSpecsFromCoverage = totalCoverage => {
// remove coverage for the spec files themselves,
// only keep "external" application source file coverage
const integrationFolder = Cypress.config('integrationFolder')
const supportFile = Cypress.config('supportFile')
const testFilePattern = Cypress.config('testFiles')
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'

const isInIntegrationFolder = filename =>
filename.startsWith(integrationFolder)
const isTestFile = filename => Cypress.minimatch(filename, testFilePattern)
const isSupportFile = filename => filename === supportFile

const isA = (fileCoverge, filename) =>
isInIntegrationFolder(filename) || isSupportFile(filename)
const isB = (fileCoverge, filename) =>
isTestFile(filename) || isSupportFile(filename)

const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB

const coverage = Cypress._.omitBy(totalCoverage, isTestFileFilter)
return coverage
}

// to disable code coverage commands and save time
// pass environment variable coverage=false
// cypress run --env coverage=false
Expand Down Expand Up @@ -141,16 +167,7 @@ if (Cypress.env('coverage') === false) {
// the coverage information only once after all tests have finished
const unitTestCoverage = window.__coverage__
if (unitTestCoverage) {
// remove coverage for the spec files themselves,
// only keep "external" application source file coverage
const supportFile = Cypress.config('supportFile')
const testFilePattern = Cypress.config('testFiles')

const isTestFile = (fileCoverage, filename) =>
Cypress.minimatch(filename, testFilePattern) || filename === supportFile

const coverage = Cypress._.omitBy(window.__coverage__, isTestFile)
sendCoverage(coverage, 'unit')
sendCoverage(unitTestCoverage, 'unit')
}
})

Expand Down