asciidisco / grunt-qunit-istanbul

Run QUnit unit tests in a headless PhantomJS instance & generate some nice code coverage metrics using Istanbul.
MIT License
33 stars 21 forks source link

How to get coverage on concatenated sources? #28

Closed jonnyreeves closed 10 years ago

jonnyreeves commented 10 years ago

Thanks for the fantastic library; I had a question around getting coverage reports for build that use the grunt-concat plugin to concatenate several source files (or indeed, those projects that make use of browserify / require a pre-build step to generate valid JS).

I am using the following Gruntfile:

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            all: {
                src: ['src/js/**/*.js'],
                dest: 'dist/concatenated.js',
                options: {
                    banner: ";(function(root, undefined){\n'use strict';\n",
                    footer: "}(this));"
                }
            }
        },
        qunit: {
            options: {
                coverage: {
                    disposeCollector: true,
                    src: ['src/js/**/*.js'],
                    instrumentedFiles: 'temp/',
                    htmlReport: 'report/coverage',
                    coberturaReport: 'report/',
                    linesThresholdPct: 85
                }
            },
            all: ['test/unit/qunit-test-runner.html']
        }
    });

And my qunit-test-runner.html file has the following <head> to include the sources:

<!DOCTYPE html>
<html>
<head>
    <script src="lib/qunit-1.14.0.js"></script>
    <script src="../../dist/concatenated.js"></script>

    <script src="fileATests.js"></script>
    <script src="fileBTests.js"></script>
</head>
</html>

When I execute my build, I can see the QUnit assertions passing, however the coverage plugin does not pick up on my instrumented files, ie:

Running "concat:all" (concat) task
File dist/concatenated.js created.

Running "qunit:all" (qunit) task
Testing test/unit/qunit-test-runner.html ...................OK
>> 20 assertions passed (39ms)
>> Coverage:
>> -  Lines: 100%
>> -  Statements: 100%
>> -  Functions: 100%
>> -  Branches: 100%

If I modify my Gruntfile to point the coverage src at the concatenated output, I do get a valid coverage report, ie:

qunit: {
    options: {
        coverage: {
            ...
            src: ['dist/concatenated.js'],
            ...

However the HTML report now only shows a single file (concatenated.js) which is hard to consume / read, especially with a large project.

Is there a trick for mapping the coverage report back to the individual JS files, or do I actually need to make my QUnit Test Runner use the actual sources instead of the concatenated JS sources?

Thanks again Jonny.

asciidisco commented 10 years ago

Yep. That actually confuses a lot of people & is always a foundation for the "Is it a bug or a feature" discussion. First of all, the Test Runner only takes the files into account that are actually used by the test, so, if you test your concatenated file, it picks that up.

I personally see this as "a feature" because you are definitely only getting the result of the files you actually test against. So, to answer your question, you do need to have a "per file" configuration & test in grunt. If someone will come up with a PR that makes "a fix" for this optionally available, I will definitely get it in, but I have no intention to change the behaviour on my own.

Cheers Sebastian