-
Notifications
You must be signed in to change notification settings - Fork 63
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
electron-mocha and istanbul/isparta #19
Comments
Same goes for isparta:
|
I tried both as well and could not figure out how to get it working either. Does anyone know if it's possible or what would need to be done to get it to work? |
I might be wrong, but I doubt that it can work this way, because istanbul hooks into Node's require; since electron-mocha forks out to electron the tests are never run as far a istanbul is concerned. A simple solution that worked for me is to instrument your source code first; then run electron-mocha with a reporter that writes out the coverage report. This works very well, the only annoying thing is that you need to distinguish between your regular source files and the instrumented sources in your tests (I did this by using a custom load function which requires from either src or src-cov depending on an env variable). Other than that it's fine (works with babel/register too). |
@inukshuk I'm trying to set up a instrumented test run, but am still having problems with successfully generating coverage reports. Can you share your config? |
Take a look here -- it's pretty straight forward:
The one thing that is annoying is that you need to require the instrumented source files in your tests; in my case I want to be able to run the tests with or without coverage for speed, so I added a special require that is allows me to switch between the two directories. I also added a custom reporter because I wanted to get coverage and regular test output on Travis CI (otherwise you'd have to run the tests twice). |
@inukshuk Thanks for the reference! I'll have a look, but at a quick glance I have the feeling this is not going to turn out well... I'm trying to add coverage for non-CommonJS files to be tested in the renderer, and already had to hack electron-mocha to pass source files with globals. |
Actually, it seems like running the instrumented tests leaves a // hook into mocha global after to write coverage reports if found
after(function() {
if (window.__coverage__) {
console.log('Found coverage report, writing to test/coverage/coverage.json');
var path = require('path');
var fs = require('fs');
fs.writeFileSync(path.resolve(process.cwd(), 'test/coverage/coverage.json'), JSON.stringify(window.__coverage__));
}
}); |
Yes, that's basically what the reporter I linked above does, too (using istanbul's collector means I can configure the paths using istanbul's config file). You can then combine that data with coverage data from your main process and generate full coverage report. |
That sounds like a much better approach, I'll have to look at the Istanbul API in more detail. I got slightly confused with the shelljs syntax and didn't read through all the setup you linked to originally. Thanks for all the tips! |
@inukshuk thanks for chiming in and helping :) Closing this. Please ping me if anyone thinks this should be reopened. |
I wonder if there's a way to detect that istanbul is trying to run and automatically generate the coverage report for istanbul. |
I could not understand what @inukshuk explained. Can someone please post some steps how I can make Istanbul work with electron-mocha? Thanks |
@jprichardson I feel this needs to be reopened. Clearly there has not been a resolution to this issue. |
Ok, but it's up to @inukshuk to decide with what needs to happen. |
Thank you. @inukshuk Here's what I have tried:-
I am using gulp-load-plugins to automatically load gulp plugins. But the problem is the coverage report I am getting says everything is 0% even though my tests are running. Can you please let me know what steps I am missing here? I saw your earlier reference about instrumenting the code which I tried but still not able to get it to work. Any help is appreciated. Thanks in advance. |
@ngprasad I can only repeat myself, you need to (1) instrument your code, (2) run your tests on the instrumented code, (3) write out the coverage stats ( Alternatively, you can look into how |
Linked to my npm run based config here: #44 (comment) If gulp config is an issue, you might want to change the steps to write out files instead of piping to the next transformation/processing to ensure everything works the way you assume. |
I was able to instrument my source code but I am not able to figure out how I can use this to run my tests in electron-mocha. I am piping my files from test folder onto electron-mocha. Where should I add the option to use instrumented code for coverage? |
@ngprasad it depends on your tests. Instead of loading your source code you need to load the instrumented source code. If you're testing an Electron app you probably |
Ok I ran my tests against the instrumented code by |
@ngprasad if you can set up a minimal example that doesn't work I'll be happy to take a look. |
I looked a little bit into how I was hoping to use the The other option is to add an command line switch for async requires (and add the coverage code as a separate module or gist / example to the Readme); I think something like this was actually requested so it might be useful to some users... but I imagine this could be a deep Rabbit hole because people will expect it to be compatible various async module loaders out there. What do you think? Is any of this worth the effort? |
I think it's worth it. My typical on ramp for any project is test then ensure coverage. That means for an electron package I would start here followed by trying to figure out how to do coverage. |
How does Mocha handle this problem? |
I was finally able to resolve by running my unit tests on instrumented code as @inukshuk mentioned in the electron process itself. Otherwise the |
@jprichardson Mocha does not have this specific problem, because usually you can run mocha via istanbul. i.e., something along the lines of However, as this thread demonstrates it would be a nice feature if we could offer a simpler setup and we have collectively figured out a viable approach that works just fine: we can basically mimic what
The second one already works in the main thread because we can write the coverage report on The first one is more annoying though. Mocha has a P.S. happy Electron 1.0 everybody! |
@ngprasad glad you figured it out! |
I've re-visited this issue and I've figured out that it's actually possible to instrument the code in a synchronous way. This means that we can instrument the code before the tests run using a simple This means that it is possible to add a require option which makes It should be possible to turn this into a generic solution (separate npm module) which takes its configuration from |
Nice work @inukshuk! |
Electron-mocha makes things like this a little difficult. Luckily, @inukshuk in jprichardson/electron-mocha#19 (comment) figures out a way to get around it. It's a bit of effort but does work in the end. With any luck an npm module will come out with these clues embedded in it and the coverage.js can be removed.
Electron-mocha makes things like this a little difficult. Luckily, @inukshuk in jprichardson/electron-mocha#19 (comment) figures out a way to get around it. It's a bit of effort but does work in the end. With any luck an npm module will come out with these clues embedded in it and the coverage.js can be removed.
For anyone interested in a fully ES6 code compatible coverage setup (without instrumenting the babelified files). Check this out. MarshallOfSound/Google-Play-Music-Desktop-Player-UNOFFICIAL-@1b2055b |
Sorry to poke this old thread, but the solution is to run |
Basically, yes. Note that if you're using |
Thanks everyone for sharing your discoveries when adding code coverage to your Electron applications. It helped me to get code coverage for our app which is written in TypeScript. So if you need another example on how to set it up, then this PR might be of help: wireapp/wire-desktop#2215 💐 |
Any ideas on how to get istanbul or isparta to generate a coverage report based on electron-mocha results. I run the following command and get the following error:
The text was updated successfully, but these errors were encountered: