@@ -368,6 +368,54 @@ Otherwise, the test is considered to be a failure. Test files must be
368
368
executable by Node.js, but are not required to use the ` node:test ` module
369
369
internally.
370
370
371
+ ## Collecting code coverage
372
+
373
+ When Node.js is started with the [ ` --test-coverage ` ] [ ] command-line flag, code
374
+ coverage is collected and statistics are reported once all tests have completed.
375
+ If the [ ` NODE_V8_COVERAGE ` ] [ ] environment variable is used to specify a
376
+ code coverage directory, the generated V8 coverage files are written to that
377
+ directory. Node.js core modules and files within ` node_modules/ ` directories
378
+ are not included in the coverage report. If coverage is enabled, the coverage
379
+ report is sent to any [ test reporters] [ ] via the ` 'test:coverage' ` event.
380
+
381
+ Coverage can be disabled on a series of lines using the following
382
+ comment syntax:
383
+
384
+ ``` js
385
+ /* node:coverage disable */
386
+ if (anAlwaysFalseCondition) {
387
+ // Code in this branch will never be executed, but the lines are ignored for
388
+ // coverage purposes. All lines following the 'disable' comment are ignored
389
+ // until a corresponding 'enable' comment is encountered.
390
+ console .log (' this is never executed' );
391
+ }
392
+ /* node:coverage enable */
393
+ ```
394
+
395
+ Coverage can also be disabled for a specified number of lines. After the
396
+ specified number of lines, coverage will be automatically reenabled. If the
397
+ number of lines is not explicitly provided, a single line is ignored.
398
+
399
+ ``` js
400
+ /* node:coverage ignore next */
401
+ if (anAlwaysFalseCondition) { console .log (' this is never executed' ); }
402
+
403
+ /* node:coverage ignore next 3 */
404
+ if (anAlwaysFalseCondition) {
405
+ console .log (' this is never executed' );
406
+ }
407
+ ```
408
+
409
+ The test runner's code coverage functionality has the following limitations,
410
+ which will be addressed in a future Node.js release:
411
+
412
+ * Although coverage data is collected for child processes, this information is
413
+ not included in the coverage report. Because the command line test runner uses
414
+ child processes to execute test files, it cannot be used with ` --test-coverage ` .
415
+ * Source maps are not supported.
416
+ * Excluding specific files or directories from the coverage report is not
417
+ supported.
418
+
371
419
## Mocking
372
420
373
421
The ` node:test ` module supports mocking during testing via a top-level ` mock `
@@ -1215,6 +1263,42 @@ A successful call to [`run()`][] method will return a new {TestsStream}
1215
1263
object, streaming a series of events representing the execution of the tests.
1216
1264
` TestsStream ` will emit events, in the order of the tests definition
1217
1265
1266
+ ### Event: ` 'test:coverage' `
1267
+
1268
+ * ` data ` {Object}
1269
+ * ` summary ` {Object} An object containing the coverage report.
1270
+ * ` files ` {Array} An array of coverage reports for individual files. Each
1271
+ report is an object with the following schema:
1272
+ * ` path ` {string} The absolute path of the file.
1273
+ * ` totalLineCount ` {number} The total number of lines.
1274
+ * ` totalBranchCount ` {number} The total number of branches.
1275
+ * ` totalFunctionCount ` {number} The total number of functions.
1276
+ * ` coveredLineCount ` {number} The number of covered lines.
1277
+ * ` coveredBranchCount ` {number} The number of covered branches.
1278
+ * ` coveredFunctionCount ` {number} The number of covered functions.
1279
+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1280
+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1281
+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1282
+ * ` uncoveredLineNumbers ` {Array} An array of integers representing line
1283
+ numbers that are uncovered.
1284
+ * ` totals ` {Object} An object containing a summary of coverage for all
1285
+ files.
1286
+ * ` totalLineCount ` {number} The total number of lines.
1287
+ * ` totalBranchCount ` {number} The total number of branches.
1288
+ * ` totalFunctionCount ` {number} The total number of functions.
1289
+ * ` coveredLineCount ` {number} The number of covered lines.
1290
+ * ` coveredBranchCount ` {number} The number of covered branches.
1291
+ * ` coveredFunctionCount ` {number} The number of covered functions.
1292
+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1293
+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1294
+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1295
+ * ` workingDirectory ` {string} The working directory when code coverage
1296
+ began. This is useful for displaying relative path names in case the tests
1297
+ changed the working directory of the Node.js process.
1298
+ * ` nesting ` {number} The nesting level of the test.
1299
+
1300
+ Emitted when code coverage is enabled and all tests have completed.
1301
+
1218
1302
### Event: ` 'test:diagnostic' `
1219
1303
1220
1304
* ` data ` {Object}
@@ -1595,6 +1679,7 @@ added:
1595
1679
1596
1680
[ TAP ] : https://testanything.org/
1597
1681
[ `--import` ] : cli.md#--importmodule
1682
+ [ `--test-coverage` ] : cli.md#--test-coverage
1598
1683
[ `--test-name-pattern` ] : cli.md#--test-name-pattern
1599
1684
[ `--test-only` ] : cli.md#--test-only
1600
1685
[ `--test-reporter-destination` ] : cli.md#--test-reporter-destination
@@ -1603,6 +1688,7 @@ added:
1603
1688
[ `MockFunctionContext` ] : #class-mockfunctioncontext
1604
1689
[ `MockTracker.method` ] : #mockmethodobject-methodname-implementation-options
1605
1690
[ `MockTracker` ] : #class-mocktracker
1691
+ [ `NODE_V8_COVERAGE` ] : cli.md#node_v8_coveragedir
1606
1692
[ `SuiteContext` ] : #class-suitecontext
1607
1693
[ `TestContext` ] : #class-testcontext
1608
1694
[ `context.diagnostic` ] : #contextdiagnosticmessage
@@ -1613,4 +1699,5 @@ added:
1613
1699
[ describe options ] : #describename-options-fn
1614
1700
[ it options ] : #testname-options-fn
1615
1701
[ stream.compose ] : stream.md#streamcomposestreams
1702
+ [ test reporters ] : #test-reporters
1616
1703
[ test runner execution model ] : #test-runner-execution-model
0 commit comments