Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass command line arguments for start command #672

Closed
timbertson opened this issue Aug 2, 2013 · 16 comments
Closed

feat: pass command line arguments for start command #672

timbertson opened this issue Aug 2, 2013 · 16 comments

Comments

@timbertson
Copy link
Contributor

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?

@vojtajina
Copy link
Contributor

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

@vojtajina
Copy link
Contributor

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

@timbertson
Copy link
Contributor Author

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?

@timbertson
Copy link
Contributor Author

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

@vojtajina
Copy link
Contributor

Yep, you can use env variables:

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

@timbertson
Copy link
Contributor Author

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.

@geddski
Copy link

geddski commented Aug 5, 2013

@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/

@geddski
Copy link

geddski commented Aug 6, 2013

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

@geddski
Copy link

geddski commented Aug 22, 2013

@vojtajina sadly my use case lost to @gfxmonk's and grep does not work. In #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.

@vojtajina
Copy link
Contributor

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

@geddski
Copy link

geddski commented Aug 26, 2013

@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?

@geddski
Copy link

geddski commented Aug 26, 2013

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

@gaelazzo
Copy link

I proposed a change in order to allow start to accept clientArgs
#957

@danielsiwiec
Copy link
Contributor

@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]
    }
  })
}

@gaelazzo
Copy link

gaelazzo commented Apr 2, 2016

Many thanks.

@rochapablo
Copy link

rochapablo commented Mar 7, 2018

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants