Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: detect only tests when isolation is off #54832

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ changes:
-->

Configures the test runner to only execute top level tests that have the `only`
option set.
option set. This flag is not necessary when test isolation is disabled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should link to the test isolation docs


### `--test-reporter`

Expand Down
8 changes: 4 additions & 4 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ const { describe, it } = require('node:test');

## `only` tests

If Node.js is started with the [`--test-only`][] command-line option, it is
possible to skip all tests except for a selected subset by passing
the `only` option to the tests that should run. When a test with the `only`
option is set, all subtests are also run.
If Node.js is started with the [`--test-only`][] command-line option, or test
isolation is disabled, it is possible to skip all tests except for a selected
subset by passing the `only` option to the tests that should run. When a test
with the `only` option is set, all subtests are also run.
cjihrig marked this conversation as resolved.
Show resolved Hide resolved
cjihrig marked this conversation as resolved.
Show resolved Hide resolved
If a suite has the `only` option set, all tests within the suite are run,
unless it has descendants with the `only` option set, in which case only those
tests are run.
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ testResources.set(reporterScope.asyncId(), reporterScope);

function createTestTree(rootTestOptions, globalOptions) {
const buildPhaseDeferred = createDeferredPromise();
const isFilteringByName = globalOptions.testNamePatterns ||
globalOptions.testSkipPatterns;
const isFilteringByOnly = globalOptions.only || (globalOptions.isTestRunner &&
globalOptions.isolation === 'none');
const harness = {
__proto__: null,
buildPromise: buildPhaseDeferred.promise,
Expand All @@ -62,6 +66,8 @@ function createTestTree(rootTestOptions, globalOptions) {
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
teardown: null,
snapshotManager: null,
isFilteringByName,
isFilteringByOnly,
async waitForBuildPhase() {
if (harness.buildSuites.length > 0) {
await SafePromiseAllReturnVoid(harness.buildSuites);
Expand Down
97 changes: 57 additions & 40 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ class Test extends AsyncResource {
this.parent = parent;
this.testNumber = 0;
this.outputSubtestCount = 0;
this.filteredSubtestCount = 0;
this.diagnostics = [];
this.filtered = false;
this.filteredByName = false;
this.hasOnlyTests = false;

if (parent === null) {
this.root = this;
Expand All @@ -413,26 +415,48 @@ class Test extends AsyncResource {
} else {
const nesting = parent.parent === null ? parent.nesting :
parent.nesting + 1;
const { config, isFilteringByName, isFilteringByOnly } = parent.root.harness;

this.root = parent.root;
this.harness = null;
this.config = this.root.harness.config;
this.config = config;
this.concurrency = parent.concurrency;
this.nesting = nesting;
this.only = only ?? (parent.only && !parent.runOnlySubtests);
this.only = only;
this.reporter = parent.reporter;
this.runOnlySubtests = false;
this.childNumber = parent.subtests.length + 1;
this.timeout = parent.timeout;
this.entryFile = parent.entryFile;

if (this.willBeFiltered()) {
this.filtered = true;
this.parent.filteredSubtestCount++;
if (isFilteringByName) {
this.filteredByName = this.willBeFilteredByName();
if (!this.filteredByName) {
for (let t = this.parent; t !== null && t.filteredByName; t = t.parent) {
t.filteredByName = false;
}
}
}

if (this.config.only && only === false) {
fn = noop;
if (isFilteringByOnly) {
if (this.only) {
// If filtering impacts the tests within a suite, then the suite only
// runs those tests. If filtering does not impact the tests within a
// suite, then all tests are run.
this.parent.runOnlySubtests = true;

if (this.parent === this.root || this.parent.startTime === null) {
for (let t = this.parent; t !== null && !t.hasOnlyTests; t = t.parent) {
t.hasOnlyTests = true;
}
}
} else if (this.only === false) {
fn = noop;
}
} else if (only || this.parent.runOnlySubtests) {
const warning =
"'only' and 'runOnly' require the --test-only command-line option.";
this.diagnostic(warning);
}
}

Expand Down Expand Up @@ -491,7 +515,6 @@ class Test extends AsyncResource {
this.endTime = null;
this.passed = false;
this.error = null;
this.diagnostics = [];
this.message = typeof skip === 'string' ? skip :
typeof todo === 'string' ? todo : null;
this.activeSubtests = 0;
Expand All @@ -509,12 +532,6 @@ class Test extends AsyncResource {
ownAfterEachCount: 0,
};

if (!this.config.only && (only || this.parent?.runOnlySubtests)) {
const warning =
"'only' and 'runOnly' require the --test-only command-line option.";
this.diagnostic(warning);
}

if (loc === undefined) {
this.loc = undefined;
} else {
Expand Down Expand Up @@ -542,9 +559,27 @@ class Test extends AsyncResource {
}
}

willBeFiltered() {
if (this.config.only && !this.only) return true;
applyFilters() {
if (this.error) {
// Never filter out errors.
return;
}

if (this.filteredByName) {
this.filtered = true;
return;
}

if (this.root.harness.isFilteringByOnly && !this.only && !this.hasOnlyTests) {
if (this.parent.runOnlySubtests ||
this.parent.hasOnlyTests ||
this.only === false) {
this.filtered = true;
}
}
}

willBeFilteredByName() {
const { testNamePatterns, testSkipPatterns } = this.config;

if (testNamePatterns && !testMatchesPattern(this, testNamePatterns)) {
Expand Down Expand Up @@ -757,12 +792,10 @@ class Test extends AsyncResource {
ArrayPrototypePush(this.diagnostics, message);
}

get shouldFilter() {
return this.filtered && this.parent?.filteredSubtestCount > 0;
}

start() {
if (this.shouldFilter) {
this.applyFilters();

if (this.filtered) {
noopTestStream ??= new TestsStream();
this.reporter = noopTestStream;
this.run = this.filteredRun;
Expand Down Expand Up @@ -970,7 +1003,7 @@ class Test extends AsyncResource {
this.mock?.reset();

if (this.parent !== null) {
if (!this.shouldFilter) {
if (!this.filtered) {
const report = this.getReportDetails();
report.details.passed = this.passed;
this.testNumber ||= ++this.parent.outputSubtestCount;
Expand Down Expand Up @@ -1159,7 +1192,7 @@ class TestHook extends Test {
getRunArgs() {
return this.#args;
}
willBeFiltered() {
willBeFilteredByName() {
return false;
}
postRun() {
Expand Down Expand Up @@ -1192,7 +1225,6 @@ class Suite extends Test {
this.fn = options.fn || this.fn;
this.skipped = false;
}
this.runOnlySubtests = this.config.only;

try {
const { ctx, args } = this.getRunArgs();
Expand All @@ -1216,21 +1248,6 @@ class Suite extends Test {

postBuild() {
this.buildPhaseFinished = true;
if (this.filtered &&
(this.filteredSubtestCount !== this.subtests.length || this.error)) {
// A suite can transition from filtered to unfiltered based on the
// tests that it contains - in case of children matching patterns.
this.filtered = false;
this.parent.filteredSubtestCount--;
} else if (
this.config.only &&
this.config.testNamePatterns == null &&
this.config.testSkipPatterns == null &&
this.filteredSubtestCount === this.subtests.length
) {
// If no subtests are marked as "only", run them all
this.filteredSubtestCount = 0;
}
}

getRunArgs() {
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-runner-no-isolation-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ test('works with --test-name-pattern', () => {

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.match(stdout, /# tests 1/);
assert.match(stdout, /# tests 0/);
assert.match(stdout, /# suites 0/);
assert.match(stdout, /# pass 1/);
assert.match(stdout, /ok 1 - test one/);
});

test('works with --test-skip-pattern', () => {
Expand Down
9 changes: 0 additions & 9 deletions test/parallel/test-runner-no-isolation-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ const order = [
'afterEach one: suite one - test',
'afterEach two: suite one - test',

'beforeEach(): global',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these hooks be called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for "test one" which is filtered out.

'beforeEach one: test one',
'beforeEach two: test one',
'test one',

'afterEach(): global',
'afterEach one: test one',
'afterEach two: test one',

'before suite two: suite two',
'beforeEach(): global',
'beforeEach one: suite two - test',
Expand Down
Loading