Gonçalo Vieira
Gonçalo Vieira

Reputation: 2249

Karma: Running a single test file from command line

So, I've been looking all over for this, found "similar" answers here, but not exactly what I want.

Right now if I want to test a single file with karma, I need to do fit(), fdescribe() on the file in question...

However, what I do want is to be able to just call karma, with the config file, and direct it to a specific file, so I don't need to modify the file at all, ie:

karma run --conf karma.conf.js --file /path/to/specific/test_file.js

is it possible to do this? Or with any helper? (using grunt or gulp?)

Upvotes: 109

Views: 135075

Answers (4)

flyingfishcattle
flyingfishcattle

Reputation: 2153

I tried @Yuriy Kharchenko's solution but ran into a Expected string or object with "pattern" property error.

Therefore I made the following modifications to his answer and now I'm able to run single files using Karma:

function getSpecs(specList) {
  if (specList) {
    return specList.toString();
  } else {
    return ['**/*_spec.js'] // whatever your default glob is
  }
}


module.exports = function(config) {
  config.set({
    //...
    files: [
        { pattern: getSpecs(process.env.KARMA_SPECS), type: "module"}
    ]
  });
});

Note: This solution only works with a single file mentioned in the KARMA_SPECS env variable. Ex: export KARMA_SPECS="src/plugins/muc-views/tests/spec1.js"

Upvotes: 0

Yuriy Kharchenko
Yuriy Kharchenko

Reputation: 1073

Even though --files is no longer supported, you can use an env variable to provide a list of files:

// karma.conf.js
function getSpecs(specList) {
  if (specList) {
    return specList.split(',')
  } else {
    return ['**/*_spec.js'] // whatever your default glob is
  }
}

module.exports = function(config) {
  config.set({
    //...
    files: ['app.js'].concat(getSpecs(process.env.KARMA_SPECS))
  });
});

Then in CLI:

$ env KARMA_SPECS="spec1.js,spec2.js" karma start karma.conf.js --single-run

Upvotes: 21

Paul Sweatte
Paul Sweatte

Reputation: 24617

This option is no longer supported in recent versions of karma:

see https://github.com/karma-runner/karma/issues/1731#issuecomment-174227054

The files array can be redefined using the CLI as such:

karma start --files=Array("test/Spec/services/myServiceSpec.js")

or escaped:

karma start --files=Array\(\"test/Spec/services/myServiceSpec.js\"\)

References

Upvotes: 9

bvaughn
bvaughn

Reputation: 13487

First you need to start karma server with

karma start

Then, you can use grep to filter a specific test or describe block:

karma run -- --grep=testDescriptionFilter

Upvotes: 68

Related Questions