Initial Karma Parameters - Walkthrough

Is there a way to pass a parameter through the Karma command line and then read what is somewhere in your tests? For example, this is what you need:

karma start -branding="clientX"

And then somewhere in my specifications I will need to access this variable (I need the value "clientX").

Is this even possible?

+4
source share
2 answers

You can pass parameters for verification. It can be a little tricky. What you can do is check on __karma__.config.argsin your test package:

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

karma run

If you want to pass arguments using karma run, then all you need.

, karma start, karma run -- --foo, :

LOG: ['--foo']

, , karma run, __karma__.config.args. , karma run -- --foo , Karma " ". (karma start .)

karma start

karma start -.

karma.conf.js, karma init, , karma start --single-run --foo. karma.conf.js, :

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

karma start --single-run --foo, , run .

, process.argv, , Karma, args .

, , karma start --single-run --something, config.something karma.conf.js.

Karama 1.1.x Karma 1.2.0. , , client.args. karma start, karma run. client.args ( branding). 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);
});

karma start --single-run --foo --branding=q, :

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

, karma run -- --foo --branding=q, :

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

, karma run config.args, .

+17

, . , , client karma.conf.js:

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        plugins: [
            ...
        ],
        client: { //Put the parameters here
            codeCoverage: config.cc,
            testSuite: config.testSuite
        },
...

:

karma start --cc --testSuite=sanity

, (, --cc), true.

:

console.log('Coverage: ', __karma__.config.codeCoverage);
console.log('Test suite: ', __karma__.config.testSuite);
0

Source: https://habr.com/ru/post/1583898/


All Articles