Skip to content

fix: better filtering of support files #177

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 5 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
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
33 changes: 32 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ workflows:
- run:
command: npx nyc report --check-coverage true --lines 100
working_directory: examples/same-folder
# how to fail if the specific file coverage is not found?!
- run:
command: npx nyc report --check-coverage true --lines 100 --include unit-utils.js
working_directory: examples/same-folder
Expand All @@ -221,6 +220,37 @@ workflows:
node ../../scripts/only-covered main.js unit-utils.js
working_directory: examples/same-folder

- cypress/run:
attach-workspace: true
name: example-support-files
requires:
- cypress/install
# there are no jobs to follow this one
# so no need to save the workspace files (saves time)
no-workspace: true
start: npm start --prefix examples/support-files
wait-on: 'http://localhost:1234'
command: npx cypress run --project examples/support-files
# store screenshots and videos
store_artifacts: true
post-steps:
- run: cat examples/support-files/.nyc_output/out.json
# store the created coverage report folder
# you can click on it in the CircleCI UI
# to see live static HTML site
- store_artifacts:
path: examples/support-files/coverage
# make sure the examples captures 100% of code
- run:
command: npx nyc report --check-coverage true --lines 100
working_directory: examples/support-files
- run:
name: Check code coverage 📈
command: |
node ../../scripts/check-coverage main.js
node ../../scripts/only-covered main.js
working_directory: examples/support-files

- publish:
filters:
branches:
Expand All @@ -235,3 +265,4 @@ workflows:
- example-before-all-visit
- example-ts-example
- example-same-folder
- example-support-files
3 changes: 3 additions & 0 deletions examples/support-files/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["istanbul"]
}
3 changes: 3 additions & 0 deletions examples/support-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example: support-files

Filtering out support files
5 changes: 5 additions & 0 deletions examples/support-files/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fixturesFolder": false,
"pluginsFile": "cypress/plugins/index.js",
"baseUrl": "http://localhost:1234"
}
5 changes: 5 additions & 0 deletions examples/support-files/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="cypress" />
it('works', () => {
cy.visit('/')
cy.contains('Page body')
})
4 changes: 4 additions & 0 deletions examples/support-files/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (on, config) => {
on('task', require('../../../../task'))
on('file:preprocessor', require('../../../../use-babelrc'))
}
2 changes: 2 additions & 0 deletions examples/support-files/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '../../../../support'
console.log('this is commands file')
1 change: 1 addition & 0 deletions examples/support-files/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./commands')
13 changes: 13 additions & 0 deletions examples/support-files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<body>
Page body
<script src="main.js"></script>
<script>
// use functions creates in "main.js"
if (add(2, 3) !== 5) {
throw new Error('wrong addition')
}
if (sub(2, 3) !== -1) {
throw new Error('wrong subtraction')
}
</script>
</body>
3 changes: 3 additions & 0 deletions examples/support-files/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.add = (a, b) => a + b

window.sub = (a, b) => a - b
12 changes: 12 additions & 0 deletions examples/support-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "example-support-files",
"description": "Filtering out support files",
"devDependencies": {
"@babel/core": "7.9.0"
},
"scripts": {
"start": "../../node_modules/.bin/parcel serve index.html",
"cy:open": "../../node_modules/.bin/cypress open",
"dev": "../../node_modules/.bin/start-test 1234 cy:open"
}
}
46 changes: 37 additions & 9 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
const sendCoverage = (coverage, pathname = '/') => {
logMessage(`Saving code coverage for **${pathname}**`)

const appCoverageOnly = filterSpecsFromCoverage(coverage)
const withoutSpecs = filterSpecsFromCoverage(coverage)
const appCoverageOnly = filterSupportFilesFromCoverage(withoutSpecs)

// stringify coverage object for speed
cy.task('combineCoverage', JSON.stringify(appCoverageOnly), {
log: false
Expand All @@ -23,23 +25,49 @@ 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
/**
* Removes support file from the coverage object.
* If there are more files loaded from support folder, also removes them
*/
const filterSupportFilesFromCoverage = totalCoverage => {
const integrationFolder = Cypress.config('integrationFolder')
const supportFile = Cypress.config('supportFile')
const supportFolder = Cypress.config('supportFolder')

const isSupportFile = filename => filename === supportFile

let coverage = Cypress._.omitBy(totalCoverage, (fileCoverage, filename) =>
isSupportFile(filename)
)

// check the edge case
// if we have files from support folder AND the support folder is not same
// as the integration, or its prefix (this might remove all app source files)
// then remove all files from the support folder
if (!integrationFolder.startsWith(supportFolder)) {
// remove all covered files from support folder
coverage = Cypress._.omitBy(totalCoverage, (fileCoverage, filename) =>
filename.startsWith(supportFolder)
)
}
return coverage
}

/**
* remove coverage for the spec files themselves,
* only keep "external" application source file coverage
*/
const filterSpecsFromCoverage = totalCoverage => {
const integrationFolder = Cypress.config('integrationFolder')
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 isA = (fileCoverge, filename) => isInIntegrationFolder(filename)
const isB = (fileCoverge, filename) => isTestFile(filename)

const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB

Expand Down