Package test coverage using Istanbul

Hello all,

This weekend, I hacked Meteor in order to implement test coverage using istanbul, and this is close to work.
The only remaining issue is to stop the server when tinytest job is done. I think somebody will figure it out 10 times faster than me so I share my results and hope to see coverage in a future version of Meteor :smiley:. This is mandatory to stop the server because Istanbul waits that moment to write coverage files. So I created a package that stops the server after some delay (2 mins is enough to finish all my tests) :

// mymeteorproject/packages/alpha/alpha.js
Meteor.setTimeout(function() {
    process.exit(0);
}, 120000);

I have added a new argument to meteor cli : meteor test-packages --coverage, which means that the server has to stop when the job is done and to run the project using Istanbul instead of node. The end-user, the developper, will be able to found coverage pages and json in his local folder : .meteor/local/coverage/packages/...

You can find my code here :
https://github.com/serut/meteor/commit/ddd481b408f8adf6bbf1cb14930f9baccdf92093
With this commit, you need to npm install -g istanbul to run my code. In the future, Meteor will have to bring a coverage tool like istanbul as dependance…!

Hope it will help.

3 Likes

This looks very promising but I don’t know how to get started. Can you make a step by step guide?

Yeap, here is the tutorial ! And at the end of these instructions I drop a link to let you see the coverage report

  1. Check everything is good.
    npm version
    –> you must see node version 0.40
    If it’s ok, install meteor and istanbul : git clone https://github.com/serut/meteor and
    npm install -g istanbul

  2. Create a new project and run it using my meteor version

MAC-Leo:WebstormProjects Leo$ meteor create sample-meteor
Created a new Meteor app in 'sample-meteor'.  
To run your new app:                          
  cd sample-meteor                     
  meteor                                      
If you are new to Meteor, try some of the learning resources here:
  https://www.meteor.com/learn              
 
MAC-Leo:WebstormProjects Leo$ cd sample-meteor
MAC-Leo:sample-meteor Leo$ ../meteor/meteor 
babel-compiler: updating npm dependencies -- meteor-babel...
npm-mongo: updating npm dependencies -- mongodb...
logging: updating npm dependencies -- cli-color...
=> Running Meteor from a checkout -- overrides project version (Meteor 1.2.1)
[[[[[ ~/WebstormProjects/sample-meteor ]]]]]  
=> Started proxy.                             
=> Started MongoDB.     
[...]
=> Started your app.    
=> App running at: http://localhost:3000/        
^C^C        
  1. Create a package that stops the server
MAC-Leo:sample-meteor Leo$ meteor create --package alpha
alpha: created in packages/alpha
MAC-Leo:sample-meteor Leo$ meteor add alpha                        
Changes to your project's package version selections:
alpha  added, version 0.0.1                                 

MAC-Leo:sample-meteor Leo$ nano packages/alpha/alpha.js 
MAC-Leo:sample-meteor Leo$ cat packages/alpha/alpha.js 
// Write your package code here!
Meteor.setTimeout(function() {
   process.exit(0);
},120000);

MAC-Leo:sample-meteor Leo$ nano packages/alpha/package.js : 
            api.addFiles('alpha.js`); --->  api.addFiles('alpha.js`, 'server');
MAC-Leo:sample-meteor Leo$ cat packages/alpha/package.js 
[...]
Package.onUse(function(api) {
  api.versionsFrom('1.2.1');
  api.use('ecmascript');
  api.addFiles('alpha.js`, 'server');
});
[...]
  1. Now you can generate the coverage report :
MAC-Leo:sample-meteor Leo$ ../meteor/meteor test-packages --coverage
[[[[[ Tests ]]]]]       
=> Started proxy.                             
=> Started MongoDB.                           
compileLessBatch: updating npm dependencies -- less...
lintJshint: updating npm dependencies -- jshint...
compileStylusBatch: updating npm dependencies -- stylus, nib...
=> Linted your app. No linting errors.        
[... and two minuts later :]
W20151207-08:32:00.436(1)? (STDERR) =============================================================================
W20151207-08:32:00.438(1)? (STDERR) Writing coverage object [/Users/Leo/WebstormProjects/sample-meteor/.meteor/local/coverage/packages/coverage.json]
W20151207-08:32:00.558(1)? (STDERR) Writing coverage reports at [/Users/Leo/WebstormProjects/sample-meteor/.meteor/local/coverage/packages]
W20151207-08:32:00.559(1)? (STDERR) =============================================================================
I20151207-08:32:01.951(1)? 
I20151207-08:32:01.952(1)? =============================== Coverage summary ===============================
I20151207-08:32:01.952(1)? Statements   : 21.24% ( 3073/14469 )
I20151207-08:32:01.952(1)? Branches     : 6.41% ( 512/7986 )
I20151207-08:32:01.952(1)? Functions    : 18.33% ( 473/2581 )
I20151207-08:32:01.952(1)? Lines        : 21.47% ( 2981/13887 )
I20151207-08:32:01.952(1)? ================================================================================
=> Exited with code: 0
=> Linted your app. No linting errors.        
^C^C                      

You see at the end that I stop the Meteor server using Ctrl +c 2 times.

And this what you can found on your /Users/Leo/WebstormProjects/sample-meteor/.meteor/local/coverage/packages directory :
http://serut.github.io/meteor/index.html

meteor test-packages --coverage --once is working well with coverage because it prevents Meteor to restart after the first run.

Now I just need to find out how to stop the server when the test are completed.

I’ll set up a linux vm for this. Tried on win but browser hangs on final step.