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-name-pattern needing to come before filenames is hostile to npm scripts #51384

Open
domenic opened this issue Jan 5, 2024 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. test_runner

Comments

@domenic
Copy link
Contributor

domenic commented Jan 5, 2024

What is the problem this feature will solve?

It is common practice to set up npm scripts for testing. E.g.

{
  "test": "node --test tests/*.js more-tests/*.js"
}

However, this cannot be combined with --test-name-pattern. Attempting to do so, e.g.

npm test -- --test-name-pattern="my pattern"

will not work, because this gets translated to

node --test tests/*.js more-tests/*.js --test-name-pattern="my pattern"

which, I believe, ends up passing --test-name-pattern="my pattern" as an argument to these test files, instead of passing it as an argument to the test runner. The correct invocation is

node --test-name-pattern="my pattern" --test tests/*.js more-tests/*.js

but this is impossible to do via npm scripts, it seems. (See alternatives considered.)

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

I don't know what a good solution to this would be. Some possible ideas:

  • Special-case command line processing such that when --test is present, node grabs the --test-name-pattern argument for itself instead of passing it to scripts?

  • Introduce a new binary, e.g. node_test, which processes command-line arguments in such a way? I believe this is how most test runners behave.

  • Introduce a file-based customization of the test runner, including which tests to run, so that I don't have to pass the test filenames as arguments to the test runner in a way that causes this problem?

  • Improve npm scripts to support a better method of passing arguments in the middle of the script? (See below.)

What alternatives have you considered?

I investigated how to get npm scripts to substitute in arguments you pass to npm run into the script command, so that the translation becomes the correct one. This is a well-studied problem, and the following two Stack Overflow posts have the best answers, as far as I can tell:

None of them seem very satisfactory, unfortunately. In particular, if you want something that works cross-platform, you basically have to write a wrapper script.

As an alternative, I could continue using other test runners, which support npm scripts better.

@domenic domenic added the feature request Issues that request new features to be added to Node.js. label Jan 5, 2024
@benjamingr
Copy link
Member

@nodejs/test_runner @MoLow wdyt?

FWIW:

Introduce a file-based customization of the test runner, including which tests to run, so that I don't have to pass the test filenames as arguments to the test runner in a way that causes this problem?

You can do this using run, additionally you can pass parameters through the NODE_OPTIONS environment variable as a workaround.

@jacob-ebey
Copy link

jacob-ebey commented May 9, 2024

I run into this all the time and if I could remove the script test:target for this use-case that would be phenomenal. Every single other test runner worth while accounts for this and has become part of my workflow that breaks in these setups.

@RedYetiDev
Copy link
Member

RedYetiDev commented May 15, 2024

you can pass parameters through the NODE_OPTIONS environment variable as a workaround.

--test-name-pattern and --test-skip-pattern aren't allowed in NODE_OPTIONS, would you like me to open a PR to change that?

~ node -v          
v22.1.0~ NODE_OPTIONS="--test-name-pattern" node
node: --test-name-pattern is not allowed in NODE_OPTIONS~ NODE_OPTIONS="--test-name-pattern=\"foo\"" node
node: --test-name-pattern= is not allowed in NODE_OPTIONS

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. test_runner
Projects
Status: Pending Triage
Development

No branches or pull requests

5 participants