Skip to content

Filter tests by name with a regular expression in ng test #23005

@flavianh

Description

@flavianh

I've struggled to find how to filter tests ng test for one specific case: running slow tests separately but still with Karma / Jasmine.
Two pre-existing (but closed) issues (#3603 and #3436) partially addressed the problem but I think I managed to find a more elegant solution than @danday74 and @EugeneSnihovsky suggestions, though they address another problem: filtering by file name.

ng test --include did not work for me as it only includes the files that are given to it; meaning that if your test file depends on a production file, both have to be mentioned. This is not useful at all.

In the end my solution is quite simple (works for karma + jasmine) and filters by test name only:

// some-test.spec.js

it("#slow run a full check", () => {...});

it("run a rapid check", () => {...});
// karma.conf.js

// Use an environment variable to pass the search regex
const KARMA_GREP = process.env.KARMA_GREP;

// Add client.args in config
module.exports = function (config) {
  config.set({
    // Use the karma-jasmine client configuration
    client: {
      ...(KARMA_GREP && {
        args: ["--grep", KARMA_GREP],
      }),
    },
}

And run in the CLI

# Run #slow tests
KARMA_GREP='#slow' ng test
# Run not #slow tests
KARMA_GREP='/^(?!.*#slow).*$/' ng test

Since KARMA_GREP can be a regular expression, it is quite versatile

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions