Karma: feat: pass command line arguments for `start` command

Created on 2 Aug 2013  路  16Comments  路  Source: karma-runner/karma

Following on from #530

We now have:

$ karma start &
$ karma run -- --grep someTest

But that sucks for batch runs (like CI scripts), because you need to:

  • run karma start
  • parse its output (!) to see when it's ready
  • run karma run -- <args>
  • kill the first process
  • deal with other messy stuff related to managing multiple processes

There is real need for this feature, e.g segmenting travis builds by test type:

  • karma start --single-run -- --tag unit
  • karma start --single-run -- --tag integration

Options:

  • treat everything after -- as clientArgs, just like we do for run.
    @vojtajina doesn't like this, because it removes the possibility of using this for something else in the future.
  • an explicit --client-args would trigger that _ALL_ following arguments should be taken as clientArgs:
    karma start --single-run --client-args --grep unit
    This should probably grab only up to the next "--" argument though.
  • more tediously, specifying them individually:
    karma start --single-run --client-arg=--grep --client-arg=unit

Other suggestions?

Most helpful comment

@gaelazzo @timbertson A bit late to the party, but since this there's a much simpler solution now:

karma start --grep integration

karma.conf.js:

module.exports = function (config) {
  config.set({
    ...,
    client: {
      args: ['--grep', config.grep]
    }
  })
}

All 16 comments

Put it to the config file, exactly as you do with any other config variable.

module.exports = function(config) {
  config.set({
    client: {
      args: ['--foo', 'bar'],
      // other client-side config
      captureConsole: false
    }
  });
};

That works if you only ever want to pass one set of arguments, but my example was specifically using the same config but passing different arguments for different runs. e.g one run uses --grep unit and the other uses --grep integration, which is a common separation for CI tasks.

In which case you'd have to maintain one config file for each set of arguments you want to use. Surely that doesn't count as a sufficiently usable workaround, so can we please reopen this?

Having a side channel that's not explicitly on the command line would be acceptable, personally. e.g:

env KARMA_CLIENT_ARGS='["--grep","integration"]' karma serve --single-run

Yep, you can use env variables:

// karma.conf.js
module.exports = function(config) {
  config.set({
    client: {
      args: process.env.KARMA_CLIENT_ARGS.split(',')
    }
  });
};

Right, that would work. But actually asking users to always cut & paste that code into each config they write (or have it silently fail to recognise arguments) seems kinda user-hostile. It would be much harder to misconfigure if this were baked into karma-runner itself somewhere.

@vojtajina the start command is where it makes sense to pass clientArgs, at least for grunt-karma and grep. Otherwise you have to use grunt watch instead of karma's watch.

grunt karma:dev --grep=/admin/

Also without this you can't grep when doing a singleRun, since it doesn't ever call run.

@vojtajina sadly my use case lost to @gfxmonk's and grep does not work. In https://github.com/karma-runner/karma/pull/574 I originally made it pass the args through on start because karma's autoWatch mode only calls start. Using grunt-watch isn't an option on many large projects like mine because of the open file descriptor limit. Could you please consider passing the client args to start? I don't even need them parsed (I parse the cli args myself in grunt-karma), I only need them passed along to the mocha adapters.

@geddski grunt-karma can pass anything to the client, it can set the "client" config property, which gets passed to the browser...

@vojtajina hmm you're right, client does pass it on. Except in my project using the requirejs adapter, then config is undefined in karma-mocha adapter. Any idea what could cause that?

@vojtajina yep it was my use of the requirejs adapter, forgot to pass in the config on start. Thanks!

I proposed a change in order to allow start to accept clientArgs
https://github.com/karma-runner/karma/pull/957

@gaelazzo @timbertson A bit late to the party, but since this there's a much simpler solution now:

karma start --grep integration

karma.conf.js:

module.exports = function (config) {
  config.set({
    ...,
    client: {
      args: ['--grep', config.grep]
    }
  })
}

Many thanks.

@danielsiwiec, still getting Executed 1 of 12.

./node_modules/.bin/karma start --grep test/user.spec.ts

./node_modules/.bin/karma start --grep UserService

./node_modules/.bin/karma start -- --grep="UserService"

None of those commands above seems to make any diference.

"karma": "~2.0.0",
"karma-cli": "~1.0.1",

Is it really possible run a single test?

Was this page helpful?
0 / 5 - 0 ratings