From 61192892515c365fc867f323e7fd736d6b3aabee 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. PR-URL: https://github.com/nodejs/node/pull/46017 Reviewed-By: Moshe Atlow Reviewed-By: Geoffrey Booth --- doc/api/cli.md | 12 + doc/api/test.md | 87 ++++++ doc/node.1 | 3 + lib/internal/process/pre_execution.js | 43 +-- lib/internal/test_runner/coverage.js | 371 +++++++++++++++++++++++ lib/internal/test_runner/harness.js | 41 ++- lib/internal/test_runner/reporter/tap.js | 38 +++ lib/internal/test_runner/runner.js | 2 +- lib/internal/test_runner/test.js | 6 + lib/internal/test_runner/tests_stream.js | 4 + lib/internal/util.js | 22 ++ src/inspector_profiler.cc | 3 +- src/node_options.cc | 9 + src/node_options.h | 1 + test/fixtures/test-runner/coverage.js | 89 ++++++ test/parallel/test-runner-coverage.js | 98 ++++++ 16 files changed, 796 insertions(+), 33 deletions(-) create mode 100644 lib/internal/test_runner/coverage.js create mode 100644 test/fixtures/test-runner/coverage.js create mode 100644 test/parallel/test-runner-coverage.js diff --git a/doc/api/cli.md b/doc/api/cli.md index a5f168a0b4f86d..48697a801b40c6 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1228,6 +1228,17 @@ 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. If no tests are run, a coverage +report is not generated. See the documentation on +[collecting code coverage from tests][] for more details. + ### `--test-name-pattern`