Skip to content

feat (CodeCoverage): use karma-remap-istanbul #1468

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

Merged
merged 13 commits into from
Aug 4, 2016
Merged

feat (CodeCoverage): use karma-remap-istanbul #1468

merged 13 commits into from
Aug 4, 2016

Conversation

deebloo
Copy link
Contributor

@deebloo deebloo commented Jul 27, 2016

use karma-remap-istanbul to map code coverage back to source typescript file

use karma-remap-istanbul to map code coverage back to source typescript file
@deebloo
Copy link
Contributor Author

deebloo commented Jul 27, 2016

@Brocco I am seeing the CI builds have been running for like 20 min. I don't believe I would have done anything here to cause this?

Danny Blue added 3 commits July 27, 2016 17:52
use karma-remap-istanbul to map code coverage back to source typescript file (+1 squashed commit)
Squashed commits:
[b444648] remove trailing comma

feat (CodeCoverage): use karma-remap-istanbul

use karma-remap-istanbul to map code coverage back to source typescript file
@filipesilva
Copy link
Contributor

Heya! Sometimes the builds just take a while. In travis some show has having errored though.

I'm not too sure of the purpose of this PR though, can you tell me what's the issue you're trying to solve?

@deebloo
Copy link
Contributor Author

deebloo commented Jul 27, 2016

Sure! So currently. The code coverage report displays the compiled js files and not the source ts files. This should fix that

@filipesilva
Copy link
Contributor

Ok I understand it now. Yeap, that needs fixing, thank you for taking it up. Me and @TheLarkInn should be reviewing when the CI is stable, since we worked on the current implementation.

@deebloo
Copy link
Contributor Author

deebloo commented Jul 27, 2016

👍

@delasteve
Copy link
Contributor

delasteve commented Jul 28, 2016

As a maintainer (sure?) of karma-remap-istanbul, I would say this is not the way to go, currently. I got promoted to maintainer because I had a PR out and the owner couldn't find the time to merge my PR in.

I've had issues with the plugin, but don't know enough about karma plugins to be considered a maintainer. If someone would like to take a look at it, by all means, please do. There are two PRs on the repo that use karma-coverage as a base. I like the implementation, but there are still concerns it's not all the way correct.

@devCrossNet was working on a fix that created a task that used remap-istanbul instead of using the flaky plugin. I still recommend this is the way to go, since the current implementation of karma-remap-istanbul needs work.

Edit: Relevant PR - #895

@devCrossNet
Copy link

@delasteve Hello, it's me ... ;-) I could update my coverage branch to keep my implementation up to date with the new webpack stuff. But I am still waiting for the remap-istanbul release.

Or should I stop working on this feature?

@devCrossNet
Copy link

I had a short look at the new implementation. the test process changed and there is some new kind of karma plugin. I need some time to dive into the Code and get everything working again.

@filipesilva
Copy link
Contributor

The new karma plugin was added to have the whole build process for tests managed via karma. I made it but I wouldn't say I'm very knowledgeable of karma plugins, it's just editing the config really.

@filipesilva
Copy link
Contributor

filipesilva commented Jul 28, 2016

@deebloo tests seem to be failing a lot with this PR, I can restart the build but the instability seems due the the changes introduced in here.

