Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
6119289
test_runner: add initial code coverage support
cjihrig Dec 21, 2022
9b23309
doc,lib,src,test: rename --test-coverage
cjihrig Jan 8, 2023
bb14a2b
meta: move one or more collaborators to emeritus
nodejs-github-bot Feb 1, 2023
7e7e2d0
deps: update c-ares to 1.19.0
targos Feb 1, 2023
a66e7ca
test: add trailing commas in `test/internet`
aduh95 Feb 1, 2023
c7f29b2
test: add trailing commas in `test/known_issues`
aduh95 Feb 1, 2023
33a98c4
doc: update threat model based on discussions
mhdawson Jan 26, 2023
05ad792
async_hooks: remove experimental onPropagate option
jasnell Jan 27, 2023
b364b9b
benchmark: add trailing commas in `benchmark/fs`
aduh95 Feb 1, 2023
d556ccd
test: make more crypto tests work with BoringSSL
codebytere Feb 1, 2023
2c8c9f9
benchmark: add trailing commas in `benchmark/async_hooks`
aduh95 Feb 1, 2023
b31aabb
test_runner: top-level diagnostics not ommited when running with --test
pulkit-30 Feb 2, 2023
905a675
doc: add note about major release rotation
RafaelGSS Feb 2, 2023
b844a09
stream: dont access Object.prototype.type during TransformStream init
debadree25 Feb 2, 2023
31d3e3c
doc: fix -C flag usage
sxzz Feb 2, 2023
4cf4b41
stream: add suport for abort signal in finished() for webstreams
debadree25 Feb 2, 2023
c206853
stream: add pipeline() for webstreams
debadree25 Feb 2, 2023
c526f9f
test_runner: fix missing test diagnostics
MoLow Feb 2, 2023
51c6c61
tools: update ESLint custom rules to not use the deprecated format
aduh95 Feb 3, 2023
8b88d60
benchmark: add trailing commas in `benchmark/util`
aduh95 Feb 3, 2023
7760d40
benchmark: add trailing commas in `benchmark/net`
aduh95 Feb 3, 2023
897e3c2
benchmark: add trailing commas in `benchmark/module`
aduh95 Jan 30, 2023
0c2c322
doc: add deokjinkim to collaborators
deokjinkim Jan 31, 2023
aef57cd
doc: fix wrong location of `requestTimeout`'s default value
deokjinkim Feb 3, 2023
e77d538
src: allow embedder control of code generation policy
codebytere Feb 3, 2023
6cf860d
src: back snapshot I/O with a std::vector sink
joyeecheung Feb 3, 2023
faba8d4
test: add trailing commas in `test/js-native-api`
aduh95 Jan 25, 2023
a8a2d0e
src: add GetCurrentCipherName/Version to crypto_common
jasnell Dec 19, 2022
efc59d0
src: add KeyObjectHandle::HasInstance
jasnell Dec 19, 2022
95065c3
src: add additional utilities to crypto::SecureContext
jasnell Dec 19, 2022
6801d37
src: add snapshot support for embedder API
addaleax Dec 12, 2022
658d2f4
src: make build_snapshot a per-Isolate option, rather than a global one
addaleax Jan 24, 2023
683a1f8
src: allow snapshotting from the embedder API
addaleax Jan 24, 2023
731a7ae
async_hooks: add async local storage propagation benchmarks
legendecas Feb 3, 2023
17b3ee3
vm: properly support symbols on globals
dubzzz Feb 4, 2023
7f8b292
benchmark: add trailing commas in `benchmark/buffers`
aduh95 Feb 4, 2023
6c69ad6
benchmark: add trailing commas in `benchmark/misc`
aduh95 Feb 4, 2023
6bf7388
stream: allow transfer of readable byte streams
mrbbot Feb 4, 2023
6f24f06
timers: cleanup no-longer relevant TODOs in timers/promises
jasnell Feb 5, 2023
9d9b3f8
benchmark: add trailing commas in `benchmark/process`
aduh95 Feb 5, 2023
d6fbebd
url: remove unused `setURLConstructor` function
anonrig Feb 5, 2023
9a604d6
url: remove unused `URL::toObject`
anonrig Feb 5, 2023
129c9e7
url: remove unused `URL::ToFilePath()`
anonrig Feb 5, 2023
a7beac0
doc: enrich test command with executable
tony-go Feb 6, 2023
375bb22
doc: update test concurrency description / default values
richiemccoll Jan 31, 2023
00b81c7
src: replace icu with simdutf for char counts
anonrig Feb 2, 2023
f35f6d2
src: use simdutf utf8 to utf16 instead of icu
anonrig Feb 2, 2023
60c2a86
src: use string_view instead of std::string&
anonrig Feb 2, 2023
f72f643
src: remove unused includes from main thread
anonrig Feb 3, 2023
1a808a4
src: check return value of ftell()
tniessen Feb 6, 2023
3c5db8f
src: avoid leaking snapshot fp on error
tniessen Feb 6, 2023
17b82c8
meta: update AUTHORS
nodejs-github-bot Feb 6, 2023
c3325bf
src: make edge names in BaseObjects more descriptive in heap snapshots
joyeecheung Feb 6, 2023
89aa161
test: fix tap parser fails if a test logs a number
pulkit-30 Feb 6, 2023
7d6c27e
deps: add ada as a dependency
anonrig Jan 27, 2023
a51fe3c
url: replace url-parser with ada
anonrig Feb 6, 2023
c34bac2
src: allow blobs in addition to `FILE*`s in embedder snapshot API
addaleax Feb 6, 2023
148c5d9
tools: update lint-md-dependencies to rollup@3.13.0 vfile-reporter@7.0.5
nodejs-github-bot Feb 7, 2023
641e177
test: enable api-invalid-label.any.js in encoding WPTs
panva Feb 7, 2023
8590eb4
lib: add aborted() utility function
debadree25 Feb 7, 2023
cb9b9ad
meta: move one or more collaborators to emeritus
nodejs-github-bot Feb 8, 2023
d8c527f
http: use v8::Array::New() with a prebuilt vector
joyeecheung Feb 8, 2023
f91260b
stream: refactor to use `validateAbortSignal`
aduh95 Feb 8, 2023
35570e9
lib: tighten `AbortSignal.prototype.throwIfAborted` implementation
aduh95 Feb 8, 2023
da9ebaf
lib: predeclare Event.isTrusted prop descriptor
santigimeno Feb 8, 2023
31fb2e2
src: add fflush() to SnapshotData::ToFile()
addaleax Feb 8, 2023
7524871
benchmark: add trailing commas in `benchmark/http2`
aduh95 Feb 9, 2023
638d9b8
benchmark: add trailing commas in `benchmark/url`
aduh95 Feb 10, 2023
ff95eb7
benchmark: add trailing commas in `benchmark/crypto`
aduh95 Feb 10, 2023
c771d66
doc: fix test runner examples
richiemccoll Feb 10, 2023
c0983cf
deps: copy `postject-api.h` and `LICENSE` to the `deps` folder
RaisinTen Feb 9, 2023
4b89ec4
benchmark: add trailing commas in `benchmark/http`
aduh95 Feb 12, 2023
8ee9e48
tools: update doc to remark-html@15.0.2 to-vfile@7.2.4
nodejs-github-bot Feb 12, 2023
f87216b
tools: update lint-md-dependencies to rollup@3.15.0 to-vfile@7.2.4
nodejs-github-bot Feb 12, 2023
803f00a
tools: update eslint to 8.34.0
nodejs-github-bot Feb 12, 2023
217f2f6
test: add trailing commas in `test/pummel`
aduh95 Feb 12, 2023
7f2cdd3
http: add note about clientError event
ShogunPanda Feb 12, 2023
ffdd64d
doc: remove unused functions from example of `streamConsumers.text`
deokjinkim Feb 13, 2023
5d1a729
meta: update AUTHORS
nodejs-github-bot Feb 14, 2023
6b21170
benchmark: add trailing commas in `benchmark/path`
aduh95 Feb 14, 2023
aae0020
lib: enforce use of trailing commas for functions
aduh95 Feb 14, 2023
a0a6ee0
doc: add response.strictContentLength to documentation
marco-ippolito Feb 14, 2023
8b46c76
lib: enforce use of trailing commas in more files
aduh95 Feb 16, 2023
a740908
lib: delete module findPath unused params
sinkhaha Feb 17, 2023
633d3f2
stream: add abort signal for ReadableStream and WritableStream
debadree25 Feb 17, 2023
5b74811
test_runner: allow nesting test within describe
MoLow Feb 17, 2023
0f75633
test,crypto: update WebCryptoAPI WPT
panva Feb 8, 2023
e60d3f2
test: add WPTRunner support for variants and generating WPT reports
panva Feb 3, 2023
f62b58a
tools: add a daily wpt.fyi synchronized report upload
panva Feb 4, 2023
87df34a
tools: fix update-ada script
anonrig Feb 7, 2023
20cb13b
deps: update ada to v1.0.1
anonrig Feb 7, 2023
bdba600
src: remove icu usage from node_string.cc
anonrig Feb 17, 2023
b4deb2f
crypto: don't assume FIPS is disabled by default
mhdawson Feb 6, 2023
1cd22e7
tools: fix bug in `prefer-primordials` lint rule
aduh95 Feb 17, 2023
f390841
src: allow optional Isolate termination in node::Stop()
codebytere Feb 17, 2023
c4ca98e
test: cover publicExponent validation in OpenSSL
tniessen Feb 18, 2023
6a8b04d
doc: move bcoe to emeriti
Feb 18, 2023
2239e24
test: fix assertions in test-snapshot-dns-lookup*
tniessen Feb 18, 2023
164bfe8
src: add initial support for single executable applications
RaisinTen Feb 18, 2023
4aebee6
lib: do not crash using workers with disabled shared array buffers
BridgeAR Feb 18, 2023
a79a8bf
doc: add debadree25 to collaborators
debadree25 Feb 18, 2023
2ed3875
deps: update undici to 5.20.0
nodejs-github-bot Feb 18, 2023
6c99703
deps: update corepack to 0.16.0
nodejs-github-bot Feb 18, 2023
f192b83
esm: misc test refactors
GeoffreyBooth Feb 18, 2023
6ad01fd
test: fix `test-net-autoselectfamily` for kernel without IPv6 support
LiviaMedeiros Feb 18, 2023
6b64a94
stream: add trailing commas in stream source files
aduh95 Feb 18, 2023
7756438
stream: add trailing commas in webstream source files
aduh95 Feb 18, 2023
fa600fe
lib: add trailing commas in `internal/process`
aduh95 Feb 18, 2023
ddf5002
test_runner: parse non-ascii character correctly
mertcanaltin Feb 18, 2023
c781a48
build: do not disable inspector when intl is disabled
Trott Mar 30, 2021
e15ddba
build: add GitHub Action for coverage with --without-intl
Trott Mar 28, 2021
152a3c7
process: print versions by sort
himself65 Feb 18, 2023
60a6126
deps: upgrade npm to 9.5.0
npm-cli-bot Feb 18, 2023
00a428e
2023-02-21, Version 19.7.0 (Current)
MylesBorins Feb 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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: #46017
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Feb 18, 2023
commit 61192892515c365fc867f323e7fd736d6b3aabee
12 changes: 12 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

