@@ -368,6 +368,26 @@ Otherwise, the test is considered to be a failure. Test files must be
368368executable by Node.js, but are not required to use the ` node:test `  module
369369internally.
370370
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+ 
371391## Mocking  
372392
373393The ` 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}
12191239object, streaming a series of events representing the execution of the tests.
12201240` TestsStream `  will emit events, in the order of the tests definition
12211241
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+ 
12221278### Event: ` 'test:diagnostic' `   
12231279
12241280*  ` data `  {Object}
@@ -1589,6 +1645,7 @@ added:
15891645
15901646[ TAP ] : https://testanything.org/ 
15911647[ `--import` ] : cli.md#--importmodule 
1648+ [ `--test-coverage` ] : cli.md#--test-coverage 
15921649[ `--test-name-pattern` ] : cli.md#--test-name-pattern 
15931650[ `--test-only` ] : cli.md#--test-only 
15941651[ `--test-reporter-destination` ] : cli.md#--test-reporter-destination 
@@ -1597,6 +1654,7 @@ added:
15971654[ `MockFunctionContext` ] : #class-mockfunctioncontext 
15981655[ `MockTracker.method` ] : #mockmethodobject-methodname-implementation-options 
15991656[ `MockTracker` ] : #class-mocktracker 
1657+ [ `NODE_V8_COVERAGE` ] : cli.md#node_v8_coveragedir 
16001658[ `SuiteContext` ] : #class-suitecontext 
16011659[ `TestContext` ] : #class-testcontext 
16021660[ `context.diagnostic` ] : #contextdiagnosticmessage 
@@ -1607,4 +1665,5 @@ added:
16071665[ describe options ] : #describename-options-fn 
16081666[ it options ] : #testname-options-fn 
16091667[ stream.compose ] : stream.md#streamcomposestreams 
1668+ [ test reporters ] : #test-reporters 
16101669[ test runner execution model ] : #test-runner-execution-model 
0 commit comments