From c9d6e3ff04c4f16eacc0a9756d4c464888444d22 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Tue, 4 Sep 2018 17:39:19 -0700 Subject: [PATCH] coverage: expose native V8 coverage native V8 coverage reports can now be written to disk by setting the variable NODE_V8_COVERAGE=dir PR-URL: https://github.com/nodejs/node/pull/22527 Reviewed-By: Anna Henningsen Reviewed-By: Yang Guo Reviewed-By: Ruben Bridgewater Reviewed-By: Evan Lucas Reviewed-By: Rod Vagg --- doc/api/cli.md | 28 +++++ lib/child_process.js | 8 ++ lib/internal/bootstrap/node.js | 6 + lib/internal/print_help.js | 2 + lib/internal/process/coverage.js | 71 ++++++++++++ lib/internal/process/per_thread.js | 4 + node.gyp | 1 + test/fixtures/v8-coverage/basic.js | 6 + test/fixtures/v8-coverage/exit-1.js | 7 ++ test/fixtures/v8-coverage/sigint.js | 7 ++ .../v8-coverage/spawn-subprocess-no-cov.js | 5 + test/fixtures/v8-coverage/spawn-subprocess.js | 6 + test/fixtures/v8-coverage/subprocess.js | 8 ++ test/parallel/test-v8-coverage.js | 105 ++++++++++++++++++ 14 files changed, 264 insertions(+) create mode 100644 lib/internal/process/coverage.js create mode 100644 test/fixtures/v8-coverage/basic.js create mode 100644 test/fixtures/v8-coverage/exit-1.js create mode 100644 test/fixtures/v8-coverage/sigint.js create mode 100644 test/fixtures/v8-coverage/spawn-subprocess-no-cov.js create mode 100644 test/fixtures/v8-coverage/spawn-subprocess.js create mode 100644 test/fixtures/v8-coverage/subprocess.js create mode 100644 test/parallel/test-v8-coverage.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 637c1cd9050557..6b7d451d682d1c 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -625,6 +625,32 @@ Path to the file used to store the persistent REPL history. The default path is `~/.node_repl_history`, which is overridden by this variable. Setting the value to an empty string (`''` or `' '`) disables persistent REPL history. +### `NODE_V8_COVERAGE=dir` + +When set, Node.js will begin outputting [V8 JavaScript code coverage][] to the +directory provided as an argument. Coverage is output as an array of +[ScriptCoverage][] objects: + +```json +{ + "result": [ + { + "scriptId": "67", + "url": "internal/tty.js", + "functions": [] + } + ] +} +``` + +`NODE_V8_COVERAGE` will automatically propagate to subprocesses, making it +easier to instrument applications that call the `child_process.spawn()` family +of functions. `NODE_V8_COVERAGE` can be set to an empty string, to prevent +propagation. + +At this time coverage is only collected in the main thread and will not be +output for code executed by worker threads. + ### `OPENSSL_CONF=file`