Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gulp-jsx-coverage

zordius2720.4.0

Enable istanbul coverage on ES2015/babel or coffee-script/cjsx files when you do mocha/jasmine tests, also deal with sourceMap for coverage report and stack trace.

flux, react, babel, 6to5, es6, es2015, jsx, istanbul, coverage, mocha, jasmine, sourceMap, stack trace, gulp, CoffeeScript

readme

gulp-jsx-coverage

Enable istanbul coverage on ES2015/babel files when you do mocha/jasmine tests, also deal with sourceMap for stack trace....as gulp task.

npm version npm download Dependency Status Build Status License

Features

  • Help you create a gulp task to handle mocha testing + istanbul coverage
  • Transpile .jsx and ES2015 .js files on the fly
  • Use babel to transpile .js and .jsx files so you can use ES2015 features inside your .js and .jsx files!
  • Customize everything by options
  • sourceMaps on stack traces when mocha test failed (powered by source-map-support)
  • coverage threshold

ORIGINAL CODE/LINE in coverage reports

ORIGINAL CODE/LINE in stack traces

Usage

  1. Install

For Mocha tests:

npm install gulp gulp-jsx-coverage gulp-mocha babel-plugin-istanbul --save-dev

For Jasmine tests:

npm install gulp gulp-jsx-coverage gulp-jasmine babel-plugin-istanbul --save-dev
  1. Configure Babel

Configure your .babelrc and install proper presets or plugins.

Here is a .babelrc example:

{
  presets: ['es2015', 'react']
}

And then:

npm install babel-preset-es2015 babel-preset-react
  1. Create Gulp Task

Put this into your gulpfile.js:

gulp.task('your_task_name', require('gulp-jsx-coverage').createTask({
    src: ['test/**/*.js', 'test/components/*.jsx'],  // your test files
}));

Then run the task: gulp your_task_name

Best Practices

  • The golden rule: Use .jsx as ext name when jsx syntax inside it. Require it by require('file.jsx').
  • When you develop a module, do not use any module loader hooks. (Refer to Babel require hook document)
  • Excludes babel as possible as you can to improve babel performance.
  • When you develop an application, you may use module loader hooks. But, don't enable the hook when you do testing.

Usage: General Mocha Test Creator

gulp.task('your_task_name', require('gulp-jsx-coverage').createTask({
    src: ['test/**/*.js', 'test/components/*.jsx'],  // will pass to gulp.src as mocha tests

    istanbul: {                                      // will pass to istanbul, this is default setting
        exclude: /node_modules|test[0-9]/            // do not instrument these files
    },

    threshold: [                                     // fail the task when coverage lower than one of this array
        {
            type: 'lines',                           // one of 'lines', 'statements', 'functions', 'banches'
            min: 90
        }
    ],

    babel: {                                         // this is default setting
        include: /\.jsx?$/,
        exclude: /node_modules/,
        omitExt: false                               // if you wanna omit file ext when require(), put an array
    },                                               // of file exts here. Ex: ['.jsx', '.es6'] (NOT RECOMMENDED)

    coverage: {
        reporters: ['text-summary', 'json', 'lcov'], // list of istanbul reporters
        directory: 'coverage'                        // will pass to istanbul reporters
    },

    mocha: {                                         // will pass to mocha
        reporter: 'spec'
    },

    //optional
    cleanup: function () {
        // do extra tasks after test done
        // EX: clean global.window when test with jsdom
    }
}));

Usage: Other Testing Frameworks

var GJC = require('gulp-jsx-coverage');
var jasmine = require('gulp-jasmine');

gulp.task('my_jasmine_tests', function () {
    GJC.initModuleLoader(GJCoptions);                     // Refer to previous gulp-jsx-coverage options

    return gulp.src('test/*.js')
    .pipe(jasmine(jasmineOptions))
    .on('end', GJC.collectIstanbulCoverage(GJCoptions));  // Refer to previous gulp-jsx-coverage options
});

Live Example: mocha

git clone https://github.com/zordius/gulp-jsx-coverage.git
cd gulp-jsx-coverage
npm install
npm run mocha

OUTPUT:

 ~/gulp-jsx-coverage master>npm run mocha

> gulp-jsx-coverage@0.3.8 mocha /Users/zordius/gulp-jsx-coverage
> gulp mocha_tests

[11:50:14] Using gulpfile ~/gulp-jsx-coverage/gulpfile.js
[11:50:14] Starting 'mocha_tests'...


  target (tested by test1.js)
    ✓ should multiply correctly
    - should not show coverage info for test1.js
    ✓ should handle es2015 template string correctly

  target (tested by test2.jsx)
    ✓ should multiply correctly (77ms)
    - should not show coverage info for test2.jsx
    1) should exception and failed

  Component.jsx (tested by test2.jsx)
    ✓ should render Hello World


  4 passing (104ms)
  2 pending
  1 failing

  1) target (tested by test2.jsx) should exception and failed:
     TypeError: "hohoho
 this is
 multi line
 error!".notAFunction is not a function
      at Context.<anonymous> (test2.jsx:34:10)


