Karma: Running a single test file from command line
Matthew Barrera
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?)
74 Answers
First you need to start karma server with
karma startThen, you can use grep to filter a specific test or describe block:
karma run -- --grep=testDescriptionFilter 7 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 This option is no longer supported in recent versions of karma:
see
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
6I 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"