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

Gonçalo Vieira
  • 2,249
  • 2
  • 19
  • 39
  • 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 '15 at 17:22
  • 1
    isn't that just for a specific `it` ? No possibility of using it for a file? – Gonçalo Vieira Mar 19 '15 at 17:23
  • Great question. The place I saw it mentioned was in the context of a single `it` but... seeing as how describes can be nested, maybe it would work for an entire test? – bvaughn Mar 19 '15 at 17:24
  • 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 '15 at 17:28
  • Well, if you could post this as an answer, I'll accept it, because it seems to work, atm I have 1 describe block per file, so that's alright to use :) – Gonçalo Vieira Mar 19 '15 at 17:29
  • 1
    Hot dog. Glad it helped. Posted. :) – bvaughn Mar 19 '15 at 17:30
  • 1
    Related question: https://stackoverflow.com/questions/26552729/karma-run-single-test/45350941#45350941 – Stefan Jul 27 '17 at 12:47

4 Answers4

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
jakub.g
  • 38,512
  • 12
  • 92
  • 130
bvaughn
  • 13,300
  • 45
  • 46
  • 13
    This answer should say that you should have `karma` running with `karma start` before running `karma run` – etagwerker Jul 17 '15 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 '15 at 16:45
  • 2
    What could I do if I want this to happen programmatically? I.e. I wouldn't know the description of the file that changed, but I would know its filename. – MPV Jan 27 '16 at 16:23
  • 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 03 '17 at 12:59
  • 13
    this does not run a single test! – eav Apr 06 '17 at 15:37
  • 2
    This answer is 2 years old! ;) Things change. – bvaughn May 04 '17 at 21:56
  • 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 06 '18 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
Yuriy Kharchenko
  • 1,063
  • 10
  • 10
  • 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 02 '17 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

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
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"

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25