109

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

7
  • 1
    Tried karma run -- --grep=testDescriptionFilter ? I've seen this suggested before, but I can't verify first hand that it works. (If I were home, I'd try before suggesting. Since I can't try- I'm commenting instead of answering.) :)
    – bvaughn
    Mar 19, 2015 at 17:22
  • 1
    isn't that just for a specific it ? No possibility of using it for a file? Mar 19, 2015 at 17:23
  • 4
    To be honest, I've always just modified karma.conf if I want to focus on a single test file. It's easy enough to do that I've never spent the effort to find a faster way.
    – bvaughn
    Mar 19, 2015 at 17:28
  • 1
    Hot dog. Glad it helped. Posted. :)
    – bvaughn
    Mar 19, 2015 at 17:30
  • 1
    Related question: stackoverflow.com/questions/26552729/karma-run-single-test/…
    – Stefan
    Jul 27, 2017 at 12:47

4 Answers 4

68

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
7
  • 13
    This answer should say that you should have karma running with karma start before running karma run
    – etagwerker
    Jul 17, 2015 at 12:54
  • 4
    I don't really think that's necessary, given that the question asked about how to filter with karma run. Seemed pretty clear that the poster knew how to start Karma and run all tests and was just looking for the syntax regarding filtering.
    – bvaughn
    Jul 18, 2015 at 16:45
  • 4
    This solution appears not to work at all with Jasmine tests (karma-jasmine). And the karma run --help does not show --grep as command option at all. I'm using Karma latest (it says 1.5.0).
    – Yavin5
    Mar 3, 2017 at 12:59
  • 13
    this does not run a single test!
    – eav
    Apr 6, 2017 at 15:37
  • 3
    This worked for me with karma-mocha, but I had to comment out singleRun: true in karma.conf.js and run karma start --no-auto-watch so that it didn't try running the whole suite from karma start...
    – Nickolay
    May 6, 2018 at 21:18
20

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
1
  • 2
    This ends up working great! Just make sure that your default glob has a directory name on the front of it, or else you get a pattern error. Also, I changed my files: definition like this: files: [ ].concat(getSpecs(process.env.KARMA_SPECS)), And that ended up working better, because the function already puts in the glob pattern there if no environment variable is set. Oh, and I don't think you need the "env " on the front of the command (it works for me without that). Thanks for this solution @Yuriy Kharchenko !
    – Yavin5
    Mar 2, 2017 at 22:38
9

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

6
  • 2
    gives me this error - bash: syntax error near unexpected token `(' Aug 26, 2015 at 12:52
  • 5
    so, when I try this I get the following error: config.files = config.files.map(createPatternObject).map(createPatternMapper(basePathResolve)), which might be due to the fact that I'm using requireJS? Nov 23, 2015 at 14:41
  • 1
    erm, not because of requireJS, for some odd reason karma isn't reading Array("zzz") as being a JS Array? O_o Nov 23, 2015 at 14:56
  • 2
    nm, it seems that karma no longer supports a --files arg? Nov 23, 2015 at 14:58
  • 24
    Warning for everyone coming here this option is no longer supported in karma: github.com/karma-runner/karma/issues/… Jan 27, 2016 at 16:17
0

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.