nodejs / nodejs/node

--test-name-pattern needing to come before filenames is hostile to npm scripts

Aperta
#51,384 17 commenti 27 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature request never-stale test_runner
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l’invocazione di npm test con --test-name-pattern e confrontala con il comando funzionante in cui l’opzione precede --test e i nomi dei file. Analizza la gestione della riga di comando di Node per --test e --test-name-pattern; per considerare il lavoro completato è necessario un approccio definito che supporti questo caso d’uso di npm-script, probabilmente con test che coprano l’ordine degli argomenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, node.js
Ambito
cli, testing-qa
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.