Skip to content

Explicit glob argument for test runner?Β #58033

@olalonde

Description

@olalonde

What is the problem this feature will solve?

Unless I'm missing something, it seems that at the moment the glob arguments are only recognized when they are the last arguments. So the common npm run test -- --test-only or npm run test -- --test-update-snapshots pattern doesn't work. This results in having to copy/paste the glob arguments in all the test scripts. Here's what it looks like in a project of mine that uses typescript:

{
  "scripts": {
    "test": "NODE_NO_WARNINGS=1 node --test '**/*.test.ts' '**/test/**/*.ts'",
    "test:only": "NODE_NO_WARNINGS=1 node --test-only --test '**/*.test.ts' '**/test/**/*.ts'",
    "test:watch": "NODE_NO_WARNINGS=1 node --watch --watch-path-ignore=generated/ --test '**/*.test.ts' '**/test/**/*.ts'",
    "test:update": "NODE_NO_WARNINGS=1 node --test --test-update-snapshots '**/*.test.ts' '**/test/**/*.ts'",
    "test:coverage": "NODE_NO_WARNINGS=1 node --test --test-reporter=spec --test-coverage-exclude='**/generated/**' --test-coverage-exclude='node_modules/**' --test-coverage-functions=80 --experimental-test-coverage '**/*.test.ts' '**/test/**/*.ts'",
    "test:coverage:lcov": "NODE_NO_WARNINGS=1 node --test --test-reporter=lcov --test-coverage-exclude='**/generated/**' --test-coverage-exclude='node_modules/**' --test-coverage-functions=80 --experimental-test-coverage '**/*.test.ts' '**/test/**/*.ts' > coverage/lcov.info",
  }
}

This is obviously prone to error.

I tried a few different ways to avoid repeating the globs all over the place but nothing seemed to work (well, there's always the option of writing scripts outside package.json but it would be nice if it wasn't necessary).

If the globs were named, e.g. --glob='**/*.test.ts' --glob='**/test/**/*.ts' or if it was allowed to put more arguments after them e.g. --test '**/*.test.ts' '**/test/**/*.ts' --test-only, it would then be possible to rewrite the above as:

{
  "scripts": {
    "test": "NODE_NO_WARNINGS=1 node --test '**/*.test.ts' '**/test/**/*.ts'",
    "test:only": "npm test -- --test-only",
    "test:watch": "npm test -- --watch --watch-path-ignore=generated/",
    "test:update": "npm test -- --test-update-snapshots",
    "test:coverage": "npm test -- --test-reporter=spec --test-coverage-exclude='**/generated/**' --test-coverage-exclude='node_modules/**' --test-coverage-functions=80 --experimental-test-coverage",
    "test:coverage:lcov": "npm test -- --test-reporter=lcov --test-coverage-exclude='**/generated/**' --test-coverage-exclude='node_modules/**' --test-coverage-functions=80 --experimental-test-coverage > coverage/lcov.info",
  }
}

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

See above.

What alternatives have you considered?

  1. Named arguments , e.g.:

--test-include='**/*.test.ts' --test-include='/test/**/*.ts'

  1. Simply treating the globs as arguments to --test and keep parsing other arguments after.

e.g.: --test '**/*.test.ts' '**/test/**/*.ts' --test-only --other-arg --etc

  1. Configuration in package.json instead of CLI argument:
{

   "test": {
       "include": ["**/*.test.ts", "/test/**/*.ts"]
    }
}

S

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions