Skip to content

Commit

Permalink
feat(task): let Grunt do the file matching
Browse files Browse the repository at this point in the history
Use the task's `files` property, but merge with `options.files`,
so we can specify some files that should be available in all test-targets.
Grunt's file objects may be extended with the file options supported
by Karma (included, watched).
Projects that use the undocumented `karma.<target>.files` option should
rename this option and use `karma.<target>.src` instead.
  • Loading branch information
jpommerening authored and dignifiedquire committed Jan 9, 2015
1 parent f900008 commit cb53dea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ karma: {
To change the `logLevel` in the grunt config file instead of the karma config, use one of the following strings:
`OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`

The `files` option can be extended "per-target" in the typical way
Grunt handles [files][grunt-config-files]:

```js
karma: {
options: {
files: ['lib/**/*.js']
},
unit: {
files: [
{ src: ['test/**/*.js'] }
]
}
}
```

When using the "Grunt way" of specifying files, you can also extend the
file objects with the options [supported by karma][karma-config-files]:

```js
karma: {
unit: {
files: [
{ src: ['test/**/*.js'], served: true },
{ src: ['lib/**/*.js'], served: true, included: false }
]
}
}
```

### Config with Grunt Template Strings in `files`

When using template strings in the `files` option, the results will flattened. Therefore, if you include a variable that includes an array, the array will be flattened before being passed to Karma.
Expand Down Expand Up @@ -200,7 +230,9 @@ $ grunt karma:dev watch --grep=mypattern
## License
MIT License

[karma-config-file]: http://karma-runner.github.com/0.8/config/configuration-file.html
[karma-config-file]: http://karma-runner.github.com/0.12/config/configuration-file.html
[karma-config-files]: http://karma-runner.github.io/0.12/config/files.html
[grunt-config-files]: http://gruntjs.com/configuring-tasks#files
[grunt-contrib-watch]: https://github.com/gruntjs/grunt-contrib-watch
[PhantomJS]: http://phantomjs.org/
[karma-mocha]: https://github.com/karma-runner/karma-mocha
24 changes: 17 additions & 7 deletions tasks/grunt-karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ module.exports = function(grunt) {
grunt.registerMultiTask('karma', 'run karma.', function() {
var done = this.async();
var options = this.options({
background: false
background: false,
files: [],
client: {}
});

if (!options.client) {
options.client = {};
}
// Allow for passing cli arguments to `client.args` using `--grep=x`
var args = parseArgs(process.argv.slice(2));
if (_.isArray(options.client.args)) {
Expand Down Expand Up @@ -69,9 +68,20 @@ module.exports = function(grunt) {
data.configFile = path.resolve(data.configFile);
}

if (data.files){
data.files = _.flatten(data.files);
}
data.files = [].concat.apply(options.files, this.files.map(function(file) {
return file.src.map(function(src) {
var obj = {
pattern: src
};

['watched', 'served', 'included'].forEach(function(opt) {
if (opt in file) {
obj[opt] = file[opt];
}
});
return obj;
});
}));

// Allow the use of templates in preprocessors
if (_.isPlainObject(data.preprocessors)) {
Expand Down

1 comment on commit cb53dea

@MaxiSir
Copy link

@MaxiSir MaxiSir commented on cb53dea Jan 9, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This break the posibility to let the files be defined in the karma config, and forces it to be defined only in gruntfile.

Please sign in to comment.