Repository navigation
--test-name-pattern needing to come before filenames is hostile to npm scripts #51384
Description
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
--testis present,nodegrabs the--test-name-patternargument 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriaged
- StatusShow more project fieldsNo status
Activity
benjamingr commentedon Jan 5, 2024
@nodejs/test_runner @MoLow wdyt?
FWIW:
You can do this using
run, additionally you can pass parameters through theNODE_OPTIONSenvironment variable as a workaround.jacob-ebey commentedon May 9, 2024
I run into this all the time and if I could remove the script
test:targetfor 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.avivkeller commentedon May 15, 2024
--test-name-patternand--test-skip-patternaren't allowed inNODE_OPTIONS, would you like me to open a PR to change that?cjihrig commentedon Aug 27, 2024
If anyone wants to pick this up, it would involve updating the test runner's
parseCommandLine()function to also consider values inprocess.argv. I don't think it would be difficult to implement technically, but there are a few caveats to consider:process.execArgvis how all Node core CLI flags work. Since this would be introducing an inconsistency, I'm not sure how well it would be received.process.argv. I think--test,--experimental-test-coverage, and--experimental-test-isolationare currently the only test runner flags used in the C++ layer. Another inconsistency to be aware of.--requireor--importinprocess.argvto work, but they wouldn't.--test) is being used, or any timenode:testis used.My feeling is that doing this might introduce more issues than it solves, but I wouldn't block anyone from doing it (others people might though).
avivkeller commentedon Aug 27, 2024
FWIW:
Currently, (without any changes) there are a few ways to achieve this goal (as @benjamingr previously mentioned):
run()- You can use therunfunction to specify patternsNODE_OPTIONS- As of cli: allow--test-[name/skip]-patterninNODE_OPTIONS#53001, you can use environment variables to specify patterns.So I'm not sure if it's worth checking a whole new set of arguments when there are other ways to achieve this.
jacksonthall22 commentedon Sep 26, 2024
I'll throw in my 2 cents—I'm only just learning about node's testing suite, but it is definitely misleading that both
--testand--test-name-pattern"do the testing". Just wasted about 2 hours realizing I could not easily solve OP's same problem before landing here. Seems like--testshould be responsible for doing the testing, with the next arg being the path/pattern, and then just have--test-name-pattern=.../--test-skip-pattern=.../etc. act like modifiers on--test's default behavior. I would not expect those flags to actually "execute" the tests on their own (and hence they should not require a path/pattern arg after them). That would allow for an ideal scenario where both/many modifiers could be used (not even sure if that's possible now?):package.json{ "scripts": { "build": "node --test tests/*.js" } }and use it like this:
No idea what the technical/practical implications of this change would be though since I'm new here, just seems like a more logical interface.
krutoo commentedon Mar 9, 2025
Same issue but
run()is not suitable for me because I need to--importsetup file before tests runkrutoo commentedon Mar 9, 2025
Also looks like NODE_OPTIONS not working, i do it like this:
It becomes to:
I also have
.npmrcwith:This script provide all tests run instead only matched by pattern
krutoo commentedon Mar 19, 2025
Does anyone know when it will be possible to put a
--test-name-patternflag after a--testflag and after files glob?And will there be such a feature?
12 remaining items
leoschweizer commentedon Aug 5, 2025
@p-mcgowan awesome that you are already working on this! I ran into the same problem (but for occasionally updating snapshots with
--test-update-snapshots), and my idea would have been exactly to allow providing the glob pattern as a named (instead of positional) argument. Started poking around the codebase how this could be implemented and found you already did. So, totally +1 for that idea.p-mcgowan commentedon Aug 5, 2025
@leoschweizer thanks! i ended up going with named glob instead of positional in the linked PR - it turned out to be a better option as I mentioned in the comments.
github-actions commentedon Feb 2, 2026
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-staleIssues and PRs exempt from automated stale handling.
label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
styfle commentedon Feb 3, 2026
Please mark as
never-stalefilmaj commentedon Feb 9, 2026
Note that other test-specific flags suffer from the same issue as the OP described. For example, I may want a 'base' npm run script to run unit tests, but a second npm run script, leveraging the first one, that specifies the
--test-reporter. My use case: in CI, I want to output test results in Junit XML format for easy storage / analytics / insights in my CI provider. Ideally I could do something like:{ "scripts": { "test:unit": "node --test --experimental-test-module-mocks --experimental-test-coverage --test-coverage-exclude='test/**/*' --test-coverage-exclude='scripts/**/*' 'test/unit/**/*.test.js'", "test:unit:xml": "npm run test:unit -- --test-reporter=junit", } }vassudanagunta commentedon Mar 7, 2026
I think the underlying problem is that running tests should be a distinct command, e.g.
node testornode-test, not an option flag. Not only does this command-as-flag approach cause numerous CLI syntax headaches, this issue being but one, it flies against the intuitive notion of an option flag. A flag, or any option for that matter, adjusts the behavior of an operation, not switch it out entirely for a different operation.As I point out under a related issue, this distinction is exemplified in the difference between
node --inspectandnode inspect.If running tests were a subcommand, options could come before or after arguments in the same way most other subcommand-based CLI's allow:
Note the additional benefit: shorter and better-named options.
Passing arguments through to tests could also follow existing subcommand-based CLI conventions:
There are ways to intelligently deal with the backward compatibility concerns of introducing a
node testsubcommand. Note we already havenode inspect. Alternatively, a separatenode-testexecutable would give the same benefits, without any backward compatibility issues at all.See also #53483 by @MoLow.
test: add --test-files-glob flag
Use Node.js's built-in test runner