From 59a4ee0cefc975b4e7759fbc664370fa14d0deaf Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 21 Dec 2022 13:26:16 -0500 Subject: [PATCH] test_runner: add initial code coverage support This commit adds code coverage functionality to the node:test module. When node:test is used in conjunction with the new --test-coverage CLI flag, a coverage report is created when the test runner finishes. The coverage summary is forwarded to any test runner reporters so that the display can be customized as desired. This new functionality is compatible with the existing NODE_V8_COVERAGE environment variable as well. There are still several limitations, which will be addressed in subsequent pull requests: - Coverage is only reported for a single process. It is possible to merge coverage reports together. Once this is done, the --test flag will be supported as well. - Source maps are not currently supported. - Excluding specific files or directories from the coverage report is not currently supported. Node core modules and node_modules/ are excluded though. --- doc/api/cli.md | 11 + doc/api/test.md | 59 ++++ doc/node.1 | 3 + lib/internal/process/pre_execution.js | 43 +-- lib/internal/test_runner/coverage.js | 362 +++++++++++++++++++++++ lib/internal/test_runner/runner.js | 2 +- lib/internal/test_runner/test.js | 14 + lib/internal/test_runner/tests_stream.js | 4 + lib/internal/util.js | 22 ++ lib/test/reporter/tap.js | 38 +++ src/node_options.cc | 9 + src/node_options.h | 1 + 12 files changed, 537 insertions(+), 31 deletions(-) create mode 100644 lib/internal/test_runner/coverage.js diff --git a/doc/api/cli.md b/doc/api/cli.md index fa044c442a0138..70e3b5c2176062 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1229,6 +1229,16 @@ Starts the Node.js command line test runner. This flag cannot be combined with See the documentation on [running tests from the command line][] for more details. +### `--test-coverage` + + + +When used in conjunction with the `node:test` module, a code coverage report is +generated as part of the test runner output. See the documentation on +[collecting code coverage from tests][] for more details. + ### `--test-name-pattern`