Skip to content

Commit 4d07ead

Browse files
committed
find files to include using globby
1 parent 65cb0c8 commit 4d07ead

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

examples/all-files/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"scripts": {
55
"start": "../../node_modules/.bin/parcel serve index.html",
66
"cy:open": "../../node_modules/.bin/cypress open",
7-
"dev": "../../node_modules/.bin/start-test 1234 cy:open"
7+
"cy:run": "../../node_modules/.bin/cypress run",
8+
"dev": "../../node_modules/.bin/start-test 1234 cy:open",
9+
"e2e": "../../node_modules/.bin/start-test 1234 cy:run"
810
},
911
"nyc": {
1012
"all": true,

task-utils.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { readFileSync, writeFileSync, existsSync } = require('fs')
77
const { isAbsolute, resolve, join } = require('path')
88
const debug = require('debug')('code-coverage')
99
const chalk = require('chalk')
10+
const globby = require('globby')
1011

1112
function combineNycOptions({
1213
pkgNycOptions,
@@ -264,12 +265,51 @@ function tryFindingLocalFiles(nycFilename) {
264265
}
265266
}
266267

268+
/**
269+
* If the website or unit tests did not load ALL files we need to
270+
* include, then we should include the missing files ourselves
271+
* before generating the report.
272+
*
273+
* @see https://github.com/cypress-io/code-coverage/issues/207
274+
*/
275+
function includeAllFiles(nycOptions) {
276+
debug('include all files options: %o', {
277+
all: nycOptions.all,
278+
include: nycOptions.include,
279+
exclude: nycOptions.exclude
280+
})
281+
282+
let patterns = []
283+
if (Array.isArray(nycOptions.include)) {
284+
patterns = patterns.concat(nycOptions.include)
285+
} else if (typeof nycOptions.include === 'string') {
286+
patterns.push(nycOptions.include)
287+
}
288+
289+
if (Array.isArray(nycOptions.exclude)) {
290+
const negated = nycOptions.exclude.map(s => '!' + s)
291+
patterns = patterns.concat(negated)
292+
} else if (typeof nycOptions.exclude === 'string') {
293+
patterns.push('!' + nycOptions.exclude)
294+
}
295+
// always exclude node_modules
296+
// https://github.com/istanbuljs/nyc#including-files-within-node_modules
297+
patterns.push('!**/node_modules/**')
298+
299+
debug('searching files to include using patterns %o', patterns)
300+
301+
const allFiles = globby.sync(patterns)
302+
debug('found these files %o', allFiles)
303+
// TODO check if any of the files to include are missing from NYC output JSON file
304+
}
305+
267306
module.exports = {
268307
showNycInfo,
269308
resolveRelativePaths,
270309
checkAllPathsNotFound,
271310
tryFindingLocalFiles,
272311
readNycOptions,
273312
combineNycOptions,
274-
defaultNycOptions
313+
defaultNycOptions,
314+
includeAllFiles
275315
}

task.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const {
88
resolveRelativePaths,
99
checkAllPathsNotFound,
1010
tryFindingLocalFiles,
11-
readNycOptions
11+
readNycOptions,
12+
includeAllFiles
1213
} = require('./task-utils')
1314
const { fixSourcePaths } = require('./support-utils')
1415
const NYC = require('nyc')
@@ -165,6 +166,11 @@ const tasks = {
165166
// seems nyc API really is using camel cased version
166167
nycReportOptions.reportDir = nycReportOptions['report-dir']
167168

169+
if (nycReportOptions.all) {
170+
debug('nyc needs to report on all included files')
171+
includeAllFiles(nycReportOptions)
172+
}
173+
168174
debug('calling NYC reporter with options %o', nycReportOptions)
169175
debug('current working directory is %s', process.cwd())
170176
const nyc = new NYC(nycReportOptions)

0 commit comments

Comments
 (0)