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

node:test describe filter #47281

Closed
loynoir opened this issue Mar 28, 2023 · 4 comments
Closed

node:test describe filter #47281

loynoir opened this issue Mar 28, 2023 · 4 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@loynoir
Copy link

loynoir commented Mar 28, 2023

What is the problem this feature will solve?

  • filter describe/it
  • filter nested describe/it

What is the feature you are proposing to solve the problem?

Reproduce

lib.mjs

code
import { setTimeout as setTimeoutAsync } from "node:timers/promises";

/**
 * @param {number} [data]
 */
export async function cheapFn(data) {
  console.log(`calling ${cheapFn.name}(${data})`);
  await setTimeoutAsync(1);

  if (data) throw new Error();
  return true;
}

/**
 * @param {number} [data]
 */
export async function expensiveFn(data) {
  console.log(`calling ${expensiveFn.name}(${data})`);
  await setTimeoutAsync(3000);

  if (data) throw new Error();
  return true;
}

test.mjs

code
import { describe, it } from "node:test";
import { doesNotReject, rejects } from "node:assert";
import { cheapFn, expensiveFn } from "./lib.mjs";

describe(cheapFn.name, () => {
  it(doesNotReject.name, async () => {
    await doesNotReject(() => cheapFn(0));
  });

  it(rejects.name, async () => {
    await rejects(() => cheapFn(1));
  });
});

describe(expensiveFn.name, () => {
  it(doesNotReject.name, async () => {
    await doesNotReject(() => expensiveFn(0));
  });

  it(rejects.name, async () => {
    await rejects(() => expensiveFn(1));
  });
});

Actual

$ node --test-reporter=spec --test-name-pattern=cheapFn ./test.mjs
▶ cheapFn
  ﹣ doesNotReject (0.386277ms) # SKIP
  ﹣ rejects (0.070959ms) # SKIP
▶ cheapFn (1.834906ms)

﹣ expensiveFn (0.065669ms) # SKIP
ℹ tests 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 7.875541

describe(cheapFn) is matched, but child it() are all skipped.

Expected

$ node --test-reporter=spec --group-name-pattern=cheapFn ./test.mjs
calling cheapFn(0)
calling cheapFn(1)
▶ cheapFn
  ✔ doesNotReject (2.892296ms)
  ✔ rejects (0.523082ms)
▶ cheapFn (5.236258ms)

﹣ expensiveFn (0.068014ms) # SKIP
ℹ tests 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 9.044444

describe(cheapFn) is matched, are child it() are all matched, unless it() conflict with --test-name-pattern

What alternatives have you considered?

Join nested name, and match against test-name-pattern.

This will be super simple, but

  • will break the current meaning of test-name-pattern
  • need to discuss the seperator

Something like

> var describeNames = ['level1', 'level2'];
> var itName = 'should pass'
> var testNamePattern = /level1:level2:should pass/
>
> testNamePattern.test([...describeNames,itName].join(':'))
true
@loynoir loynoir added the feature request Issues that request new features to be added to Node.js. label Mar 28, 2023
@cjihrig
Copy link
Contributor

cjihrig commented Mar 28, 2023

Thanks for the report. Currently, you can specify --test-name-pattern multiple times to get the functionality you want. Specifying a fully qualified test name is a duplicate of #46728, so I'm going to close this out.

@cjihrig cjihrig closed this as completed Mar 28, 2023
@loynoir
Copy link
Author

loynoir commented Mar 28, 2023

@cjihrig

Specify --test-name-pattern multiple times does not work.

Doing so will call expensiveFn.

$ node --test-reporter=spec --test-name-pattern=cheapFn --test-name-pattern='.*' ./test.mjs
calling cheapFn(0)
calling cheapFn(1)
▶ cheapFn
  ✔ doesNotReject (2.859524ms)
calling expensiveFn(0)
  ✔ rejects (0.491546ms)
▶ cheapFn (5.284205ms)

calling expensiveFn(1)
▶ expensiveFn
  ✔ doesNotReject (3003.84589ms)
  ✔ rejects (3004.548911ms)
▶ expensiveFn (6009.70523ms)

@cjihrig
Copy link
Contributor

cjihrig commented Mar 28, 2023

It works, it's just clunky and can definitely be improved (#46728 😄). You're passing a pattern of .*, which matches everything.

@loynoir
Copy link
Author

loynoir commented Mar 28, 2023

It works, it's just clunky and can definitely be improved (#46728 smile). You're passing a pattern of .*, which matches everything.

I just want to match everything under cheapFn, but seems repeatable test-name-pattern don't know level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

2 participants