Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Aug 18, 2022
1 parent b7fab50 commit b52fccb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion doc/api/test.md
Expand Up @@ -316,7 +316,7 @@ 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.

## `runFiles([options])`
## `run([options])`

<!-- YAML
added: REPLACEME
Expand All @@ -338,6 +338,8 @@ added: REPLACEME
If unspecified, subtests inherit this value from their parent.
**Default:** `Infinity`.
* Returns: {Promise} Resolved with `undefined` once all test files complete.
If one of the test files fails, `run()` will return a rejected `Promise`
with an [`ERR_TEST_FAILURE`][] error

## `test([name][, options][, fn])`

Expand Down Expand Up @@ -870,6 +872,7 @@ added:
[TAP]: https://testanything.org/
[`--test-only`]: cli.md#--test-only
[`--test`]: cli.md#--test
[`ERR_TEST_FAILURE`]: errors.md#err_test_failure
[`SuiteContext`]: #class-suitecontext
[`TestContext`]: #class-testcontext
[`test()`]: #testname-options-fn
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/main/test_runner.js
Expand Up @@ -3,9 +3,9 @@ const {
prepareMainThreadExecution,
markBootstrapComplete
} = require('internal/process/pre_execution');
const { runFiles } = require('internal/test_runner/runner');
const { run } = require('internal/test_runner/runner');

prepareMainThreadExecution(false);
markBootstrapComplete();

runFiles();
run();
3 changes: 2 additions & 1 deletion lib/internal/test_runner/harness.js
Expand Up @@ -14,6 +14,7 @@ const {
ERR_TEST_FAILURE,
},
} = require('internal/errors');
const { kEmptyObject } = require('internal/util');
const { getOptionValue } = require('internal/options');
const { kCancelledByParent, Test, ItTest, Suite } = require('internal/test_runner/test');

Expand All @@ -22,7 +23,7 @@ const testResources = new SafeMap();
const root = new Test({ __proto__: null, name: '<root>' });
const wasRootSetup = new SafeWeakMap();

function createTestTree(options = {}) {
function createTestTree(options = kEmptyObject) {
return setup(new Test({ __proto__: null, ...options, name: '<root>' }));
}

Expand Down
16 changes: 12 additions & 4 deletions lib/internal/test_runner/runner.js
Expand Up @@ -22,6 +22,7 @@ const {
} = require('internal/errors');
const { validateArray } = require('internal/validators');
const { kEmptyObject } = require('internal/util');
const { getOptionValue } = require('internal/options');
const { createTestTree } = require('internal/test_runner/harness');
const { kSubtestsFailed, Test } = require('internal/test_runner/test');
const {
Expand All @@ -31,6 +32,7 @@ const {
const { basename, join, resolve } = require('path');
const { once } = require('events');

const isTestRunnerCli = getOptionValue('--test');
const kFilterArgs = ['--test'];

// TODO(cjihrig): Replace this with recursive readdir once it lands.
Expand Down Expand Up @@ -137,18 +139,24 @@ function runTestFile(path, root) {
return subtest.start();
}

async function runFiles(options) {
async function run(options) {
if (options === null || typeof options !== 'object') {
options = kEmptyObject;
}
const { concurrency, timeout, signal, files } = options;
// TODO(MoLow): Allow piping report to a writable other than stdout.
const root = createTestTree({ concurrency, timeout, signal });

if (files !== undefined) {
validateArray(options, 'options.files');
validateArray(options.files, 'options.files');
}

await ArrayPrototypeMap(files ?? createTestFileList(), (path) => runTestFile(path, root));
await SafePromiseAll(ArrayPrototypeMap(files ?? createTestFileList(), (path) => runTestFile(path, root)));
await root.postRun();

if (root.error && !isTestRunnerCli) {
throw root.error;
}
}

module.exports = { runFiles };
module.exports = { run };
2 changes: 1 addition & 1 deletion lib/internal/test_runner/test.js
Expand Up @@ -537,7 +537,7 @@ class Test extends AsyncResource {
}
}

if (this.passed && failedSubtests > 0) {
if ((this.passed || this.parent === null) && failedSubtests > 0) {
const subtestString = `subtest${failedSubtests > 1 ? 's' : ''}`;
const msg = `${failedSubtests} ${subtestString} failed`;

Expand Down
4 changes: 2 additions & 2 deletions lib/test.js
@@ -1,10 +1,10 @@
'use strict';
const { ObjectAssign } = primordials;
const { test, describe, it, before, after, beforeEach, afterEach } = require('internal/test_runner/harness');
const { runFiles } = require('internal/test_runner/runner');
const { run } = require('internal/test_runner/runner');
const { emitExperimentalWarning } = require('internal/util');

emitExperimentalWarning('The test runner');

module.exports = test;
ObjectAssign(module.exports, { test, describe, it, before, after, beforeEach, afterEach, runFiles });
ObjectAssign(module.exports, { test, describe, it, before, after, beforeEach, afterEach, run });

0 comments on commit b52fccb

Please sign in to comment.