GeezerVison is showing github.com — test_runner: add support for --test-coverage-include-all · nodejs/node@1d14900
File treeExpand file treeCollapse file tree Open diff view settings fixtures/test-runner/coverage-include-all
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +16Lines changed: 16 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2725,6 +2725,21 @@ This option may be specified multiple times to include multiple glob patterns. | 2725 | 2725 | If both `--test-coverage-exclude` and `--test-coverage-include` are provided, | 2726 | 2726 | files must meet **both** criteria to be included in the coverage report. | 2727 | 2727 |
| | 2728 | +### `--test-coverage-include-all` | | 2729 | + | | 2730 | +<!-- YAML | | 2731 | +added: REPLACEME | | 2732 | +--> | | 2733 | + | | 2734 | +> Stability: 1 - Experimental | | 2735 | +
| | 2736 | +Includes source files that were never loaded by the test run in the coverage | | 2737 | +report, where they are reported as having zero coverage. | | 2738 | + | | 2739 | +Candidate files are searched for in the current working directory, and are | | 2740 | +subject to the same `--test-coverage-include` and `--test-coverage-exclude` | | 2741 | +filtering as the rest of the report. | | 2742 | + | 2728 | 2743 | ### `--test-coverage-lines=threshold` | 2729 | 2744 |
| 2730 | 2745 | <!-- YAML | @@ -3758,6 +3773,7 @@ one is included in the list below. | 3758 | 3773 | * `--test-coverage-branches` | 3759 | 3774 | * `--test-coverage-exclude` | 3760 | 3775 | * `--test-coverage-functions` | | 3776 | +* `--test-coverage-include-all` | 3761 | 3777 | * `--test-coverage-include` | 3762 | 3778 | * `--test-coverage-lines` | 3763 | 3779 | * `--test-global-setup` | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/test.md +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1777,6 +1777,13 @@ changes: | 1777 | 1777 | If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided, | 1778 | 1778 | files must meet **both** criteria to be included in the coverage report. | 1779 | 1779 | **Default:** `undefined`. | | 1780 | + * `coverageIncludeAll` {boolean} Includes source files that were never loaded by | | 1781 | + the test run in the coverage report, where they are reported as having zero | | 1782 | + coverage. Candidate files are searched for in `cwd`, and are subject to the | | 1783 | + same `coverageIncludeGlobs` and `coverageExcludeGlobs` filtering as the rest | | 1784 | + of the report. This property is only applicable when `coverage` was set to | | 1785 | + `true`. | | 1786 | + **Default:** `false`. | 1780 | 1787 | * `lineCoverage` {number} Require a minimum percent of covered lines. If code | 1781 | 1788 | coverage does not reach the threshold specified, the process will exit with code `1`. | 1782 | 1789 | **Default:** `0`. | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -607,6 +607,13 @@ Require a minimum threshold for function coverage (0 - 100). | 607 | 607 | .It Fl -test-coverage-include | 608 | 608 | A glob pattern that only includes matching files in the coverage report | 609 | 609 | . | | 610 | +.It Fl -test-coverage-include-all | | 611 | +Includes source files that were never loaded by the test run in the coverage | | 612 | +report, where they are reported as having zero coverage. | | 613 | +Candidate files are searched for in the current working directory, and are | | 614 | +subject to the same \fB--test-coverage-include\fR and \fB--test-coverage-exclude\fR | | 615 | +filtering as the rest of the report. | | 616 | +. | 610 | 617 | .It Fl -test-coverage-lines Ns = Ns Ar threshold | 611 | 618 | Require a minimum threshold for line coverage (0 - 100). | 612 | 619 | . | @@ -938,6 +945,13 @@ Node.js will exit with an error if an option that is not allowed in the environm | 938 | 945 | .Fl -print | 939 | 946 | or a script file. | 940 | 947 | . | | 948 | +.It Fl -test-coverage-include-all | | 949 | +Includes source files that were never loaded by the test run in the coverage | | 950 | +report, where they are reported as having zero coverage. | | 951 | +Candidate files are searched for in the current working directory, and are | | 952 | +subject to the same \fB--test-coverage-include\fR and \fB--test-coverage-exclude\fR | | 953 | +filtering as the rest of the report. | | 954 | +. | 941 | 955 | .It Ev NODE_PATH Ar directories... | 942 | 956 | A colon-separated list of | 943 | 957 | .Ar directories | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/coverage.js +49-1Lines changed: 49 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ const { | 20 | 20 | } = primordials; | 21 | 21 | const { | 22 | 22 | copyFileSync, | | 23 | + globSync, | 23 | 24 | mkdirSync, | 24 | 25 | mkdtempSync, | 25 | 26 | opendirSync, | @@ -29,7 +30,7 @@ const { | 29 | 30 | const { setupCoverageHooks } = require('internal/util'); | 30 | 31 | const { tmpdir } = require('os'); | 31 | 32 | const { join, resolve, relative } = require('path'); | 32 | | -const { fileURLToPath, URL } = require('internal/url'); | | 33 | +const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); | 33 | 34 | const { kMappings, SourceMap } = require('internal/source_map/source_map'); | 34 | 35 | const { | 35 | 36 | codes: { | @@ -51,6 +52,7 @@ const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//; | 51 | 52 | const kMatchGlobPatternOptions = { __proto__: null, dot: true }; | 52 | 53 | const kTypeOnlyImportRegex = /^\s*import\s+type\b/u; | 53 | 54 | const kTypeScriptSourceRegex = /\.(?:cts|mts|ts)$/u; | | 55 | +const kSourceFileGlob = '**/*.{cjs,cts,js,mjs,mts,ts}'; | 54 | 56 |
| 55 | 57 | let stripTypeScriptTypesForCoverage; | 56 | 58 |
| @@ -418,6 +420,10 @@ class TestCoverage { | 418 | 420 | this.mergeCoverage(result, this.mapCoverageWithSourceMap(coverage)); | 419 | 421 | } | 420 | 422 |
| | 423 | + if (this.options.coverageIncludeAll) { | | 424 | + this.#addUntestedFileCoverage(result); | | 425 | + } | | 426 | + | 421 | 427 | return ArrayFrom(result.values()); | 422 | 428 | } finally { | 423 | 429 | if (dir) { | @@ -426,6 +432,48 @@ class TestCoverage { | 426 | 432 | } | 427 | 433 | } | 428 | 434 |
| | 435 | + #addUntestedFileCoverage(merged) { | | 436 | + const files = globSync(kSourceFileGlob, { | | 437 | + __proto__: null, | | 438 | + cwd: this.options.cwd, | | 439 | + // Skip node_modules/, since `shouldSkipFileCoverage` would skip it anyway | | 440 | + exclude: (name) => name === 'node_modules', | | 441 | + }); | | 442 | + | | 443 | + for (let i = 0; i < files.length; ++i) { | | 444 | + const url = pathToFileURL(resolve(this.options.cwd, files[i])).href; | | 445 | + | | 446 | + if (merged.has(url) || this.shouldSkipFileCoverage(url)) { | | 447 | + continue; | | 448 | + } | | 449 | + | | 450 | + this.markTypeScriptOnlyLines(url); | | 451 | + const lines = this.getLines(url); | | 452 | + | | 453 | + if (!lines || lines.length === 0) { | | 454 | + continue; | | 455 | + } | | 456 | + | | 457 | + const lastLine = lines[lines.length - 1]; | | 458 | + | | 459 | + merged.set(url, { | | 460 | + __proto__: null, | | 461 | + url, | | 462 | + functions: [{ | | 463 | + __proto__: null, | | 464 | + functionName: '', | | 465 | + isBlockCoverage: false, | | 466 | + ranges: [{ | | 467 | + __proto__: null, | | 468 | + startOffset: 0, | | 469 | + endOffset: lastLine.startOffset + lastLine.src.length, | | 470 | + count: 0, | | 471 | + }], | | 472 | + }], | | 473 | + }); | | 474 | + } | | 475 | + } | | 476 | + | 429 | 477 |
| 430 | 478 | mapCoverageWithSourceMap(coverage) { | 431 | 479 | const { result } = coverage; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -731,6 +731,7 @@ function run(options = kEmptyObject) { | 731 | 731 | only, | 732 | 732 | globPatterns, | 733 | 733 | coverage = false, | | 734 | + coverageIncludeAll = false, | 734 | 735 | lineCoverage = 0, | 735 | 736 | branchCoverage = 0, | 736 | 737 | functionCoverage = 0, | @@ -887,6 +888,7 @@ function run(options = kEmptyObject) { | 887 | 888 |
| 888 | 889 | validateOneOf(isolation, 'options.isolation', ['process', 'none']); | 889 | 890 | validateBoolean(coverage, 'options.coverage'); | | 891 | + validateBoolean(coverageIncludeAll, 'options.coverageIncludeAll'); | 890 | 892 | if (coverageExcludeGlobs != null) { | 891 | 893 | if (!ArrayIsArray(coverageExcludeGlobs)) { | 892 | 894 | coverageExcludeGlobs = [coverageExcludeGlobs]; | @@ -928,6 +930,7 @@ function run(options = kEmptyObject) { | 928 | 930 | ...parseCommandLine(), | 929 | 931 | setup, // This line can be removed when parseCommandLine() is removed here. | 930 | 932 | coverage, | | 933 | + coverageIncludeAll, | 931 | 934 | coverageExcludeGlobs, | 932 | 935 | coverageIncludeGlobs, | 933 | 936 | rerunFailuresFilePath, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/utils.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -245,6 +245,7 @@ function parseCommandLine() { | 245 | 245 |
| 246 | 246 | const isTestRunner = getOptionValue('--test'); | 247 | 247 | const coverage = getOptionValue('--experimental-test-coverage'); | | 248 | + const coverageIncludeAll = getOptionValue('--test-coverage-include-all'); | 248 | 249 | const forceExit = getOptionValue('--test-force-exit'); | 249 | 250 | const sourceMaps = getOptionValue('--enable-source-maps'); | 250 | 251 | const updateSnapshots = getOptionValue('--test-update-snapshots'); | @@ -408,6 +409,7 @@ function parseCommandLine() { | 408 | 409 | isTestRunner, | 409 | 410 | concurrency, | 410 | 411 | coverage, | | 412 | + coverageIncludeAll, | 411 | 413 | coverageExcludeGlobs, | 412 | 414 | coverageIncludeGlobs, | 413 | 415 | destinations, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1011,6 +1011,13 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 1011 | 1011 | &EnvironmentOptions::coverage_include_pattern, | 1012 | 1012 | kAllowedInEnvvar, | 1013 | 1013 | OptionNamespaces::kTestRunnerNamespace); | | 1014 | + AddOption("--test-coverage-include-all", | | 1015 | + "include source files that were never loaded in the coverage " | | 1016 | + "report", | | 1017 | + &EnvironmentOptions::coverage_include_all, | | 1018 | + kAllowedInEnvvar, | | 1019 | + false, | | 1020 | + OptionNamespaces::kTestRunnerNamespace); | 1014 | 1021 | AddOption("--test-coverage-exclude", | 1015 | 1022 | "exclude files from coverage report that match this glob pattern", | 1016 | 1023 | &EnvironmentOptions::coverage_exclude_pattern, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -220,6 +220,7 @@ class EnvironmentOptions : public Options { | 220 | 220 | std::vector<std::string> test_skip_pattern; | 221 | 221 | std::vector<std::string> experimental_test_tag_filter; | 222 | 222 | std::vector<std::string> coverage_include_pattern; | | 223 | + bool coverage_include_all = false; | 223 | 224 | std::vector<std::string> coverage_exclude_pattern; | 224 | 225 | bool throw_deprecation = false; | 225 | 226 | bool trace_deprecation = false; | |
Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +module.exports = function covered() { | | 4 | + return 'covered'; | | 5 | +}; |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "not": "a source file" | | 3 | +} |
|