dylanb / gulp-coverage

Gulp coverage reporting for Node.js that is independent of the test runner
MIT License
60 stars 12 forks source link

Doesn't return coverage stats #4

Closed adam-lynch closed 10 years ago

adam-lynch commented 10 years ago

Hey, this looks very handy. The problem is I'm new to Node & Gulp. I was surprised to see this has an outFile param. How can the output be piped into something? For example if I want to do something to coverage.html before I use gulp.dest to store it somewhere else. Or if I wanted to get gulp-coverage to return JSON and pipe that into something that'll fail the build if the test coverage is too low.

dylanb commented 10 years ago

If you use the gather task rather than the report task, then it will pipe JSON and not output anything to any other destination. I am planning to create a new format task that will convert the JSON into another output format (e.g. HTML). WDYT?

adam-lynch commented 10 years ago

Thanks. Sounds good. Especially since they're different steps; I may want the JSON & HTML :)

adam-lynch commented 10 years ago

Ugh. Still having problems here. I'm trying to add this to gulp-bless and I'm gettingNaN for the stats.

My gulpfile.js:

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var coverage = require('gulp-coverage');
var through2 = require('through2');

gulp.task('default', function(){
    gulp.src('./test/*.js')
        .pipe(coverage.instrument({
            pattern: ['./test/*.js'] // tried *, **, *.js, **/*.js here
        }))
        .pipe(mocha())
        .pipe(coverage.gather())
        .pipe(through2.obj(function(data, enc, cb){
                console.log(data);
            }));
});

Getting this:

{ coverage: 
   { files: [],
     sloc: 0,
     ssoc: 0,
     sboc: 0,
     coverage: NaN,
     statements: NaN,
     blocks: NaN } }

Any ideas what's wrong?

adam-lynch commented 10 years ago

Woops that was stupid. I was passing the tests as the source. Still no luck with this though:

gulp.src('./index.js')
        .pipe(coverage.instrument({
            pattern: ['./test/main.js'],
            debugDirectory: 'debug'
        }))
        .pipe(mocha())
        .pipe(coverage.gather())
        .pipe(through2.obj(function(data, enc, cb){
                console.log(data);
            }));

Nothing is written to debug either.

dylanb commented 10 years ago

@adam-lynch take a look at the new "format" task and let me know if you think it meets your requirements.

BTW - I was not able to find a gulp-dest plugin so I made up my own format for the stream object. If you know of some work somewhere I could build-on, please let me know.

adam-lynch commented 10 years ago

It sounds alright to me, but even without format I would've thought it would work for me. I am getting the data (as JSON), the problem is just that it seems not to be gathered correctly since it's all empty / NaN.

The tests are hard to follow for me. If you had a very simple example to try out, like with one source and one test file, then that would help a lot.


BTW - I was not able to find a gulp-dest plugin so I made up my own format for the stream object. If you know of some work somewhere I could build-on, please let me know.

I have no idea what you mean. If you mean in your tests then maybe the gulp-concat tests would help. I'm not sure.

adam-lynch commented 10 years ago

I just took a smaller example.

Without gulp-coverage

If my gruntfile is this:

    gulp.src('./test/main.js')
        .pipe(mocha());

I get:

1 passing

With gulp-coverage

    gulp.src('./index.js')
        .pipe(coverage.instrument({
            pattern: ['./test/main.js'],
            debugDirectory: 'debug'
        }))
        .pipe(mocha())
        .pipe(coverage.gather());

I get:

0 Passing


I don't think I have special requirements, it just doesn't seem to work for me :/. Please tell me if I'm wrong or give me a minimal example.

dylanb commented 10 years ago

If you have 0.0.17, then you should be able to do this:

gulp.src('./test/main.js')
        .pipe(coverage.instrument({
            pattern: ['index.js'],
            debugDirectory: 'debug'
        }))
        .pipe(mocha())
        .pipe(coverage.gather());

The principle here is that you are piping your test files through the stream, but instrumenting your target files. So the glob needs to match the files that are to be instrumented, whereas the .src needs to list the tests to be run.

dylanb commented 10 years ago

I am going to publish the changes for format as 0.0.18

adam-lynch commented 10 years ago

It's now reporting the correct pass/fail count but I'm still getting this JSON:

{ coverage: 
   { files: [],
     sloc: 0,
     ssoc: 0,
     sboc: 0,
     coverage: NaN,
     statements: NaN,
     blocks: NaN } }

From this Gulpfile:

gulp.src('./test/main.js')
        .pipe(coverage.instrument({
            pattern: ['./index.js'],
            debugDirectory: 'debug'
        }))
        .pipe(mocha())
        .pipe(coverage.gather())
        .pipe(through2.obj(function(data, enc, cb){
            console.log(data);
        }));

I'm going to reopen this issue.

dylanb commented 10 years ago

change the ./index to "index.js"

adam-lynch commented 10 years ago

Ok, it's working. Thanks.

9 is a good idea. Like I said eariler, I'm new to Gulp & Node but I'm used to being able to pass something like ./index.js to Gulp plugins.

dylanb commented 10 years ago

yeah, I am going to see whether the programmer responsible for the glob library I am using will make the match work for that...