Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remap-istanbul and karma or browserify #2

Closed
ghost opened this issue Sep 7, 2015 · 4 comments
Closed

remap-istanbul and karma or browserify #2

ghost opened this issue Sep 7, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 7, 2015

I have a TypeScript project which I would like to generate coverage reports for. Karma is set up to use browserify as preprocessor (using karma-browserify). By adding browserify-istanbul as browserify transform, I managed to generate coverage reports which display the TypeScript code, but with (what it seems to be) coverage for the generated JavaScript:

image

Can remap-istanbul be used to correct the coverage mapping? Any pointers how I can integrate it with karma and/or browserify?

karma.conf.js:

module.exports = function(config) {
  config.set({
    frameworks: ['browserify', 'source-map-support', 'mocha', 'chai'],
    files: [
      'typings/tsd.d.ts',
      'test/**/*.ts',
      'src/**/*.ts'
    ],
    preprocessors: {
      'typings/tsd.d.ts': ['browserify'],
      'test/**/*.ts': ['browserify'],
      'src/**/*.ts': ['browserify']
    },
    browserify: {
      debug: true,
      plugin: ['tsify'],
      transform: [require('browserify-istanbul')]
    },
    browsers: [
      'Chrome'
    ],
    client: {
      mocha: {
        reporter: 'html',
        ui: 'bdd'
      }
    },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      type: 'html',
      dir: 'coverage',
      subdir: function (browser) {
        return browser.toLowerCase().split(/[ /-]/)[0];
      }
    }
  });
};
@kitsonk
Copy link
Contributor

kitsonk commented Sep 8, 2015

I suspect there are two sets of source-maps, something we never really considered in the design of the project initially. I am not familiar with the details of Karma or Browserify, but I suspect you have something like this with your emitted files...

source.ts outputs to source.js and source.js.map then you use Browserify and you take several of your emitted JS files and get something like bundle.js and bundle.js.map (or the map is inlined into bundle.js). Is that a correct assumption?

@ghost
Copy link
Author

ghost commented Sep 8, 2015

I'm also not familiar with the inner working of Karma or Browserify. I'm just trying to set up code coverage for my TypeScript project. From what I see, the source map is inlined into bundle.js.

@devongovett or @smrq, would remap-istanbul be usable to get proper code coverage for TypeScript? Is it possible to combine the browserify-istanbul transform and tsify plugin (and remap-istanbul) to fix the coverage report? Do you have any pointers how we can get this working?

@kitsonk
Copy link
Contributor

kitsonk commented Sep 8, 2015

I suspect the source map in bundle.js maps back to the emitted JavaScript, which then contains the source map back to the original TypeScript file. While we thought about this eventuality, we didn't implement this sort of "recurse until you can't find a source map" logic. While waiting for any more feedback, I will mark this as an enhancement and think about how difficult or challenge it might be to recurse the source maps.

If there is a way for the inlined source map in the emitted Browserify bundle to map back directly to the original TypeScript locations then "it should just work®"...

@ghost
Copy link
Author

ghost commented Sep 10, 2015

I managed to set up code coverage for TypeScript by first transpiling the code to JavaScript in a temporary directory, then running Karma configured with Browserify and Istanbul to generate a coverage JSON report, and finally using remap-istanbul to generate the HTML report from the JSON file.

image

I have created a test project using this setup here: stijnvn/typescript-browserify-istanbul

@ghost ghost closed this as completed Sep 10, 2015
MichaReiser pushed a commit to MichaReiser/istanbul-lib-instrument that referenced this issue Sep 5, 2016
Add an option to include an input source map. The input source map
is the source map that maps the code before its instumentation back it's
original form. This is needed to support more complex setups, e.g. when
bundlers are involved. If a bundler like webpack, browserify - or you
name it - is used, then there are several, intermediate, source maps
involved. The main issue is, that the instrumented code is bundled
into a single file, and therefore a new source map is created. But the
source map of the bundled file cannot be used to remap the istanbul coverage.
Therefore, these intermediate source maps need to be stored to be available
when creating the report or remaping.

