Karma: running one test file from the command line

So, I looked for everything for this, found "similar" answers here, but not quite what I want.

Right now, if I want to test one file with karma, I need to do fit() , fdescribe() in the corresponding file ...

However, I want to be able to simply name the karma, with the configuration file, and direct it to a specific file, so I do not need to modify the file at all, i.e.

karma run --conf karma.conf.js --file /path/to/specific/test_file.js

Can this be done? Or with any assistant? (using grunt or gulp?)

+52
javascript gruntjs gulp karma-runner karma-jasmine
Mar 19 '15 at 17:17
source share
3 answers

First you need to start the karma server with

 karma start 

Then you can use grep to filter a specific test or block description:

 karma run -- --grep=testDescriptionFilter 
+41
Mar 19 '15 at 17:30
source share

This option is no longer supported in recent versions of karma:

see https://github.com/karma-runner/karma/issues/1731#issuecomment-174227054

An array of files can be redefined using the CLI as such:

 karma start --files=Array("test/Spec/services/myServiceSpec.js") 

or shielded:

 karma start --files=Array\(\"test/Spec/services/myServiceSpec.js\"\) 

References

+7
Jul 23 '15 at 21:18
source share

Even if --files no longer supported, you can use the 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 the CLI:

 $ env KARMA_SPECS="spec1.js,spec2.js" karma start karma.conf.js --single-run 
+7
Dec 21 '16 at 9:07
source share



All Articles