Skip to content

Commit e87a9e3

Browse files
committed
insert empty coverage for missed files
1 parent 4d07ead commit e87a9e3

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

.circleci/config.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,43 @@ workflows:
354354
../../node_modules/.bin/only-covered main.js
355355
working_directory: examples/support-files
356356

357+
- cypress/run:
358+
attach-workspace: true
359+
name: example-all-files
360+
requires:
361+
- cypress/install
362+
# there are no jobs to follow this one
363+
# so no need to save the workspace files (saves time)
364+
no-workspace: true
365+
start: npm start --prefix examples/all-files
366+
wait-on: 'http://localhost:1234'
367+
command: npx cypress run --project examples/all-files
368+
# store screenshots and videos
369+
store_artifacts: true
370+
post-steps:
371+
- run: cat examples/all-files/.nyc_output/out.json
372+
- run: cat examples/all-files/coverage/coverage-final.json
373+
# store the created coverage report folder
374+
# you can click on it in the CircleCI UI
375+
# to see live static HTML site
376+
- store_artifacts:
377+
path: examples/all-files/coverage
378+
# make sure the examples captures 100% of code
379+
- run:
380+
command: npx nyc report --check-coverage true --lines 100
381+
working_directory: examples/all-files
382+
- run:
383+
name: Check code coverage 📈
384+
# we will check the final coverage report
385+
# to make sure it only has files we are interested in
386+
# because there are files covered at 0 in the report
387+
# TODO confirm "not-covered.js" is at 0/0 but included in the report
388+
command: |
389+
../../node_modules/.bin/check-coverage main.js
390+
../../node_modules/.bin/check-coverage second.js
391+
# ../../node_modules/.bin/only-covered --from coverage/coverage-final.json main.js
392+
working_directory: examples/all-files
393+
357394
- cypress/run:
358395
attach-workspace: true
359396
name: example-exclude-files
@@ -467,3 +504,4 @@ workflows:
467504
- example-exclude-files
468505
- example-docker-paths
469506
- example-use-webpack
507+
- example-all-files

examples/all-files/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"cy:open": "../../node_modules/.bin/cypress open",
77
"cy:run": "../../node_modules/.bin/cypress run",
88
"dev": "../../node_modules/.bin/start-test 1234 cy:open",
9-
"e2e": "../../node_modules/.bin/start-test 1234 cy:run"
9+
"e2e": "../../node_modules/.bin/start-test 1234 cy:run",
10+
"report": "../../node_modules/.bin/nyc report"
1011
},
1112
"nyc": {
1213
"all": true,

task-utils.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ function tryFindingLocalFiles(nycFilename) {
272272
*
273273
* @see https://github.com/cypress-io/code-coverage/issues/207
274274
*/
275-
function includeAllFiles(nycOptions) {
275+
function includeAllFiles(nycFilename, nycOptions) {
276276
debug('include all files options: %o', {
277277
all: nycOptions.all,
278278
include: nycOptions.include,
@@ -298,9 +298,42 @@ function includeAllFiles(nycOptions) {
298298

299299
debug('searching files to include using patterns %o', patterns)
300300

301-
const allFiles = globby.sync(patterns)
301+
const allFiles = globby.sync(patterns, { absolute: true })
302302
debug('found these files %o', allFiles)
303-
// TODO check if any of the files to include are missing from NYC output JSON file
303+
304+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
305+
const coverageKeys = Object.keys(nycCoverage)
306+
const coveredPaths = coverageKeys.map(key => nycCoverage[key].path)
307+
debug('coverage has the following paths %o', coveredPaths)
308+
309+
let changed
310+
allFiles.forEach(fullPath => {
311+
if (coveredPaths.includes(fullPath)) {
312+
// all good, this file exists in coverage object
313+
return
314+
}
315+
debug('adding empty coverage for file %s', fullPath)
316+
changed = true
317+
// insert placeholder object for now
318+
nycCoverage[fullPath] = {
319+
path: fullPath,
320+
statementMap: {},
321+
fnMap: {},
322+
branchMap: {},
323+
s: {},
324+
f: {},
325+
b: {}
326+
}
327+
})
328+
329+
if (changed) {
330+
debug('saving updated file %s', nycFilename)
331+
writeFileSync(
332+
nycFilename,
333+
JSON.stringify(nycCoverage, null, 2) + '\n',
334+
'utf8'
335+
)
336+
}
304337
}
305338

306339
module.exports = {

task.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const tasks = {
168168

169169
if (nycReportOptions.all) {
170170
debug('nyc needs to report on all included files')
171-
includeAllFiles(nycReportOptions)
171+
includeAllFiles(nycFilename, nycReportOptions)
172172
}
173173

174174
debug('calling NYC reporter with options %o', nycReportOptions)

0 commit comments

Comments
 (0)