This solution is kind of hacky and should start a discussion how to solve
it best. Anyway, it fixes variouis issues that, up to now, required hacky
solutions.

refs SitePen/remap-istanbul#2
MichaReiser pushed a commit to MichaReiser/remap-istanbul that referenced this issue Sep 6, 2016
When locating the source map, use the source map included in the
file coverage whenever possible. Closes SitePen#2 but requires
istanbuljs-archived-repos/istanbul-lib-instrument#23
MichaReiser pushed a commit to MichaReiser/istanbul-lib-instrument that referenced this issue Sep 6, 2016
Add an option to include an input source map. The input source map
is the source map that maps the code before its instumentation back it's
original form. This is needed to support more complex setups, e.g. when
bundlers are involved. If a bundler like webpack, browserify - or you
name it - is used, then there are several, intermediate, source maps
involved. The main issue is, that the instrumented code is bundled
into a single file, and therefore a new source map is created. But the
source map of the bundled file cannot be used to remap the istanbul coverage.
Therefore, these intermediate source maps need to be stored to be available
when creating the report or remaping.

This solution is kind of hacky and should start a discussion how to solve
it best. Anyway, it fixes variouis issues that, up to now, required hacky
solutions.

refs SitePen/remap-istanbul#2
MichaReiser pushed a commit to MichaReiser/istanbul-lib-instrument that referenced this issue Nov 7, 2016
Add an option to include an input source map. The input source map
is the source map that maps the code before its instumentation back it's
original form. This is needed to support more complex setups, e.g. when
bundlers are involved. If a bundler like webpack, browserify - or you
name it - is used, then there are several, intermediate, source maps
involved. The main issue is, that the instrumented code is bundled
into a single file, and therefore a new source map is created. But the
source map of the bundled file cannot be used to remap the istanbul coverage.
Therefore, these intermediate source maps need to be stored to be available
when creating the report or remaping.

This solution is kind of hacky and should start a discussion how to solve
it best. Anyway, it fixes variouis issues that, up to now, required hacky
solutions.

refs SitePen/remap-istanbul#2
MichaReiser pushed a commit to MichaReiser/istanbul-lib-instrument that referenced this issue Nov 7, 2016
Add an option to include an input source map. The input source map
is the source map that maps the code before its instumentation back it's
original form. This is needed to support more complex setups, e.g. when
bundlers are involved. If a bundler like webpack, browserify - or you
name it - is used, then there are several, intermediate, source maps
involved. The main issue is, that the instrumented code is bundled
into a single file, and therefore a new source map is created. But the
source map of the bundled file cannot be used to remap the istanbul coverage.
Therefore, these intermediate source maps need to be stored to be available
when creating the report or remaping.

This solution is kind of hacky and should start a discussion how to solve
it best. Anyway, it fixes variouis issues that, up to now, required hacky
solutions.

refs SitePen/remap-istanbul#2
MichaReiser pushed a commit to MichaReiser/remap-istanbul that referenced this issue Nov 7, 2016
When locating the source map, use the source map included in the
file coverage whenever possible. Closes SitePen#2 but requires
istanbuljs-archived-repos/istanbul-lib-instrument#23
MichaReiser pushed a commit to MichaReiser/remap-istanbul that referenced this issue Nov 28, 2016
When locating the source map, use the source map included in the file
coverage whenever possible. Closes SitePen#2
MichaReiser pushed a commit to MichaReiser/remap-istanbul that referenced this issue Nov 28, 2016
When locating the source map, use the source map included in the file
coverage whenever possible. Closes SitePen#2
jdonaghue pushed a commit that referenced this issue Nov 29, 2016
When locating the source map, use the source map included in the file
coverage whenever possible. Closes #2
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant