c8 7.7.1 → 7.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,34 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [7.9.0](https://www.github.com/bcoe/c8/compare/v7.8.0...v7.9.0) (2021-09-10)
6
+
7
+
8
+ ### Features
9
+
10
+ * add --exclude-node-modules option ([#321](https://www.github.com/bcoe/c8/issues/321)) ([a4733c6](https://www.github.com/bcoe/c8/commit/a4733c68abd778757bb6ed641e074be94b79c417))
11
+
12
+ ## [7.8.0](https://www.github.com/bcoe/c8/compare/v7.7.3...v7.8.0) (2021-07-10)
13
+
14
+
15
+ ### Features
16
+
17
+ * add --config option and documentation on options and configs ([#308](https://www.github.com/bcoe/c8/issues/308)) ([99436ef](https://www.github.com/bcoe/c8/commit/99436ef131c2ab966174b5012fe22e499fb44ccd))
18
+
19
+ ### [7.7.3](https://www.github.com/bcoe/c8/compare/v7.7.2...v7.7.3) (2021-06-03)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * **deps:** v8-to-istanbul with fix for Windows paths ([#311](https://www.github.com/bcoe/c8/issues/311)) ([ef1b875](https://www.github.com/bcoe/c8/commit/ef1b8757f5f9c664cf63cfce753e93b92057cab5))
25
+
26
+ ### [7.7.2](https://www.github.com/bcoe/c8/compare/v7.7.1...v7.7.2) (2021-05-02)
27
+
28
+
29
+ ### Bug Fixes
30
+
31
+ * address bugs with source remapping on Windows ([#301](https://www.github.com/bcoe/c8/issues/301)) ([c817902](https://www.github.com/bcoe/c8/commit/c81790262f843c01b3d14390fde81dbdbcf2226f))
32
+
5
33
  ### [7.7.1](https://www.github.com/bcoe/c8/compare/v7.7.0...v7.7.1) (2021-04-07)
6
34
 
7
35
 
package/README.md CHANGED
@@ -16,6 +16,31 @@ c8 node foo.js
16
16
 
17
17
  The above example will output coverage metrics for `foo.js`.
18
18
 
19
+ ## CLI Options / Configuration
20
+
21
+ c8 can be configured via command-line flags, a `c8` section in `package.json`, or a JSON configuration file on disk.
22
+
23
+ A configuration file can be specified by passing its path on the command line with `--config` or `-c`. If no config option is provided, c8 searches for files named `.c8rc`, `.c8rc.json`, `.nycrc`, or `.nycrc.json`, starting from
24
+ `cwd` and walking up the filesystem tree.
25
+
26
+ When using `package.json` configuration or a dedicated configuration file, omit the `--` prefix from the long-form of the desired command-line option.
27
+
28
+ Here is a list of common options. Run `c8 --help` for the full list and documentation.
29
+
30
+ | Option | Description | Type | Default |
31
+ | ------ | ----------- | ---- | ------- |
32
+ | `-c`, `--config` | path to JSON configuration file | `string` | See above |
33
+ | `-r`, `--reporter` | coverage reporter(s) to use | `Array<string>` | `['text']` |
34
+ | `-o`, `--reports-dir`, `--report-dir` | directory where coverage reports will be output to | `string` | `./coverage` |
35
+ | `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
36
+ | `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
37
+ | `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
38
+ | `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-exclude.js) |
39
+ | `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` |
40
+ | `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` |
41
+ | `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
42
+ | `--clean` | should temp files be deleted before script execution | `boolean` | `true` |
43
+
19
44
  ## Checking for "full" source coverage using `--all`
20
45
 
21
46
  By default v8 will only give us coverage for files that were loaded by the engine. If there are source files in your
@@ -23,8 +48,9 @@ project that are flexed in production but not in your tests, your coverage numbe
23
48
  if your project's `main.js` loads `a.js` and `b.js` but your unit tests only load `a.js` your total coverage
24
49
  could show as `100%` for `a.js` when in fact both `main.js` and `b.js` are uncovered.
25
50
 
26
- By supplying `--all` to c8, all files in `cwd` that pass the `--include` and `--exclude` flag checks, will be loaded into the
27
- report. If any of those files remain uncovered they will be factored into the report with a default of 0% coverage.
51
+ By supplying `--all` to c8, all files in directories specified with `--src` (defaults to `cwd`) that pass the `--include`
52
+ and `--exclude` flag checks, will be loaded into the report. If any of those files remain uncovered they will be factored
53
+ into the report with a default of 0% coverage.
28
54
 
29
55
  ## c8 report
30
56
 
@@ -24,7 +24,8 @@ exports.outputReport = async function (argv) {
24
24
  all: argv.all,
25
25
  allowExternal: argv.allowExternal,
26
26
  src: argv.src,
27
- skipFull: argv.skipFull
27
+ skipFull: argv.skipFull,
28
+ excludeNodeModules: argv.excludeNodeModules
28
29
  })
29
30
  await report.run()
30
31
  if (argv.checkCoverage) await checkCoverages(argv, report)
package/lib/parse-args.js CHANGED
@@ -5,65 +5,105 @@ const Yargs = require('yargs/yargs')
5
5
  const parser = require('yargs-parser')
6
6
  const { resolve } = require('path')
7
7
 
8
- const configPath = findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
9
- const config = configPath ? JSON.parse(readFileSync(configPath)) : {}
10
-
11
8
  function buildYargs (withCommands = false) {
12
9
  const yargs = Yargs([])
13
10
  .usage('$0 [opts] [script] [opts]')
11
+ .options('config', {
12
+ alias: 'c',
13
+ config: true,
14
+ describe: 'path to JSON configuration file',
15
+ configParser: (path) => JSON.parse(readFileSync(path)),
16
+ default: () => findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
17
+ })
14
18
  .option('reporter', {
15
19
  alias: 'r',
20
+ group: 'Reporting options',
16
21
  describe: 'coverage reporter(s) to use',
17
22
  default: 'text'
18
23
  })
19
24
  .option('reports-dir', {
20
25
  alias: ['o', 'report-dir'],
26
+ group: 'Reporting options',
21
27
  describe: 'directory where coverage reports will be output to',
22
28
  default: './coverage'
23
29
  })
30
+ .options('all', {
31
+ default: false,
32
+ type: 'boolean',
33
+ group: 'Reporting options',
34
+ describe: 'supplying --all will cause c8 to consider all src files in the current working directory ' +
35
+ 'when the determining coverage. Respects include/exclude.'
36
+ })
37
+ .options('src', {
38
+ default: undefined,
39
+ type: 'string',
40
+ group: 'Reporting options',
41
+ describe: 'supplying --src will override cwd as the default location where --all looks for src files. --src can be ' +
42
+ 'supplied multiple times and each directory will be included. This allows for workspaces spanning multiple projects'
43
+ })
44
+ .option('exclude-node-modules', {
45
+ default: true,
46
+ type: 'boolean',
47
+ describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default'
48
+ })
49
+ .option('include', {
50
+ alias: 'n',
51
+ default: [],
52
+ group: 'Reporting options',
53
+ describe: 'a list of specific files that should be covered (glob patterns are supported)'
54
+ })
24
55
  .option('exclude', {
25
56
  alias: 'x',
26
57
  default: defaultExclude,
58
+ group: 'Reporting options',
27
59
  describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
28
60
  })
29
61
  .option('exclude-after-remap', {
30
62
  alias: 'a',
31
63
  type: 'boolean',
32
64
  default: false,
65
+ group: 'Reporting options',
33
66
  describe: 'apply exclude logic to files after they are remapped by a source-map'
34
67
  })
35
- .option('include', {
36
- alias: 'n',
37
- default: [],
38
- describe: 'a list of specific files that should be covered (glob patterns are supported)'
68
+ .options('skip-full', {
69
+ default: false,
70
+ type: 'boolean',
71
+ group: 'Reporting options',
72
+ describe: 'do not show files with 100% statement, branch, and function coverage'
39
73
  })
40
74
  .option('check-coverage', {
41
75
  default: false,
42
76
  type: 'boolean',
77
+ group: 'Coverage thresholds',
43
78
  description: 'check whether coverage is within thresholds provided'
44
79
  })
45
80
  .option('branches', {
46
81
  default: 0,
82
+ group: 'Coverage thresholds',
47
83
  description: 'what % of branches must be covered?',
48
84
  type: 'number'
49
85
  })
50
86
  .option('functions', {
51
87
  default: 0,
88
+ group: 'Coverage thresholds',
52
89
  description: 'what % of functions must be covered?',
53
90
  type: 'number'
54
91
  })
55
92
  .option('lines', {
56
93
  default: 90,
94
+ group: 'Coverage thresholds',
57
95
  description: 'what % of lines must be covered?',
58
96
  type: 'number'
59
97
  })
60
98
  .option('statements', {
61
99
  default: 0,
100
+ group: 'Coverage thresholds',
62
101
  description: 'what % of statements must be covered?',
63
102
  type: 'number'
64
103
  })
65
104
  .option('per-file', {
66
105
  default: false,
106
+ group: 'Coverage thresholds',
67
107
  description: 'check thresholds per file',
68
108
  type: 'boolean'
69
109
  })
@@ -71,6 +111,11 @@ function buildYargs (withCommands = false) {
71
111
  describe: 'directory V8 coverage data is written to and read from',
72
112
  default: process.env.NODE_V8_COVERAGE
73
113
  })
114
+ .option('clean', {
115
+ default: true,
116
+ type: 'boolean',
117
+ describe: 'should temp files be deleted before script execution'
118
+ })
74
119
  .option('resolve', {
75
120
  default: '',
76
121
  describe: 'resolve paths to alternate base directory'
@@ -84,36 +129,13 @@ function buildYargs (withCommands = false) {
84
129
  type: 'boolean',
85
130
  describe: 'omit any paths that are not absolute, e.g., internal/net.js'
86
131
  })
87
- .option('clean', {
88
- default: true,
89
- type: 'boolean',
90
- describe: 'should temp files be deleted before script execution'
91
- })
92
- .options('all', {
93
- default: false,
94
- type: 'boolean',
95
- describe: 'supplying --all will cause c8 to consider all src files in the current working directory ' +
96
- 'when the determining coverage. Respects include/exclude.'
97
- })
98
132
  .options('allowExternal', {
99
133
  default: false,
100
134
  type: 'boolean',
101
135
  describe: 'supplying --allowExternal will cause c8 to allow files from outside of your cwd. This applies both to ' +
102
136
  'files discovered in coverage temp files and also src files discovered if using the --all flag.'
103
137
  })
104
- .options('src', {
105
- default: undefined,
106
- type: 'string',
107
- describe: 'supplying --src will override cwd as the default location where --all looks for src files. --src can be ' +
108
- 'supplied multiple times and each directory will be included. This allows for workspaces spanning multiple projects'
109
- })
110
- .options('skip-full', {
111
- default: false,
112
- type: 'boolean',
113
- describe: 'do not show files with 100% statement, branch, and function coverage'
114
- })
115
138
  .pkgConf('c8')
116
- .config(config)
117
139
  .demandCommand(1)
118
140
  .check((argv) => {
119
141
  if (!argv.tempDirectory) {
package/lib/report.js CHANGED
@@ -1,10 +1,10 @@
1
1
  const Exclude = require('test-exclude')
2
- const furi = require('furi')
3
2
  const libCoverage = require('istanbul-lib-coverage')
4
3
  const libReport = require('istanbul-lib-report')
5
4
  const reports = require('istanbul-reports')
6
5
  const { readdirSync, readFileSync, statSync } = require('fs')
7
6
  const { isAbsolute, resolve, extname } = require('path')
7
+ const { pathToFileURL, fileURLToPath } = require('url')
8
8
  const getSourceMapFromFile = require('./source-map-from-file')
9
9
  // TODO: switch back to @c88/v8-coverage once patch is landed.
10
10
  const v8toIstanbul = require('v8-to-istanbul')
@@ -27,7 +27,8 @@ class Report {
27
27
  all,
28
28
  src,
29
29
  allowExternal = false,
30
- skipFull
30
+ skipFull,
31
+ excludeNodeModules
31
32
  }) {
32
33
  this.reporter = reporter
33
34
  this.reportsDirectory = reportsDirectory
@@ -37,7 +38,8 @@ class Report {
37
38
  this.exclude = new Exclude({
38
39
  exclude: exclude,
39
40
  include: include,
40
- relativePath: !allowExternal
41
+ relativePath: !allowExternal,
42
+ excludeNodeModules: excludeNodeModules
41
43
  })
42
44
  this.excludeAfterRemap = excludeAfterRemap
43
45
  this.omitRelative = omitRelative
@@ -139,8 +141,8 @@ class Report {
139
141
  */
140
142
  _getSourceMap (v8ScriptCov) {
141
143
  const sources = {}
142
- if (this.sourceMapCache[`file://${v8ScriptCov.url}`]) {
143
- const sourceMapAndLineLengths = this.sourceMapCache[`file://${v8ScriptCov.url}`]
144
+ const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
145
+ if (sourceMapAndLineLengths) {
144
146
  // See: https://github.com/nodejs/node/pull/34305
145
147
  if (!sourceMapAndLineLengths.data) return
146
148
  sources.sourceMap = {
@@ -173,7 +175,7 @@ class Report {
173
175
  for (const v8ProcessCov of this._loadReports()) {
174
176
  if (this._isCoverageObject(v8ProcessCov)) {
175
177
  if (v8ProcessCov['source-map-cache']) {
176
- Object.assign(this.sourceMapCache, v8ProcessCov['source-map-cache'])
178
+ Object.assign(this.sourceMapCache, this._normalizeSourceMapCache(v8ProcessCov['source-map-cache']))
177
179
  }
178
180
  v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov, fileIndex))
179
181
  }
@@ -194,7 +196,7 @@ class Report {
194
196
  const stat = statSync(fullPath)
195
197
  const sourceMap = getSourceMapFromFile(fullPath)
196
198
  if (sourceMap) {
197
- this.sourceMapCache[`file://${fullPath}`] = { data: sourceMap }
199
+ this.sourceMapCache[pathToFileURL(fullPath)] = { data: sourceMap }
198
200
  }
199
201
  emptyReports.push({
200
202
  scriptId: 0,
@@ -275,7 +277,7 @@ class Report {
275
277
  }
276
278
  if (/^file:\/\//.test(v8ScriptCov.url)) {
277
279
  try {
278
- v8ScriptCov.url = furi.toSysPath(v8ScriptCov.url)
280
+ v8ScriptCov.url = fileURLToPath(v8ScriptCov.url)
279
281
  fileIndex.add(v8ScriptCov.url)
280
282
  } catch (err) {
281
283
  debuglog(`${err.stack}`)
@@ -290,6 +292,23 @@ class Report {
290
292
  }
291
293
  return { result }
292
294
  }
295
+
296
+ /**
297
+ * Normalizes a V8 source map cache.
298
+ *
299
+ * This function normalizes file URLs to a system-independent format.
300
+ *
301
+ * @param v8SourceMapCache V8 source map cache to normalize.
302
+ * @return {v8SourceMapCache} Normalized V8 source map cache.
303
+ * @private
304
+ */
305
+ _normalizeSourceMapCache (v8SourceMapCache) {
306
+ const cache = {}
307
+ for (const fileURL of Object.keys(v8SourceMapCache)) {
308
+ cache[pathToFileURL(fileURLToPath(fileURL)).href] = v8SourceMapCache[fileURL]
309
+ }
310
+ return cache
311
+ }
293
312
  }
294
313
 
295
314
  module.exports = function (opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c8",
3
- "version": "7.7.1",
3
+ "version": "7.9.0",
4
4
  "description": "output coverage reports using Node.js' built in coverage",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -37,22 +37,22 @@
37
37
  "@istanbuljs/schema": "^0.1.2",
38
38
  "find-up": "^5.0.0",
39
39
  "foreground-child": "^2.0.0",
40
- "furi": "^2.0.0",
41
40
  "istanbul-lib-coverage": "^3.0.0",
42
41
  "istanbul-lib-report": "^3.0.0",
43
42
  "istanbul-reports": "^3.0.2",
44
43
  "rimraf": "^3.0.0",
45
44
  "test-exclude": "^6.0.0",
46
- "v8-to-istanbul": "^7.1.0",
45
+ "v8-to-istanbul": "^8.0.0",
47
46
  "yargs": "^16.2.0",
48
47
  "yargs-parser": "^20.2.7"
49
48
  },
50
49
  "devDependencies": {
50
+ "@types/node": "^16.9.1",
51
51
  "chai": "^4.2.0",
52
52
  "chai-jest-snapshot": "^2.0.0",
53
- "mocha": "^8.1.3",
53
+ "mocha": "^9.0.0",
54
54
  "standard": "^16.0.3",
55
- "ts-node": "^9.0.0",
55
+ "ts-node": "^10.0.0",
56
56
  "typescript": "^4.0.0"
57
57
  },
58
58
  "engines": {