Skip to content

Commit ce34d61

Browse files
authoredApr 5, 2020
fix: better filtering of support files (#177)
* add filtering example with support files * working example * run example job on CI * start the server before testing * update support file filtering
1 parent fb90299 commit ce34d61

File tree

12 files changed

+120
-10
lines changed

12 files changed

+120
-10
lines changed
 

‎.circleci/config.yml

+32-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ workflows:
209209
- run:
210210
command: npx nyc report --check-coverage true --lines 100
211211
working_directory: examples/same-folder
212-
# how to fail if the specific file coverage is not found?!
213212
- run:
214213
command: npx nyc report --check-coverage true --lines 100 --include unit-utils.js
215214
working_directory: examples/same-folder
@@ -221,6 +220,37 @@ workflows:
221220
node ../../scripts/only-covered main.js unit-utils.js
222221
working_directory: examples/same-folder
223222

223+
- cypress/run:
224+
attach-workspace: true
225+
name: example-support-files
226+
requires:
227+
- cypress/install
228+
# there are no jobs to follow this one
229+
# so no need to save the workspace files (saves time)
230+
no-workspace: true
231+
start: npm start --prefix examples/support-files
232+
wait-on: 'http://localhost:1234'
233+
command: npx cypress run --project examples/support-files
234+
# store screenshots and videos
235+
store_artifacts: true
236+
post-steps:
237+
- run: cat examples/support-files/.nyc_output/out.json
238+
# store the created coverage report folder
239+
# you can click on it in the CircleCI UI
240+
# to see live static HTML site
241+
- store_artifacts:
242+
path: examples/support-files/coverage
243+
# make sure the examples captures 100% of code
244+
- run:
245+
command: npx nyc report --check-coverage true --lines 100
246+
working_directory: examples/support-files
247+
- run:
248+
name: Check code coverage 📈
249+
command: |
250+
node ../../scripts/check-coverage main.js
251+
node ../../scripts/only-covered main.js
252+
working_directory: examples/support-files
253+
224254
- publish:
225255
filters:
226256
branches:
@@ -235,3 +265,4 @@ workflows:
235265
- example-before-all-visit
236266
- example-ts-example
237267
- example-same-folder
268+
- example-support-files

‎examples/support-files/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["istanbul"]
3+
}

‎examples/support-files/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# example: support-files
2+
3+
Filtering out support files

‎examples/support-files/cypress.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"fixturesFolder": false,
3+
"pluginsFile": "cypress/plugins/index.js",
4+
"baseUrl": "http://localhost:1234"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="cypress" />
2+
it('works', () => {
3+
cy.visit('/')
4+
cy.contains('Page body')
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = (on, config) => {
2+
on('task', require('../../../../task'))
3+
on('file:preprocessor', require('../../../../use-babelrc'))
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../../../../support'
2+
console.log('this is commands file')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./commands')

‎examples/support-files/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<body>
2+
Page body
3+
<script src="main.js"></script>
4+
<script>
5+
// use functions creates in "main.js"
6+
if (add(2, 3) !== 5) {
7+
throw new Error('wrong addition')
8+
}
9+
if (sub(2, 3) !== -1) {
10+
throw new Error('wrong subtraction')
11+
}
12+
</script>
13+
</body>

‎examples/support-files/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.add = (a, b) => a + b
2+
3+
window.sub = (a, b) => a - b

‎examples/support-files/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "example-support-files",
3+
"description": "Filtering out support files",
4+
"devDependencies": {
5+
"@babel/core": "7.9.0"
6+
},
7+
"scripts": {
8+
"start": "../../node_modules/.bin/parcel serve index.html",
9+
"cy:open": "../../node_modules/.bin/cypress open",
10+
"dev": "../../node_modules/.bin/start-test 1234 cy:open"
11+
}
12+
}

‎support.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
const sendCoverage = (coverage, pathname = '/') => {
88
logMessage(`Saving code coverage for **${pathname}**`)
99

10-
const appCoverageOnly = filterSpecsFromCoverage(coverage)
10+
const withoutSpecs = filterSpecsFromCoverage(coverage)
11+
const appCoverageOnly = filterSupportFilesFromCoverage(withoutSpecs)
12+
1113
// stringify coverage object for speed
1214
cy.task('combineCoverage', JSON.stringify(appCoverageOnly), {
1315
log: false
@@ -23,23 +25,49 @@ const logMessage = s => {
2325
cy.log(`${s} \`[@cypress/code-coverage]\``)
2426
}
2527

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 => {
2933
const integrationFolder = Cypress.config('integrationFolder')
3034
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')
3162
const testFilePattern = Cypress.config('testFiles')
3263
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
3364

3465
const isInIntegrationFolder = filename =>
3566
filename.startsWith(integrationFolder)
3667
const isTestFile = filename => Cypress.minimatch(filename, testFilePattern)
37-
const isSupportFile = filename => filename === supportFile
3868

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)
4371

4472
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
4573

0 commit comments

Comments
 (0)
Please sign in to comment.