Is there a way to pass a parameter thru the Karma command line and then read that somewhere in your tests? For instance, this is what want:
karma start -branding="clientX"
And then somewhere in my specs I would need to access this variable (I need the "clientX" value).
Is this possible at all?
Yes it is possible. All you have to do is specify that parameter in
client
section ofkarma.conf.js
:To pass it:
Note that if you don't give a value to parameter (like
--cc
) it will be set totrue
.To access it from the tests:
It is possible to transmit parameters to test cases. It can be a bit tricky. What you can to do is check for
__karma__.config.args
in you test suite:karma run
If you want to pass arguments with
karma run
, then the above is all you need.Then if you do
karma start
and thenkarma run -- --foo
you should see on the console:Note how the argument passed to
karma run
ended up in__karma__.config.args
. Also note that the first double-dash inkarma run -- --foo
is there to separate Karma arguments from "client arguments" it is necessary. (karma start
does not make the same distinction.)karma start
karma start
works differently.If you use a default
karma.conf.js
created bykarma init
, you won't be able to pass arguments in this way by doingkarma start --single-run --foo
. You need to modify yourkarma.conf.js
to pass the arguments:If you run
karma start --single-run --foo
, then you'll get the same input as withrun
earlier.If I had to pass multiple arguments, I'd scan
process.argv
to filter out those parts of it that are only for Karma's benefit and pass the rest toargs
instead of testing for each possibility.You may have inferred from the above that when you
karma start --single-run --something
the argument ends up asconfig.something
inkarma.conf.js
.Complete example
This example was tested against Karama 1.1.x and Karma 1.2.0. It shows the same method I've discussed above to get command line parameters to transit through
client.args
. This works both withkarma start
andkarma run
. I also added a method to pass values without going throughclient.args
(that's thebranding
example). However, this method does not work withkarma run
.karma.conf.js
:test/test.js
:If you run
karma start --single-run --foo --branding=q
, you get:If you start Karma and then use
karma run -- --foo --branding=q
, you get:As mentioned above, when using
karma run
, everything must go throughconfig.args
to be visible in the test.