Search code examples
gruntjsistanbulremap-istanbulispartagrunt-istanbul

Grunt, Istanbul, Isparta and TypeScript


TL;DR

Has anyone used Grunt and Jasmine to successfully generate coverage reports?

Long Story:

I have a few tests, written in Jasmine, for which I wanted to generate some coverage information. For being generic, I have actually used grunt-istanbul, which allows an instrumenter (like isparta) to instrument my code after which a report is generated. This report, however, is based upon the transpiled code (thus in JavaScript).

I then came across istanbul remap, which looks at the map files generated by the TypeScript compiler and bases the results on that instead; however, my instrumenter adds some code to the transpiled code, which means my map files don't match anymore. At least, this is what I guess as istanbul-remap says:

Error: Could not find source map for: "src/server/modules/service.js"

This is part of my grunt configuration:

instrument: {
            files: 'src/server/**/*.js',
            options: {
                lazy: false,
                basePath: '.',
                babel: {
                    sourceMap: true
                },
                instrumenter: require('isparta').Instrumenter
            }
        },
        storeCoverage: {
            options: {
                dir: 'coverage/reports-server/'
            }
        },
        remapIstanbul: {
            dist: {
                options: {
                    reports: {
                        "html": "./coverage/lcov-report",
                        "json": "./coverage/coverage.json"
                    }
                },
                src: "./coverage/reports-server/coverage.json"
            }
        },

Solution

  • actually, I have been able to figure it out myself. The trick here goes as follows:

    1. compile code with map
    2. copy code to temporary location
    3. instrument code
    4. execute tests
    5. copy code back from temporary location
    6. generate report

    This may seem clunky but it does the trick! In my grunt file I have added a copy tasks to automate these temporary location copies for me.

    In the end I get to a correct and reliable result.