<!-- YAML
added: REPLACEME
-->

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`

<!-- YAML
Expand Down Expand Up @@ -2346,6 +2357,7 @@ done
[`unhandledRejection`]: process.md#event-unhandledrejection
[`v8.startupSnapshot` API]: v8.md#startup-snapshot-api
[`worker_threads.threadId`]: worker_threads.md#workerthreadid
[collecting code coverage from tests]: test.md#collecting-code-coverage
[conditional exports]: packages.md#conditional-exports
[context-aware]: addons.md#context-aware-addons
[debugger]: debugger.md
Expand Down
87 changes: 87 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,54 @@ Otherwise, the test is considered to be a failure. Test files must be
executable by Node.js, but are not required to use the `node:test` module
internally.

## Collecting code coverage

When Node.js is started with the [`--test-coverage`][] command-line flag, code
coverage is collected and statistics are reported once all tests have completed.
If the [`NODE_V8_COVERAGE`][] environment variable is used to specify a
code coverage directory, the generated V8 coverage files are written to that
directory. Node.js core modules and files within `node_modules/` directories
are not included in the coverage report. If coverage is enabled, the coverage
report is sent to any [test reporters][] via the `'test:coverage'` event.

Coverage can be disabled on a series of lines using the following
comment syntax:

```js
/* node:coverage disable */
if (anAlwaysFalseCondition) {
// Code in this branch will never be executed, but the lines are ignored for
// coverage purposes. All lines following the 'disable' comment are ignored
// until a corresponding 'enable' comment is encountered.
console.log('this is never executed');
}
/* node:coverage enable */
```

Coverage can also be disabled for a specified number of lines. After the
specified number of lines, coverage will be automatically reenabled. If the
number of lines is not explicitly provided, a single line is ignored.

```js
/* node:coverage ignore next */
if (anAlwaysFalseCondition) { console.log('this is never executed'); }

