Karma: Running a single test file from command line
Asked Answered
W

4

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

Windsucking answered 19/3, 2015 at 17:17 Comment(7)
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.) :)Hoenir
isn't that just for a specific it ? No possibility of using it for a file?Intradermal
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?Hoenir
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.Hoenir
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 :)Intradermal
Hot dog. Glad it helped. Posted. :)Hoenir
Related question: https://mcmap.net/q/118698/-karma-run-single-test/…Clubwoman
H
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
Hoenir answered 19/3, 2015 at 17:30 Comment(7)
This answer should say that you should have karma running with karma start before running karma runSpirituality
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.Hoenir
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.Hilariohilarious
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).Sedation
this does not run a single test!Erlineerlinna
This answer is 2 years old! ;) Things change.Hoenir
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...Sinecure
C
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
Castorina answered 21/12, 2016 at 9:7 Comment(1)
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 !Sedation
F
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

Fluxmeter answered 23/7, 2015 at 21:18 Comment(6)
gives me this error - bash: syntax error near unexpected token `('Jaysonjaywalk
@Lakshay Use the aforementioned escaped syntax for bash Array\("foo.js"\).Fluxmeter
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?Intradermal
erm, not because of requireJS, for some odd reason karma isn't reading Array("zzz") as being a JS Array? O_oIntradermal
nm, it seems that karma no longer supports a --files arg?Intradermal
Warning for everyone coming here this option is no longer supported in karma: github.com/karma-runner/karma/issues/…Durtschi
K
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"

Kava answered 10/5, 2022 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.