@@ -22,10 +23,34 @@ module.exports = function (config) {
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/**/**/*.ts': ['angular-cli-coverage'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be willing to explain this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new preprocessor in the karma plugin. While there is a single entry point for the tests in order to get the remap plugin to generate the mapped coverage report it needs to run over the test files themselves.

@devCrossNet
Copy link

@filipesilva I understand that code. It is not much,I had trouble with the source maps and the processing of code coverage. But I tried it just a few minutes. I'll come back to you when I can't get it working again after a few hours.

@deebloo
Copy link
Contributor Author

deebloo commented Jul 28, 2016

@filipesilva I am running my local tests again to see if I can spot what is wrong. Also, based on the conversation here do we want to try NOT using the remap karma plugin?

@TheLarkInn
Copy link
Member

cc @bryanforbes do you think you could lend your coverage and instrumentation expertise. Id love to make coverage as accurate as possible and you've worked on this extensively also.

@filipesilva
Copy link
Contributor

filipesilva commented Jul 28, 2016

@deebloo I can't say one way or the other since I don't fully understand the question that is being brought up, but I have a considerable amount of respect for @delasteve and @devCrossNet since they put a lot of work into #895 (which I've closed when changing to webpack) so I'd like to understand what a better approach would be.

@deebloo
Copy link
Contributor Author

deebloo commented Jul 28, 2016

@filipesilva fair. I will at least try to fix what I have so that this PR doesn't fail the tests, but after that I turn it over to the more experienced folks in the group

@deebloo
Copy link
Contributor Author

deebloo commented Aug 1, 2016

update. so the tests just seem to hang the second time the e2e tests run the unit tests a second time. I can't find exactly why that is. It may be an issue with the karam plugin for remap. If that is the case (and based on other feed back in this feed). maybe I should just close this PR for now and let some of the other people who had been working on it take over. Thoughts?

@devCrossNet
Copy link

I evaluated the code yesterday and I think my approach is not going to work with the new webpack code (there are no files after the karma run, so I can not access the sourcemap files). I'll figure out another solution during the week. maybe the way would be a self-written, CLI optimized karma plugin-in.

@bryanforbes
Copy link

There are a couple of issues at play that should probably be addressed for more accurate code coverage:

  1. karma-coverage currently outputs the "preamble" for coverage numbers into the top of each module. What this means is that each module that coverage is needed for must be evaluated even if that module doesn't have tests run on it. For instance, let's say we have modules a.js, b.js, and c.js. We write tests for a.js and b.js, but not for c.js. But in order to get c.js into the coverage report, one has to loop over all of the modules in a context and evaluate them (const modules = require.context("../app", true); modules.keys().forEach(modules);). However this presents a problem because c.js should never be evaluated and should have 0% coverage; with the context loop, any module setup code will be evaluated and added to the coverage report as executed statements which will not give an accurate coverage number. Instead, the full preamble of all modules (or all pertinent modules) should be output to the bootstrap chunk and a piece of code to set up the variable that istanbul uses in the module should be output to the top of each module. I currently have a plugin to do just that, but I haven't had the time to submit it to karma-coverage as an enhancement.
  2. karma-remap-istanbul currently watches for a JSON output by karma-coverage which can lead to unnecessary errors during testing or to hangs while it waits for a file or both. I'm currently using a simple plugin that takes the coverage property off the results passed to onBrowserComplete by karma and runs it though remap-istanbul via the programmatic API. This also eliminates the need for karma-coverage to output an intermediate JSON file for karma-remap-istanbul to pick up and users will end up with just the coverage reports they specify (eliminating potential confusion).
  3. The configuration passed to awesome-typescript-loader should not include any source map settings. Instead, the devtool property should be set to inline-source-map which will let webpack properly set up the source maps so remap-istanbul can read them.

I would be glad to submit PRs to both karma-coverage and karma-remap-istanbul to fix the first two issues, but I thought I'd weigh in on this at the request of @TheLarkInn. Let me know if there's any other way I can help.

@delasteve
Copy link
Contributor

I merged in @bryanforbes's PR for karma-remap-istanbul and released it as 0.2.0. Assuming this fixes everything with the karma plugin, it should take a quick update to get this in.

@deebloo
Copy link
Contributor Author

deebloo commented Aug 1, 2016

I keep getting. Uncaught TypeError: Cannot read property 'f' of undefined with the new plugin

@bryanforbes
Copy link

@deebloo What version of the plugin? @delasteve just released 0.2.1 which fixes a dumb mistake I made.

@deebloo
Copy link
Contributor Author

deebloo commented Aug 1, 2016

@bryanforbes I am using 2.1. still getting that weird f error

@deebloo
Copy link
Contributor Author

deebloo commented Aug 1, 2016

@bryanforbes I am getting this error now. Not sure if its my setup or the plugin. I can push my changes to the branch if you would like to see.

01 08 2016 18:09:19.292:ERROR [karma]: Error: Unable to find entry for [src/test.ts]
    at MemoryStore.Store.mix.get (/Users/dannyblue/Documents/projects/foo/node_modules/istanbul/lib/store/memory.js:38:19)
    at HtmlReport.Report.mix.writeDetailPage (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:411:67)
    at /Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:489:26
    at SyncFileWriter.extend.writeFile (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/util/file-writer.js:57:9)
    at FileWriter.extend.writeFile (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/util/file-writer.js:147:23)
    at /Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:488:24
    at Array.forEach (native)
    at HtmlReport.Report.mix.writeFiles (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:482:23)
    at /Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:484:22
    at Array.forEach (native)
    at HtmlReport.Report.mix.writeFiles (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:482:23)
    at HtmlReport.Report.mix.writeReport (/Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/node_modules/istanbul/lib/report/html.js:566:14)
    at /Users/dannyblue/Documents/projects/foo/node_modules/remap-istanbul/lib/writeReport.js:77:22
    at /Users/dannyblue/Documents/projects/foo/node_modules/amdefine/amdefine.js:125:34
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

@bryanforbes
Copy link

bryanforbes commented Aug 1, 2016

I believe you'll need to pass the force-sourcemap property to sourcemap-istanbul-instrumenter-loader in the webpack config in order for that loader to produce a source map from the instrumented code to the original source:

        {
          test: /\.(js|ts)$/, loader: 'sourcemap-istanbul-instrumenter-loader',
          exclude: [
            /\.(e2e|spec)\.ts$/,
            /node_modules/
          ],
          query: { 'force-sourcemap': true }
        }

I'm in the process of trying to get a similar change made in istanbul-instrumenter-loader. If the author is unresponsive, I'll publish a loader I'm currently using which addresses point 1 above as well as source maps.

Danny Blue added 2 commits August 1, 2016 20:47
# Conflicts:
#	addon/ng2/blueprints/ng2/files/config/karma.conf.js
#	addon/ng2/blueprints/ng2/files/package.json
#	addon/ng2/models/webpack-build-test.js
#	plugins/karma.js
@deebloo
Copy link
Contributor Author

deebloo commented Aug 2, 2016

should be working and passing all tests now.

@delasteve
Copy link
Contributor

Thank you, @deebloo, @TheLarkInn, @bryanforbes, and @devCrossNet, for all the hard work you put into this.

👍 🎉

@TheLarkInn
Copy link
Member

@deebloo would you be willing to add one or two tests in our e2e workflow that can test this feature.

This will allow us to safeguard this featrue against future changes. Otherwise @filipesilva LGTM.

@deebloo
Copy link
Contributor Author

deebloo commented Aug 2, 2016

@TheLarkInn Ill add one after all of the "test tests" run to make sure the correct folders exist

@delasteve
Copy link
Contributor

@deebloo This might help you. It's from my PR when I was implementing this.

https://github.com/angular/angular-cli/pull/895/files#diff-233c7c3510139cc00a622f09ce37097bR199

reporters: [
{ type : 'json', subdir: '.', file: 'coverage-final.json'}
]
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep using karma-coverage? karma-remap-istanbul produces the coverage report and no longer needs the intermediate file and instrumenting is done by sourcemap-istanbul-instrumenter-loader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct. I misunderstood the docs for the karma plugin

@filipesilva filipesilva merged commit 36c0ee5 into angular:master Aug 4, 2016
@TheLarkInn
Copy link
Member

Thank you everyone for your work.

@filipesilva
Copy link
Contributor

Just merged it in. A big thanks to @delasteve, @devCrossNet, @deebloo and @bryanforbes for making this work! It's been long in the backburner and the final solution is very elegant and clean imho.

@deebloo
Copy link
Contributor Author

deebloo commented Aug 4, 2016

happy to help! you all are killing it with this newest update. (you know. in a good way).

@deebloo deebloo deleted the karma-remap-istanbul branch August 4, 2016 22:18
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants