Skip to content

Commit

Permalink
test_runner: make --test-name-pattern recursive
Browse files Browse the repository at this point in the history
PR-URL: #48382
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
MoLow authored and ruyadorno committed Sep 7, 2023
1 parent c59ae86 commit 7051caf
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 31 deletions.
27 changes: 16 additions & 11 deletions lib/internal/test_runner/test.js
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 @@ -754,6 +752,9 @@ class TestHook extends Test {
getRunArgs() {
return this.#args;
}
matchesTestNamePatterns() {
return true;
}
postRun() {
}
}
Expand All @@ -763,6 +764,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
@@ -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
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
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());
}));

test('enabled but not only', common.mustNotCall());
Expand Down
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 *

0 comments on commit 7051caf

Please sign in to comment.