Skip to content

Instantly share code, notes, and snippets.

@iwata-n
Created January 17, 2016 08:12
Show Gist options
  • Save iwata-n/059dd6a5e7926c2685c4 to your computer and use it in GitHub Desktop.
Save iwata-n/059dd6a5e7926c2685c4 to your computer and use it in GitHub Desktop.
node.jsでテストのカバレッジを取る ref: http://qiita.com/iwata-n@github/items/1e8f629eb5b429a49e6d
var gulp = require('gulp');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha');
gulp.task('pre-test', function () {
return gulp.src(['routes/*.js'])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire());
});
gulp.task('test', ['pre-test'], function () {
return gulp.src(['test/*.js'])
.pipe(mocha())
// Creating the reports after tests ran
.pipe(istanbul.writeReports())
// Enforce a coverage of at least 90%
.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
});
$ gulp test
[16:53:47] Using gulpfile ~/hoge/gulpfile.js
[16:53:47] Starting 'pre-test'...
[16:53:47] Finished 'pre-test' after 50 ms
[16:53:47] Starting 'test'...
GET /users
GET /users 200 7.133 ms - 8
✓ no param (40ms)
1 passing (45ms)
-----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-----------|----------|----------|----------|----------|----------------|
routes/ | 76.92 | 50 | 50 | 76.92 | |
index.js | 80 | 100 | 0 | 80 | 6 |
users.js | 75 | 50 | 100 | 75 | 7,8 |
-----------|----------|----------|----------|----------|----------------|
All files | 76.92 | 50 | 50 | 76.92 | |
-----------|----------|----------|----------|----------|----------------|
=============================== Coverage summary ===============================
Statements : 76.92% ( 10/13 )
Branches : 50% ( 1/2 )
Functions : 50% ( 1/2 )
Lines : 76.92% ( 10/13 )
================================================================================
[16:53:47] 'test' errored after 336 ms
[16:53:47] Error in plugin 'gulp-istanbul'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment