karma start - passing parameters

10,638

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:

it("get karma args", function () {
    console.log(__karma__.config.args);
});

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 then karma run -- --foo you should see on the console:

LOG: ['--foo']

Note how the argument passed to karma run ended up in __karma__.config.args. Also note that the first double-dash in karma 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 by karma init, you won't be able to pass arguments in this way by doing karma start --single-run --foo. You need to modify your karma.conf.js to pass the arguments:

module.exports = function(config) {
  config.set({
    client: {
        args: config.foo ? ["--foo"] : [],
    },

If you run karma start --single-run --foo, then you'll get the same input as with run 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 to args 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 as config.something in karma.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 with karma start and karma run. I also added a method to pass values without going through client.args (that's the branding example). However, this method does not work with karma run.

karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: '',
    client: {
        // Example passing through `args`.
        args: config.foo ? ["--foo"] : [],

        // It is also possible to just pass stuff like this,
        // but this works only with `karma start`, not `karma run`.
        branding: config.branding,
    },
    frameworks: ['jasmine'],
    files: [
      'test/**/*.js'
    ],
    exclude: [],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

test/test.js:

it("get karma arg", function () {
    console.log("BRANDING", __karma__.config.branding);
    console.log("ARGS", __karma__.config.args);
});

If you run karma start --single-run --foo --branding=q, you get:

LOG: 'BRANDING', 'q'
LOG: 'ARGS', ['--foo']

If you start Karma and then use karma run -- --foo --branding=q, you get:

LOG: 'BRANDING', undefined
LOG: 'ARGS', ['--foo', '--branding=q']

As mentioned above, when using karma run, everything must go through config.args to be visible in the test.

Share:
10,638
Wagner Danda da Silva Filho
Author by

Wagner Danda da Silva Filho

● 20 years of hands-on experience with developing and delivering user-friendly, javascript-enabled web-based solutions ● 7 years developing cutting edge, responsive UIs with AngularJs and Bootstrap ● 10+ years leading and motivating small development teams

Updated on June 15, 2022

Comments

  • Wagner Danda da Silva Filho
    Wagner Danda da Silva Filho almost 2 years

    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?