@@ -370,6 +370,54 @@ Otherwise, the test is considered to be a failure. Test files must be
370370executable by Node.js, but are not required to use the ` node:test ` module
371371internally.
372372
373+ ## Collecting code coverage
374+
375+ When Node.js is started with the [ ` --test-coverage ` ] [ ] command-line flag, code
376+ coverage is collected and statistics are reported once all tests have completed.
377+ If the [ ` NODE_V8_COVERAGE ` ] [ ] environment variable is used to specify a
378+ code coverage directory, the generated V8 coverage files are written to that
379+ directory. Node.js core modules and files within ` node_modules/ ` directories
380+ are not included in the coverage report. If coverage is enabled, the coverage
381+ report is sent to any [ test reporters] [ ] via the ` 'test:coverage' ` event.
382+
383+ Coverage can be disabled on a series of lines using the following
384+ comment syntax:
385+
386+ ``` js
387+ /* node:coverage disable */
388+ if (anAlwaysFalseCondition) {
389+ // Code in this branch will never be executed, but the lines are ignored for
390+ // coverage purposes. All lines following the 'disable' comment are ignored
391+ // until a corresponding 'enable' comment is encountered.
392+ console .log (' this is never executed' );
393+ }
394+ /* node:coverage enable */
395+ ```
396+
397+ Coverage can also be disabled for a specified number of lines. After the
398+ specified number of lines, coverage will be automatically reenabled. If the
399+ number of lines is not explicitly provided, a single line is ignored.
400+
401+ ``` js
402+ /* node:coverage ignore next */
403+ if (anAlwaysFalseCondition) { console .log (' this is never executed' ); }
404+
405+ /* node:coverage ignore next 3 */
406+ if (anAlwaysFalseCondition) {
407+ console .log (' this is never executed' );
408+ }
409+ ```
410+
411+ The test runner's code coverage functionality has the following limitations,
412+ which will be addressed in a future Node.js release:
413+
414+ * Although coverage data is collected for child processes, this information is
415+ not included in the coverage report. Because the command line test runner uses
416+ child processes to execute test files, it cannot be used with ` --test-coverage ` .
417+ * Source maps are not supported.
418+ * Excluding specific files or directories from the coverage report is not
419+ supported.
420+
373421## Mocking
374422
375423The ` node:test ` module supports mocking during testing via a top-level ` mock `
@@ -1249,6 +1297,42 @@ A successful call to [`run()`][] method will return a new {TestsStream}
12491297object, streaming a series of events representing the execution of the tests.
12501298` TestsStream ` will emit events, in the order of the tests definition
12511299
1300+ ### Event: ` 'test:coverage' `
1301+
1302+ * ` data ` {Object}
1303+ * ` summary ` {Object} An object containing the coverage report.
1304+ * ` files ` {Array} An array of coverage reports for individual files. Each
1305+ report is an object with the following schema:
1306+ * ` path ` {string} The absolute path of the file.
1307+ * ` totalLineCount ` {number} The total number of lines.
1308+ * ` totalBranchCount ` {number} The total number of branches.
1309+ * ` totalFunctionCount ` {number} The total number of functions.
1310+ * ` coveredLineCount ` {number} The number of covered lines.
1311+ * ` coveredBranchCount ` {number} The number of covered branches.
1312+ * ` coveredFunctionCount ` {number} The number of covered functions.
1313+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1314+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1315+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1316+ * ` uncoveredLineNumbers ` {Array} An array of integers representing line
1317+ numbers that are uncovered.
1318+ * ` totals ` {Object} An object containing a summary of coverage for all
1319+ files.
1320+ * ` totalLineCount ` {number} The total number of lines.
1321+ * ` totalBranchCount ` {number} The total number of branches.
1322+ * ` totalFunctionCount ` {number} The total number of functions.
1323+ * ` coveredLineCount ` {number} The number of covered lines.
1324+ * ` coveredBranchCount ` {number} The number of covered branches.
1325+ * ` coveredFunctionCount ` {number} The number of covered functions.
1326+ * ` coveredLinePercent ` {number} The percentage of lines covered.
1327+ * ` coveredBranchPercent ` {number} The percentage of branches covered.
1328+ * ` coveredFunctionPercent ` {number} The percentage of functions covered.
1329+ * ` workingDirectory ` {string} The working directory when code coverage
1330+ began. This is useful for displaying relative path names in case the tests
1331+ changed the working directory of the Node.js process.
1332+ * ` nesting ` {number} The nesting level of the test.
1333+
1334+ Emitted when code coverage is enabled and all tests have completed.
1335+
12521336### Event: ` 'test:diagnostic' `
12531337
12541338* ` data ` {Object}
@@ -1631,6 +1715,7 @@ added:
16311715
16321716[ TAP ] : https://testanything.org/
16331717[ `--import` ] : cli.md#--importmodule
1718+ [ `--test-coverage` ] : cli.md#--test-coverage
16341719[ `--test-name-pattern` ] : cli.md#--test-name-pattern
16351720[ `--test-only` ] : cli.md#--test-only
16361721[ `--test-reporter-destination` ] : cli.md#--test-reporter-destination
@@ -1639,6 +1724,7 @@ added:
16391724[ `MockFunctionContext` ] : #class-mockfunctioncontext
16401725[ `MockTracker.method` ] : #mockmethodobject-methodname-implementation-options
16411726[ `MockTracker` ] : #class-mocktracker
1727+ [ `NODE_V8_COVERAGE` ] : cli.md#node_v8_coveragedir
16421728[ `SuiteContext` ] : #class-suitecontext
16431729[ `TestContext` ] : #class-testcontext
16441730[ `context.diagnostic` ] : #contextdiagnosticmessage
@@ -1649,4 +1735,5 @@ added:
16491735[ describe options ] : #describename-options-fn
16501736[ it options ] : #testname-options-fn
16511737[ stream.compose ] : stream.md#streamcomposestreams
1738+ [ test reporters ] : #test-reporters
16521739[ test runner execution model ] : #test-runner-execution-model
0 commit comments