• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript codecoverage tool for Grunt

grunt-istanbul Build Status

JavaScript codecoverage tool for Grunt

Getting Started

This plugin requires Grunt ~0.4.1

Install this grunt plugin next to your project's Gruntfile.js with: npm install grunt-istanbul

Then add this line to your project's Gruntfile.js gruntfile:

grunt.loadNpmTasks('grunt-istanbul');

Documentation

This grunt plugin uses Istanbul to perform the code coverage tasks. It provides the following grunt tasks:

  • instrument: instruments a file or a directory tree
  • reloadTasks: override instrumented tasks
  • storeCoverage: store coverage from global
  • makeReport: make coverage report

To use this grunt-istanbul plugin, register a grunt task to run the following:

  1. Instrument your source code
  2. Run your test suite against your instrumented source code
  3. Store your coverage results
  4. Make the report

For step 2, an environment variable can be used to determine which path to use for loading the source code to run the tests against. For example, when you normally run your tests you want them to point directly at your source code. But when you run your instanbul code coverage task you want your tests to point at your instrumented source code. The grunt-env plugin can be used for setting an environment variable in a grunt task. Here's an example solution that solves this problem using grunt-env and grunt-mocha-test:

// in Gruntfile.js
module.exports = function (grunt) {

  grunt.initConfig({
    env: {
      coverage: {
        APP_DIR_FOR_CODE_COVERAGE: '../test/coverage/instrument/app/'
      }
    },
    instrument: {
      files: 'app/*.js',
      options: {
        lazy: true,
        basePath: 'test/coverage/instrument/'
      }
    },
    mochaTest: {
      options: {
        reporter: 'spec'
      },
      src: ['test/*.js']
    },
    storeCoverage: {
      options: {
        dir: 'test/coverage/reports'
      }
    },
    makeReport: {
      src: 'test/coverage/reports/**/*.json',
      options: {
        type: 'lcov',
        dir: 'test/coverage/reports',
        print: 'detail'
      }
    }
  });

  grunt.registerTask('coverage', ['env:coverage', 'instrument', 'mochaTest',
    'storeCoverage', 'makeReport']);
};
// require_helper.js
module.exports = function (path) {
  return require((process.env.APP_DIR_FOR_CODE_COVERAGE || '../app/') + path);
};
// using requireHelper in a test
var requireHelper = require('../require_helper');
var formValidator = requireHelper('form_validator');

You can also pass an instrumenter argument to the instrument options as well as any other arguments that your instrumenter takes.

// in Gruntfile.js
module.exports = function (grunt) {
 var isparta = require('isparta');
  grunt.initConfig({
    instrument: {
      files: 'app/*.es6',
      options: {
        lazy: true,
        basePath: 'test/coverage/instrument/'
        babel: {ignore: false, experimental: true, extensions: ['.es6']},
        instrumenter: isparta.Instrumenter
      }
    }
  });
};

If you want to specify a current working directory, you can specify a path the cwd options :

// in Gruntfile.js
module.exports = function (grunt) {
 var isparta = require('isparta');
  grunt.initConfig({
    instrument: {
      files: '**/*.es6',
      options: {
        cwd: 'app/'
        lazy: true,
        basePath: 'test/coverage/instrument/'
        babel: {ignore: false, experimental: true, extensions: ['.es6']}
      }
    }
  });
};

If you wan to insturment multiple locations, you specify multiple task targets:

