SBoudrias / gulp-istanbul

Istanbul unit test coverage plugin for gulp.
MIT License
186 stars 87 forks source link

Babel, sourcemaps, and separate commands: nothing covered #106

Closed DaAwesomeP closed 8 years ago

DaAwesomeP commented 8 years ago

I can't get istanbul to check anything. I'm using Babel and souremaps. Is this a result from running the pretest and test parts separately?

gulp.task('static', () => {
  return gulp.src('lib/*.js')
    .pipe(excludeGitignore())
    .pipe(lec()) // gulp-line-ending-corrector
    .pipe(gulp.dest('.'))
})

gulp.task('clean', (callback) => {
  del(['dist/*']).then(() => {
    callback()
  })
})

gulp.task('build', gulp.series('static', 'clean', () => {
  return gulp.src('lib/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(babel())
    .pipe(istanbul())
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('dist'))
}))

gulp.task('test', () => {
  return gulp.src(['test/test-*.js'], { read: false })
    .pipe(mocha({
      reporter: 'spec',
      globals: {
        should: require('should')
      }
    }))
    .pipe(istanbul.writeReports())
})

In my CI tests, I run gulp build in one stage, and then gulp test later on. Here's what I get:

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

=============================== Coverage summary ===============================
Statements   : 100% ( 0/0 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 0/0 )
================================================================================
SBoudrias commented 8 years ago

Does your tests read from the dist files? Because as you're not hooking the requires, then it seems to me you're just loading uninstrumented files.

For es6 support, you should look at either updating your Node version or specifying an instrumenter supporting es6

I'll close this issue as it's not a bug with the gulp-istanbul.

DaAwesomeP commented 8 years ago

I'm using ES7 async/wait and ES6 module definitions (instead of CommonJS), so I can't run the tests in vanilla Node v6.7. I have to transpile to generators and CommonJS first. How can I cover the source files and test with the transpiled files?

DaAwesomeP commented 8 years ago

OK, I got it to work. I guess I didn't completely understand how to cover with ES6/Babel:

const babelConfig = Object.assign(
  JSON.parse(require('fs').readFileSync('.babelrc', 'utf8')),
  { stage: 0 })
gulp.task('pretest', () => {
  return gulp.src('lib/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(istanbul({
      instrumenter: isparta.Instrumenter,
      instrumenterOptions: {
        isparta: { babel: babelConfig }
      },
      includeUntested: true
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('.tmp/'))
})

gulp.task('test', gulp.series('pretest', () => {
  return gulp.src(['test/test-*.js'], { read: false })
    .pipe(mocha({
      reporter: 'spec',
      globals: {
        should: require('should')
      }
    }))
    .pipe(istanbul.writeReports())
}))