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: make --test-name-pattern recursive #48382

Merged
merged 2 commits into from
Jun 11, 2023
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
27 changes: 16 additions & 11 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,11 @@ class Test extends AsyncResource {
this.timeout = timeout;
}

if (testNamePatterns !== null) {
// eslint-disable-next-line no-use-before-define
const match = this instanceof TestHook || ArrayPrototypeSome(
testNamePatterns,
(re) => RegExpPrototypeExec(re, name) !== null,
);
this.name = name;
this.parent = parent;

if (!match) {
skip = 'test name does not match pattern';
}
if (testNamePatterns !== null && !this.matchesTestNamePatterns()) {
skip = 'test name does not match pattern';
}

if (testOnlyFlag && !this.only) {
Expand All @@ -276,8 +271,6 @@ class Test extends AsyncResource {
this.fn = fn;
this.harness = null; // Configured on the root test by the test harness.
this.mock = null;
this.name = name;
this.parent = parent;
this.cancelled = false;
this.skipped = !!skip;
this.isTodo = !!todo;
Expand All @@ -302,6 +295,11 @@ class Test extends AsyncResource {
}
}

matchesTestNamePatterns() {
return ArrayPrototypeSome(testNamePatterns, (re) => RegExpPrototypeExec(re, this.name) !== null) ||
this.parent?.matchesTestNamePatterns();
}

hasConcurrency() {
return this.concurrency > this.activeSubtests;
}
Expand Down Expand Up @@ -750,6 +748,9 @@ class TestHook extends Test {
getRunArgs() {
return this.#args;
}
matchesTestNamePatterns() {
return true;
}
postRun() {
}
}
Expand All @@ -759,6 +760,10 @@ class Suite extends Test {
constructor(options) {
super(options);

if (testNamePatterns !== null && !options.skip && !options.todo) {
this.fn = options.fn || this.fn;
this.skipped = false;
}
this.runOnlySubtests = testOnlyFlag;

try {
Expand Down
28 changes: 24 additions & 4 deletions test/fixtures/test-runner/output/name_pattern.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --no-warnings --test-name-pattern=enabled --test-name-pattern=/pattern/i
// Flags: --no-warnings --test-name-pattern=enabled --test-name-pattern=yes --test-name-pattern=/pattern/i
'use strict';
const common = require('../../../common');
const {
Expand All @@ -18,7 +18,7 @@ it('top level it enabled', common.mustCall());
it('top level it disabled', common.mustNotCall());
it.skip('top level skipped it disabled', common.mustNotCall());
it.skip('top level skipped it enabled', common.mustNotCall());
describe('top level describe disabled', common.mustNotCall());
describe('top level describe never disabled', common.mustCall());
describe.skip('top level skipped describe disabled', common.mustNotCall());
describe.skip('top level skipped describe enabled', common.mustNotCall());
test('top level runs because name includes PaTtErN', common.mustCall());
Expand All @@ -38,10 +38,30 @@ describe('top level describe enabled', () => {
afterEach(common.mustCall(3));
after(common.mustCall());

it('nested it disabled', common.mustNotCall());
it('nested it not disabled', common.mustCall());
it('nested it enabled', common.mustCall());
describe('nested describe disabled', common.mustNotCall());
describe('nested describe not disabled', common.mustCall());
describe('nested describe enabled', common.mustCall(() => {
it('is enabled', common.mustCall());
}));
});

describe('yes', function() {
it('no', () => {});
it('yes', () => {});

describe('maybe', function() {
it('no', () => {});
it('yes', () => {});
});
});

describe('no', function() {
it('no', () => {});
it('yes', () => {});

describe('maybe', function() {
it('no', () => {});
it('yes', () => {});
});
});
90 changes: 79 additions & 11 deletions test/fixtures/test-runner/output/name_pattern.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ ok 7 - top level skipped it enabled # SKIP
---
duration_ms: *
...
# Subtest: top level describe disabled
ok 8 - top level describe disabled # SKIP test name does not match pattern
# Subtest: top level describe never disabled
ok 8 - top level describe never disabled
---
duration_ms: *
type: 'suite'
Expand Down Expand Up @@ -69,8 +69,8 @@ ok 12 - top level test enabled
duration_ms: *
...
# Subtest: top level describe enabled
# Subtest: nested it disabled
ok 1 - nested it disabled # SKIP test name does not match pattern
# Subtest: nested it not disabled
ok 1 - nested it not disabled
---
duration_ms: *
...
Expand All @@ -79,8 +79,8 @@ ok 12 - top level test enabled
---
duration_ms: *
...
# Subtest: nested describe disabled
ok 3 - nested describe disabled # SKIP test name does not match pattern
# Subtest: nested describe not disabled
ok 3 - nested describe not disabled
---
duration_ms: *
type: 'suite'
Expand All @@ -103,12 +103,80 @@ ok 13 - top level describe enabled
duration_ms: *
type: 'suite'
...
1..13
# tests 13
# suites 6
# pass 6
# Subtest: yes
# Subtest: no
ok 1 - no
---
duration_ms: *
...
# Subtest: yes
ok 2 - yes
---
duration_ms: *
...
# Subtest: maybe
# Subtest: no
ok 1 - no
---
duration_ms: *
...
# Subtest: yes
ok 2 - yes
---
duration_ms: *
...
1..2
ok 3 - maybe
---
duration_ms: *
type: 'suite'
...
1..3
ok 14 - yes
---
duration_ms: *
type: 'suite'
...
# Subtest: no
# Subtest: no
ok 1 - no # SKIP test name does not match pattern
---
duration_ms: *
...
# Subtest: yes
ok 2 - yes
---
duration_ms: *
...
# Subtest: maybe
# Subtest: no
ok 1 - no # SKIP test name does not match pattern
---
duration_ms: *
...
# Subtest: yes
ok 2 - yes
---
duration_ms: *
...
1..2
ok 3 - maybe
---
duration_ms: *
type: 'suite'
...
1..3
ok 15 - no
---
duration_ms: *
type: 'suite'
...
1..15
# tests 21
# suites 10
# pass 13
# fail 0
# cancelled 0
# skipped 7
# skipped 8
# todo 0
# duration_ms *
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/output/name_pattern_with_only.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { test } = require('node:test');

test('enabled and only', { only: true }, common.mustCall(async (t) => {
await t.test('enabled', common.mustCall());
await t.test('disabled', common.mustNotCall());
await t.test('disabled but parent not', common.mustCall());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this running? Is it because the parent is enabled? If so, I don't think that's what #46728 is about.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@targos targos Jun 9, 2023

Choose a reason for hiding this comment

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

FWIW, this change corresponds to my expectations.
A more realistic use case:

describe('add', () => {
  it('should return 2', () => assert.strictEqual(add(1, 1), 2));
  it('should return 10', () => assert.strictEqual(add(8, 2), 10));
  it('should throw', () => assert.throws(() => add(), TypeError));
});

describe('subtract', () => {
  // ...
});

node --test-name-pattern=add should run all children of describe('add')

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with this PR now that it's clear this is not fixing #46728.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you please approve? :)

}));

test('enabled but not only', common.mustNotCall());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ TAP version 13
---
duration_ms: *
...
# Subtest: disabled
ok 2 - disabled # SKIP test name does not match pattern
# Subtest: disabled but parent not
ok 2 - disabled but parent not
---
duration_ms: *
...
Expand All @@ -33,9 +33,9 @@ ok 4 - not only and does not match pattern # SKIP 'only' option not set
1..4
# tests 6
# suites 0
# pass 2
# pass 3
# fail 0
# cancelled 0
# skipped 4
# skipped 3
# todo 0
# duration_ms *