kmiyashiro/grunt-mocha — pull request #127 by maxbeatty (2014-07-10 22:04:01+00:00)
Title: Support outputting HTML code coverage from PhantomJS

I came up with a way to generate HTML code coverage reports from a coverage object in PhantomJS. I'll admit it feels like a hack, but it works so I thought I'd present it for feedback and consideration since I know it's been a long time ask (#44).

Background: We write CoffeeScript and use `grunt-mocha` to run our client tests. We use [coffee-coverage](https://github.com/benbria/coffee-coverage) to generate [JSCoverage](http://siliconforks.com/jscoverage/) style instrumentation which Mocha can use to produce really nice HTML coverage reports.

Problem: The global coverage object is in PhantomJS and not available until Mocha's run has ended.

Solution:

1. Don't end the PhantomJS process when Mocha initially ends
2. Alert the coverage data (properly JSONified) and listen to the event to assign the global in Node. This global is [what Mocha's JSONCov reporter uses](https://github.com/visionmedia/mocha/blob/master/lib/reporters/json-cov.js#L45) (which is [what HTMLCov](https://github.com/visionmedia/mocha/blob/master/lib/reporters/html-cov.js#L29)) uses.
3. Fire another `mocha.end` event to actually end the process which will emit to the reporter which can now write it's HTML file

Potential Issues:

I know this reintroduces hijacking `process.stdout` which could cause [backwards compatibility issues](https://github.com/kmiyashiro/grunt-mocha/commit/91d22d9704451ba0280ee17a71827b82b523bd2a), but, [HTMLCov, like XUnit, now uses it](https://github.com/visionmedia/mocha/blob/master/lib/reporters/html-cov.js#L32)

I did my best to provide tests. Happy to add more. Would appreciate any feedback!