/* node:coverage ignore next 3 */
if (anAlwaysFalseCondition) {
console.log('this is never executed');
}
```

The test runner's code coverage functionality has the following limitations,
which will be addressed in a future Node.js release:

* Although coverage data is collected for child processes, this information is
not included in the coverage report. Because the command line test runner uses
child processes to execute test files, it cannot be used with `--test-coverage`.
* Source maps are not supported.
* Excluding specific files or directories from the coverage report is not
supported.

## Mocking

The `node:test` module supports mocking during testing via a top-level `mock`
Expand Down Expand Up @@ -1215,6 +1263,42 @@ A successful call to [`run()`][] method will return a new {TestsStream}
object, streaming a series of events representing the execution of the tests.
`TestsStream` will emit events, in the order of the tests definition

### Event: `'test:coverage'`

* `data` {Object}
* `summary` {Object} An object containing the coverage report.
* `files` {Array} An array of coverage reports for individual files. Each
report is an object with the following schema:
* `path` {string} The absolute path of the file.
* `totalLineCount` {number} The total number of lines.
* `totalBranchCount` {number} The total number of branches.
* `totalFunctionCount` {number} The total number of functions.
* `coveredLineCount` {number} The number of covered lines.
* `coveredBranchCount` {number} The number of covered branches.
* `coveredFunctionCount` {number} The number of covered functions.
* `coveredLinePercent` {number} The percentage of lines covered.
* `coveredBranchPercent` {number} The percentage of branches covered.
* `coveredFunctionPercent` {number} The percentage of functions covered.
* `uncoveredLineNumbers` {Array} An array of integers representing line
numbers that are uncovered.
* `totals` {Object} An object containing a summary of coverage for all
files.
* `totalLineCount` {number} The total number of lines.
* `totalBranchCount` {number} The total number of branches.
* `totalFunctionCount` {number} The total number of functions.
* `coveredLineCount` {number} The number of covered lines.
* `coveredBranchCount` {number} The number of covered branches.
* `coveredFunctionCount` {number} The number of covered functions.
* `coveredLinePercent` {number} The percentage of lines covered.
* `coveredBranchPercent` {number} The percentage of branches covered.
* `coveredFunctionPercent` {number} The percentage of functions covered.
* `workingDirectory` {string} The working directory when code coverage
began. This is useful for displaying relative path names in case the tests
changed the working directory of the Node.js process.
* `nesting` {number} The nesting level of the test.

Emitted when code coverage is enabled and all tests have completed.

### Event: `'test:diagnostic'`

* `data` {Object}
Expand Down Expand Up @@ -1595,6 +1679,7 @@ added:

[TAP]: https://testanything.org/
[`--import`]: cli.md#--importmodule
[`--test-coverage`]: cli.md#--test-coverage
[`--test-name-pattern`]: cli.md#--test-name-pattern
[`--test-only`]: cli.md#--test-only
[`--test-reporter-destination`]: cli.md#--test-reporter-destination
Expand All @@ -1603,6 +1688,7 @@ added:
[`MockFunctionContext`]: #class-mockfunctioncontext
[`MockTracker.method`]: #mockmethodobject-methodname-implementation-options
[`MockTracker`]: #class-mocktracker
[`NODE_V8_COVERAGE`]: cli.md#node_v8_coveragedir
[`SuiteContext`]: #class-suitecontext
[`TestContext`]: #class-testcontext
[`context.diagnostic`]: #contextdiagnosticmessage
Expand All @@ -1613,4 +1699,5 @@ added:
[describe options]: #describename-options-fn
[it options]: #testname-options-fn
[stream.compose]: stream.md#streamcomposestreams
[test reporters]: #test-reporters
[test runner execution model]: #test-runner-execution-model
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ Specify the minimum allocation from the OpenSSL secure heap. The default is 2. T
.It Fl -test
Starts the Node.js command line test runner.
.
.It Fl -test-coverage
Enable code coverage in the test runner.
.
.It Fl -test-name-pattern
A regular expression that configures the test runner to only execute tests
whose name matches the provided pattern.
Expand Down
43 changes: 13 additions & 30 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
exposeInterface,
exposeLazyInterfaces,
defineReplaceableLazyAttribute,
setupCoverageHooks,
} = require('internal/util');

const {
Expand Down Expand Up @@ -66,15 +67,7 @@ function prepareExecution(options) {
setupFetch();
setupWebCrypto();
setupCustomEvent();

// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
// to child processes even when they switch cwd.
if (process.env.NODE_V8_COVERAGE) {
process.env.NODE_V8_COVERAGE =
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
}

setupCodeCoverage();
setupDebugEnv();
// Process initial diagnostic reporting configuration, if present.
initializeReport();
Expand Down Expand Up @@ -304,6 +297,17 @@ function setupWebCrypto() {
}
}

function setupCodeCoverage() {
// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
// to child processes even when they switch cwd. Don't do anything if the
// --test-coverage flag is present, as the test runner will handle coverage.
if (process.env.NODE_V8_COVERAGE && !getOptionValue('--test-coverage')) {
process.env.NODE_V8_COVERAGE =
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
}
}

// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is
// removed.
function setupCustomEvent() {
Expand All @@ -315,27 +319,6 @@ function setupCustomEvent() {
exposeInterface(globalThis, 'CustomEvent', CustomEvent);
}

// Setup User-facing NODE_V8_COVERAGE environment variable that writes
// ScriptCoverage to a specified file.
function setupCoverageHooks(dir) {
const cwd = require('internal/process/execution').tryGetCwd();
const { resolve } = require('path');
const coverageDirectory = resolve(cwd, dir);
const { sourceMapCacheToObject } =
require('internal/source_map/source_map_cache');

if (process.features.inspector) {
internalBinding('profiler').setCoverageDirectory(coverageDirectory);
internalBinding('profiler').setSourceMapCacheGetter(sourceMapCacheToObject);
} else {
process.emitWarning('The inspector is disabled, ' +
'coverage could not be collected',
'Warning');
return '';
}
return coverageDirectory;
}

function setupStacktracePrinterOnSigint() {
if (!getOptionValue('--trace-sigint')) {
return;
Expand Down
Loading