Karma: Running a single test file from command line

JavascriptGruntjsGulpKarma RunnerKarma Jasmine

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGonçalo VieiraView Question on Stackoverflow
Solution 1 - JavascriptbvaughnView Answer on Stackoverflow
Solution 2 - JavascriptYuriy KharchenkoView Answer on Stackoverflow
Solution 3 - JavascriptPaul SweatteView Answer on Stackoverflow
Solution 4 - JavascriptflyingfishcattleView Answer on Stackoverflow