Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 232
Gonçalo Vieira Avatar asked Mar 19 '15 17:03

Gonçalo Vieira


People also ask

How do I create a karma config file?

In order to serve you well, Karma needs to know about your project in order to test it and this is done via a configuration file. The easiest way to generate an initial configuration file is by using the karma init command. This page lists all of the available configuration options.

What does it mean to set the singleRun attribute to true located in the Karma Conf js file?

Setting singleRun: false assumes that you are explicitly start the karma-client manually. This means that you start karma (technically the karma-server), then go to another terminal and type karma run . Setting singleRun: true in your karma configuration will call karma run for you.


3 Answers

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
like image 164
bvaughn Avatar answered Oct 19 '22 11:10

bvaughn


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
like image 20
Yuriy Kharchenko Avatar answered Oct 19 '22 12:10

Yuriy Kharchenko


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

  • karma-runner source: cli.js

  • karma-runner source: config.js

like image 8
Paul Sweatte Avatar answered Oct 19 '22 12:10

Paul Sweatte