// in Gruntfile.js
module.exports = function (grunt) {

  grunt.initConfig({
    env: {
      coverage: {
        APP_DIR_FOR_CODE_COVERAGE: '../test/coverage/instrument/app/'
      }
    },
    instrument: {
      api: {
        files: 'app/*.js',
        options: {
          basePath: 'test/coverage/instrument/'
        }
      },
      web: {
        files: 'web/*.js',
        options: {
          basePath: 'test/coverage/instrument/'
        }
      }
    },
    mochaTest: {
      options: {
        reporter: 'spec'
      },
      src: ['test/*.js']
    },
    storeCoverage: {
      options: {
        dir: 'test/coverage/reports'
      }
    },
    makeReport: {
      src: 'test/coverage/reports/**/*.json',
      options: {
        type: 'lcov',
        dir: 'test/coverage/reports',
        print: 'detail'
      }
    }
  });

  grunt.registerTask('coverage', function(arg) {
    var insturment = arg ? 'insturment:' + arg : 'insturment';
    
    grunt.task.run(['env:coverage', instrument, 'mochaTest',
      'storeCoverage', 'makeReport']);
  });
# coverage with api and web insturmentation
$ grunt coverage
# coverage with api insturmentation
$ grunt coverage:api
# coverage with web insturmentation
$ grunt coverage:web

By default only files that have coverage are stored. When the 'include-all-sources' option is set to true it will show all instrumented files even if their coverage percentage is 0. To see all instrumented files you can init the storeCoverage task as follows:

grunt.initConfig({
  storeCoverage: {
    options: {
      dir: 'test/coverage/reports',
      'include-all-sources': true
    }
  }
});

Also, checkout the example Gruntfile.js in this repo (note that you do not need to implement the reloadTasks task in this example): Gruntfile.js

For information about the configuration options use the command line istanbul help config.

more examples

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

(Nothing yet)

License

Copyright (c) 2014 taichi Licensed under the MIT license.

More Repositories

1

ci-yarn-upgrade

Keep NPM dependencies up-to-date with CI, providing version-to-version diff for each library
JavaScript
85
star
2

siden

tiny web application framework for Java SE 8 on top of undertow.
Java
59
star
3

umlbot

uml generator bot for slack
Java
58
star
4

js-boilerplate

Modern JavaScript Application Boilerplate
JavaScript
56
star
5

vscode-textlint

TypeScript
48
star
6

kra

relational database access helper library
Go
33
star
7

gontributions

GitHub Contribution Graph Hack
Go
20
star
8

koshinuke

Application Lifecycle Management with Git.
JavaScript
17
star
9

react-beautify

TypeScript
13
star
10

approved-event-action

TypeScript
12
star
11

brackets-jsbeautifier

JS Beautifier for Brackets
JavaScript
12
star
12

rx-sqlite

A wrapper library that adds RxJS 5 API to sqlite3
JavaScript
11
star
13

yuzen

static contents generator
Groovy
11
star
14

gist.eclipse

gist eclipse integration
Java
9
star
15

aptcombo

Doma2 + lombok + Dagger2 + HikariCP + JUnit4 + gradle + eclipse + siden
Java
9
star
16

org.handwerkszeug.riak

Alternative riak java client
Java
6
star
17

invite_to_gradle

Shell
6
star
18

json-benchmarks

Benchmark Test for json libraries
Java
6
star
19

modernjs

5
star
20

joa

analyze jvm options from openJDK
Java
5
star
21

sirusi

create indenticons similar to GitHub and Gravatar (retro) avatars.
Java
5
star
22

learningRiak

learning Riak and Erlang for taichi by taichi.
5
star
23

rappa

This extension supports code reading method used for Ninja.
JavaScript
5
star
24

plugin-system

simple plugin system built on Guice
Java
4
star
25

ts-template

TypeScript
4
star
26

actions-cfn-diff

GitHub Actions to summarize diffs of CloudFormation stack
TypeScript
4
star
27

gige

Cross Compiler testing Framework
Java
2
star
28

chocolatey-peco

chocolatey packaging scripts for peco
PowerShell
2
star
29

lingrNotifier

Lingr Notifier Extension for Chrome
JavaScript
2
star
30

werkzeugkasten

Automatically exported from code.google.com/p/werkzeugkasten
Java
2
star
31

init_scripts

custom init task for gradle
2
star
32

jetty8-websocket-example

Groovy
2
star
33

liteymsr

1
star
34

OpenPowerShell

1
star
35

test

Groovy
1
star
36

taichi.github.com

1
star
37

retry-handler

m9 yoshiori
Java
1
star
38

gotive

Gist clone written in golang
1
star
39

repository

maven2 repository
1
star
40

kakasi

TypeScript
1
star
41

testYuzen

1
star
42

org.handwerkszeug.mvnhack

Download libraries from maven2 repository without maven2.
Java
1
star
43

jmh-gradle-ide

JMH & Gradle & IDE(Eclipse or IDEA) project template
Java
1
star