@@ -368,6 +368,26 @@ 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
+ The test runner's code coverage functionality has the following limitations,
382
+ which will be addressed in a future Node.js release:
383
+
384
+ * Although coverage data is collected for child processes, this information is
385
+ not included in the coverage report. Because the command line test runner uses
386
+ child processes to execute test files, it cannot be used with ` --test-coverage ` .
387
+ * Source maps are not supported.
388
+ * Excluding specific files or directories from the coverage report is not
389
+ supported.
390
+
371
391
## Mocking
372
392
373
393
The ` node:test ` module supports mocking during testing via a top-level ` mock `
@@ -1219,6 +1239,42 @@ A successful call to [`run()`][] method will return a new {TestsStream}
1219
1239
object, streaming a series of events representing the execution of the tests.
1220
1240
` TestsStream ` will emit events, in the order of the tests definition
1221
1241
1242
+ ### Event: ` 'test:coverage' `
1243
+
1244
+ * ` data ` {Object}
1245
+ * ` summary ` {Object} An object containing the coverage report.
1246
+ * ` files ` {Array} An array of coverage reports for individual files. Each
1247
+ report is an object with the following schema:
1248
+ * ` path ` {string} The absolute path of the file.
1249
+ * ` totalLineCount ` {number} The total number of lines.
1250
+ * ` totalBranchCount ` {number} The total number of branches.
1251
+ * ` totalFunctionCount ` {number} The total number of functions.
1252
+ * ` coveredLineCount ` {number} The number of covered lines.
1253
+ * ` coveredBranchCount ` {number} The number of covered branches.
1254
+ * ` coveredFunctionCount ` {number} The number of covered functions.
1255
+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1256
+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1257
+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1258
+ * ` uncoveredLineNumbers ` {Array} An array of integers representing line
1259
+ numbers that are uncovered.
1260
+ * ` totals ` {Object} An object containing a summary of coverage for all
1261
+ files.
1262
+ * ` totalLineCount ` {number} The total number of lines.
1263
+ * ` totalBranchCount ` {number} The total number of branches.
1264
+ * ` totalFunctionCount ` {number} The total number of functions.
1265
+ * ` coveredLineCount ` {number} The number of covered lines.
1266
+ * ` coveredBranchCount ` {number} The number of covered branches.
1267
+ * ` coveredFunctionCount ` {number} The number of covered functions.
1268
+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1269
+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1270
+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1271
+ * ` workingDirectory ` {string} The working directory when code coverage
1272
+ began. This is useful for displaying relative path names in case the tests
1273
+ changed the working directory of the Node.js process.
1274
+ * ` nesting ` {number} The nesting level of the test.
1275
+
1276
+ Emitted when code coverage is enabled and all tests have completed.
1277
+
1222
1278
### Event: ` 'test:diagnostic' `
1223
1279
1224
1280
* ` data ` {Object}
@@ -1589,6 +1645,7 @@ added:
1589
1645
1590
1646
[ TAP ] : https://testanything.org/
1591
1647
[ `--import` ] : cli.md#--importmodule
1648
+ [ `--test-coverage` ] : cli.md#--test-coverage
1592
1649
[ `--test-name-pattern` ] : cli.md#--test-name-pattern
1593
1650
[ `--test-only` ] : cli.md#--test-only
1594
1651
[ `--test-reporter-destination` ] : cli.md#--test-reporter-destination
@@ -1597,6 +1654,7 @@ added:
1597
1654
[ `MockFunctionContext` ] : #class-mockfunctioncontext
1598
1655
[ `MockTracker.method` ] : #mockmethodobject-methodname-implementation-options
1599
1656
[ `MockTracker` ] : #class-mocktracker
1657
+ [ `NODE_V8_COVERAGE` ] : cli.md#node_v8_coveragedir
1600
1658
[ `SuiteContext` ] : #class-suitecontext
1601
1659
[ `TestContext` ] : #class-testcontext
1602
1660
[ `context.diagnostic` ] : #contextdiagnosticmessage
@@ -1607,4 +1665,5 @@ added:
1607
1665
[ describe options ] : #describename-options-fn
1608
1666
[ it options ] : #testname-options-fn
1609
1667
[ stream.compose ] : stream.md#streamcomposestreams
1668
+ [ test reporters ] : #test-reporters
1610
1669
[ test runner execution model ] : #test-runner-execution-model
0 commit comments