Skip to content

Commit 2f70090

Browse files
committed
search using default extension masks if include is not present
1 parent 0f76cfb commit 2f70090

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

examples/all-files/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"report": "../../node_modules/.bin/nyc report"
1111
},
1212
"nyc": {
13-
"all": true
13+
"all": true,
14+
"include": "*.js"
1415
},
1516
"devDependencies": {
1617
"@babel/core": "7.9.0"

task-utils.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,35 @@ function tryFindingLocalFiles(nycFilename) {
235235
}
236236

237237
/**
238-
* If the website or unit tests did not load ALL files we need to
239-
* include, then we should include the missing files ourselves
240-
* before generating the report.
241-
*
242-
* @see https://github.com/cypress-io/code-coverage/issues/207
238+
* Tries to find source files to be included in the final coverage report
239+
* using NYC options: extension list, include and exclude.
243240
*/
244-
function includeAllFiles(nycFilename, nycOptions) {
241+
function findSourceFiles(nycOptions) {
245242
debug('include all files options: %o', {
246243
all: nycOptions.all,
247244
include: nycOptions.include,
248-
exclude: nycOptions.exclude
245+
exclude: nycOptions.exclude,
246+
extension: nycOptions.extension
249247
})
250248

249+
if (!Array.isArray(nycOptions.extension)) {
250+
console.error(
251+
'Expected NYC "extension" option to be a list of file extensions'
252+
)
253+
console.error(nycOptions)
254+
return []
255+
}
256+
251257
let patterns = []
252258
if (Array.isArray(nycOptions.include)) {
253259
patterns = patterns.concat(nycOptions.include)
254260
} else if (typeof nycOptions.include === 'string') {
255261
patterns.push(nycOptions.include)
262+
} else {
263+
debug('using default list of extensions')
264+
nycOptions.extension.forEach(extension => {
265+
patterns.push('**/*' + extension)
266+
})
256267
}
257268

258269
if (Array.isArray(nycOptions.exclude)) {
@@ -268,7 +279,30 @@ function includeAllFiles(nycFilename, nycOptions) {
268279
debug('searching files to include using patterns %o', patterns)
269280

270281
const allFiles = globby.sync(patterns, { absolute: true })
271-
debug('found these files %o', allFiles)
282+
return allFiles
283+
}
284+
/**
285+
* If the website or unit tests did not load ALL files we need to
286+
* include, then we should include the missing files ourselves
287+
* before generating the report.
288+
*
289+
* @see https://github.com/cypress-io/code-coverage/issues/207
290+
*/
291+
function includeAllFiles(nycFilename, nycOptions) {
292+
if (!nycOptions.all) {
293+
debug('NYC "all" option is not set, skipping including all files')
294+
return
295+
}
296+
297+
const allFiles = findSourceFiles(nycOptions)
298+
if (debug.enabled) {
299+
debug('found %d file(s)', allFiles.length)
300+
console.error(allFiles.join('\n'))
301+
}
302+
if (!allFiles.length) {
303+
debug('no files found, hoping for the best')
304+
return
305+
}
272306

273307
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
274308
const coverageKeys = Object.keys(nycCoverage)

0 commit comments

Comments
 (0)