Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

karma start - passing parameters

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?

like image 235
Wagner Danda da Silva Filho Avatar asked Aug 10 '16 14:08

Wagner Danda da Silva Filho


People also ask

How to generate karma config file?

The configuration file can be generated using karma init : $ karma init my. conf. js Which testing framework do you want to use?

How do I cancel my karma server?

The server can be stopped using the karma stop command.

What is karma Conf js file in angular?

Karma is the foundation of our testing workflow. It brings together our other testing tools to define the framework we want to use, the environment to test under, the specific actions we want to perform, etc. In order to do this Karma relies on a configuration file named by default karma. conf. js.


1 Answers

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.

like image 197
Louis Avatar answered Oct 05 '22 10:10

Louis