From 8175c65b4dd159c120acdf375a698ef353a2be03 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Mon, 15 Aug 2022 12:12:11 +0300 Subject: [PATCH] test_runner: support programmatically running `--test` PR-URL: https://github.com/nodejs/node/pull/44241 Fixes: https://github.com/nodejs/node/issues/44023 Fixes: https://github.com/nodejs/node/issues/43675 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Antoine du Hamel --- doc/api/test.md | 74 +++++++++++ lib/internal/main/test_runner.js | 144 +--------------------- lib/internal/test_runner/harness.js | 89 +++++--------- lib/internal/test_runner/runner.js | 163 +++++++++++++++++++++++++ lib/internal/test_runner/tap_stream.js | 34 +++--- lib/internal/test_runner/test.js | 58 +++++++-- lib/test.js | 19 +-- test/message/test_runner_no_tests.out | 2 +- test/parallel/test-runner-run.mjs | 69 +++++++++++ tools/doc/type-parser.mjs | 2 + 10 files changed, 424 insertions(+), 230 deletions(-) create mode 100644 lib/internal/test_runner/runner.js create mode 100644 test/parallel/test-runner-run.mjs diff --git a/doc/api/test.md b/doc/api/test.md index 442c66c1bc6578..da580f92ece53a 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -316,6 +316,35 @@ 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. +## `run([options])` + + + +* `options` {Object} Configuration options for running tests. The following + properties are supported: + * `concurrency` {number|boolean} If a number is provided, + then that many files would run in parallel. + If truthy, it would run (number of cpu cores - 1) + files in parallel. + If falsy, it would only run one file at a time. + If unspecified, subtests inherit this value from their parent. + **Default:** `true`. + * `files`: {Array} An array containing the list of files to run. + **Default** matching files from [test runner execution model][]. + * `signal` {AbortSignal} Allows aborting an in-progress test execution. + * `timeout` {number} A number of milliseconds the test execution will + fail after. + If unspecified, subtests inherit this value from their parent. + **Default:** `Infinity`. +* Returns: {TapStream} + +```js +run({ files: [path.resolve('./tests/test.js')] }) + .pipe(process.stdout); +``` + ## `test([name][, options][, fn])` + +* Extends {ReadableStream} + +A successful call to [`run()`][] method will return a new {TapStream} +object, streaming a [TAP][] output +`TapStream` will emit events, in the order of the tests definition + +### Event: `'test:diagnostic'` + +* `message` {string} The diagnostic message. + +Emitted when [`context.diagnostic`][] is called. + +### Event: `'test:fail'` + +* `data` {Object} + * `duration` {number} The test duration. + * `error` {Error} The failure casing test to fail. + * `name` {string} The test name. + * `testNumber` {number} The ordinal number of the test. + * `todo` {string|undefined} Present if [`context.todo`][] is called + * `skip` {string|undefined} Present if [`context.skip`][] is called + +Emitted when a test fails. + +### Event: `'test:pass'` + +* `data` {Object} + * `duration` {number} The test duration. + * `name` {string} The test name. + * `testNumber` {number} The ordinal number of the test. + * `todo` {string|undefined} Present if [`context.todo`][] is called + * `skip` {string|undefined} Present if [`context.skip`][] is called + +Emitted when a test passes. + ## Class: `TestContext`