angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Filter tests by name with a regular expression in ng test

Open flavianh opened this issue 2 years ago • 3 comments

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

flavianh avatar Apr 19 '22 14:04 flavianh

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

angular-robot[bot] avatar May 02 '22 13:05 angular-robot[bot]

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

angular-robot[bot] avatar Jun 11 '22 13:06 angular-robot[bot]

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

angular-robot[bot] avatar Jun 30 '22 13:06 angular-robot[bot]

Adding this additional option to the karma builder is currently not in planning.

Let’s reconsider in the future if things change.

alan-agius4 avatar May 22 '23 19:05 alan-agius4

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.