[11:50:16] 'mocha_tests' errored after 1.86 s
[11:50:16] Error in plugin 'gulp-mocha'
Message:
    1 test failed.
---------------|----------|----------|----------|----------|----------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------|----------|----------|----------|----------|----------------|
All files      |    90.48 |      100 |       75 |    90.48 |                |
 Component.jsx |       75 |      100 |       50 |       75 |              5 |
 target.js     |       80 |      100 |    66.67 |       80 |              8 |
 testlib.js    |      100 |      100 |      100 |      100 |                |
---------------|----------|----------|----------|----------|----------------|

Upgrade Notice

0.4.0

  • Core changed:
    • do not support isparta now
    • do not support coffee-script/cjsx now
    • do not support options.babel now (please use .babelrc)
    • do not support options.istanbul.coverageVariable now
    • move to istanbul.js and babel-plugin-istanbul now

0.3.2

  • API changed:
    • you should rename all colloectIstanbulCoverage into collectIstanbulCoverage

0.3.0

  • Babel upgraded:

  • API changed:

    • you should rename all initIstanbulHookHack into initModuleLoaderHack

0.2.0

  • the sourceMap stack trace feature requires:
    • mocha >= 2.2.2
    • the options.babel.sourceMap should be changed from 'inline' to 'both'

changelog

HISTORY

2017-01-29 0.4.0 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.4.0

  • BREAK CHANGE do not support coffee-script
  • BREAK CHANGE do not support cjsx
  • BREAK CHANGE do not support isparta nor options.isparta
  • BREAK CHANGE do not support options.istanbul.coverageVariable
  • BREAK CHANGE rename options.transpile.babel to options.babel
  • BREAK CHANGE rename .initModuleLoaderHack() to initModuleLoader()
  • move to istanbul.js and babel-plugin-istanbul, coverage report with original codes!
  • move to source-map-support, better stack traces with original codes and lines!
  • only support node4+
  • better error handling for options.coverage

2016-02-01 0.3.8 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.8

  • support multiple coverage threshold types

2016-01-12 0.3.7 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.7

  • fix .cjsx omitExt option bug

2016-01-04 0.3.6 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.6

  • support .cjsx files

2015-12-04 0.3.5 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.5

  • change example to recommend people move babel config into .babelrc
  • use source-map for better line/column hint in coverage report
  • better line/column display for mocha error stack traces

2015-12-03 0.3.4 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.4

  • fix v0.3.3 isparta coverage report issue

2015-12-02 0.3.3 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.3

  • fix wrong line number when printing mocha stacktrace

2015-12-01 0.3.2 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.2

  • update dependency
  • fix bad coverage report issue when using istanbul 0.3.8+
  • rename API: .colloectIstanbulCoverage() into .collectIstanbulCoverage()

2015-11-07 0.3.1 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.1

  • update dependency

2015-10-20 0.3.0 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.3.0

  • BREAK CHANGE rename initIstanbulHookHack to initModuleLoaderHack
  • new option: options.isparta to support isparta

2015-09-02 0.2.5 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.5

  • update dependency

2015-08-17 0.2.4 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.4

  • prevent error when empty files are included

2015-08-06 0.2.3 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.3

  • new option: options.transpile.babel.omitExt
  • new option: options.transpile.coffee.omitExt

2015-07-25 0.2.2 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.2

  • unlock strict dependency

2015-07-23 0.2.1 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.1

  • support coverage threshold
  • new option: options.threshold
  • new option: options.thresholdType
  • new .failWithThreshold() API

2015-07-23 0.2.0 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.2.0

  • sourceMaps on stack traces when mocha test failed
  • prevent error when mocha is not installed
  • prevent error when babel sourceMap is not enabled
  • prevent error when coffee sourceMap is not enabled
  • new .enableStackTrace() and .disableStackTrace() API

2015-05-18 0.1.3 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.1.3

  • new .initIstanbulHook() and .colloectIstanbulCoverage() API
  • support any test frameworks
  • add jasmine tests example

2015-05-12 0.1.2 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.1.2

  • update dependency (babel)

2015-05-07 0.1.1 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.1.1

  • update dependency (babel, gulp, istanbul)
  • update devDependency (mocha, coffee-script, jsdom)

2015-03-06 0.1.0 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.1.0

2015-01-06 0.0.5 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.0.5

  • Support cleanup callback

2014-12-31 0.0.4 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.0.4

  • Better comment intend logic

2014-12-31 0.0.3 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.0.3

  • Supports sourceMap distribution to comments in istanbul reports

2014-12-31 0.0.2 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.0.2

  • Supports .coffee testing

2014-12-30 0.0.1 https://github.com/zordius/gulp-jsx-coverage/releases/tag/v0.0.1

  • Initial Release