Skip to content

Commit

Permalink
docs(readme): add an advanced usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericbeaumet committed Nov 6, 2014
1 parent 14a8e36 commit c5d0b4c
Showing 1 changed file with 60 additions and 20 deletions.
80 changes: 60 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ npm install karma-coverage --save-dev
```

## Configuration
Following code shows the default configuration...
```js

The following code shows a simple usage:

```javascript
// karma.conf.js
module.exports = function(config) {
config.set({
Expand All @@ -37,7 +39,7 @@ module.exports = function(config) {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'src/*.js': ['coverage']
'src/**/*.js': ['coverage']
},

// optionally, configure the reporter
Expand All @@ -49,8 +51,9 @@ module.exports = function(config) {
};
```

Example use with a CoffeeScript project...
```js
Example use with a CoffeeScript project:

```javascript
// karma.conf.js
module.exports = function(config) {
config.set({
Expand All @@ -67,7 +70,7 @@ module.exports = function(config) {
// do not include tests or libraries
// (these files will be instrumented by Istanbul via Ibrik unless
// specified otherwise in coverageReporter.instrumenter)
'src/*.coffee': ['coverage'],
'src/**/*.coffee': ['coverage'],

// note: project files will already be converted to
// JavaScript via coverage preprocessor.
Expand All @@ -83,13 +86,48 @@ module.exports = function(config) {
}
});
};
```

Here is an advanced usage of karma-coverage, using severals reporters:

```javascript
// karma.conf.js
module.exports = function(config) {
config.set({
files: [
'src/**/*.js',
'test/**/*.js'
],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
// specify a common output directory
dir: 'build/reports/coverage',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
// reporters supporting the `file` property, use `subdir` to directly
// output them in the `dir` directory
{ type: 'cobertura', subdir: '.', file: 'cobertura.txt' },
{ type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
{ type: 'teamcity', subdir: '.', file: 'teamcity.txt' },
{ type: 'text', subdir: '.', file: 'text.txt' },
{ type: 'text-summary', subdir: '.', file: 'text-summary.txt' },
]
}
});
});
```

### Options
#### type
**Type:** String

**Description:** Specify a reporter type.

**Possible Values:**
* `html` (default)
* `lcov` (lcov and html)
Expand All @@ -99,16 +137,6 @@ module.exports = function(config) {
* `cobertura` (xml format supported by Jenkins)
* `teamcity` (code coverage System Messages for TeamCity)

If you set `type` to `text` or `text-summary`, you may set the `file` option, like this.
```javascript
coverageReporter: {
type : 'text',
dir : 'coverage/',
file : 'coverage.txt'
}
```
If no filename is given, it will write the output to the console.

#### dir
**Type:** String

Expand All @@ -121,8 +149,8 @@ If no filename is given, it will write the output to the console.
**Description**: This will be used in complement of the `coverageReporter.dir`
option to generate the full output directory path. By default, the output
directory is set to `./config.dir/BROWSER_NAME/`, this option allows you to
custom the second part. You can either pass a string or a function which will be
called with the `browser` passed as the only argument.
custom the second part. You can either pass a `string` or a `function` which will be
called with the browser name passed as the only argument.

```javascript
coverageReporter: {
Expand Down Expand Up @@ -152,6 +180,18 @@ coverageReporter: {
}
```

#### file

If you choose the `cobertura`, `lcovonly`, `teamcity`, `text` or `text-summary` reporters, you may set the `file` option to specify an output file.

```javascript
coverageReporter: {
type : 'text',
dir : 'coverage/',
file : 'coverage.txt'
}
```

#### multiple reporters
You can use multiple reporters, by providing array of options.

Expand All @@ -167,12 +207,12 @@ coverageReporter: {

#### instrumenter
Karma-coverage infers the instrumenter regarding of the file extension.
The .coffee files are by default covered using
The `.coffee` files are by default covered using
[Ibrik](https://github.com/Constellation/ibrik) (an
[Istanbul](https://github.com/gotwarlost/istanbul) analog for
CoffeeScript files). It is possible to override this behavior and point out an
instrumenter for the files matching a specific pattern.
To do so, you need to declare an object under with the keys representing the
To do so, you need to declare an object under with the keys represents the
pattern to match, and the instrumenter to apply. The matching will be done
using [minimatch](https://github.com/isaacs/minimatch).
If two patterns match, the last one will take the precedence.
Expand Down

0 comments on commit c5d0b4c

Please sign in to comment.