nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.52k stars 29.03k forks source link

subtest filtering only works with describe not with test #54056

Open dvirtz opened 1 month ago

dvirtz commented 1 month ago

Version

v22.5.1

Platform

Linux JKKV6G3 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

test

What steps will reproduce the bug?

import { describe, test } from 'node:test';

test('test 1', async (t) => {
  await test('some test');
  await test('some other test');
});

describe('test 2', async (t) => {
  await test('some test');
  await test('some other test');
});
$ node --test-name-pattern="test 1 some test" test
ℹ tests 0
ℹ suites 0
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 5.505249
$ node --test-name-pattern="test 2 some test" test
▶ test 2
  ✔ some test (0.933304ms)
▶ test 2 (2.713913ms)
ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 13.939723

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

According to the documentation

Starting Node.js with --test-name-pattern="test 1 some test" would match only some test in test 1.

Nothing says it's limited to describe.

What do you see instead?

no test runs

Additional information

No response

cjihrig commented 1 month ago

This is a doc issue IMO. To do what you want with only tests, you'd have to do something like this:

node --test-name-pattern="test 1" --test-name-pattern="some test" --test-skip-pattern="other" test.mjs

The reason is that the test runner eagerly evaluates all suites to apply the filtering. Tests are not eagerly evaluated because that means running the test, which is the opposite of what we want to happen here. In the example here, "test 1 some test" does not match any top level tests, so "test 1" is never executed and the "some test" test is never even created.