istanbul code coverage with mocha tests with coffeescript - node.js

I'm using mocha to run tests that are purely in coffeescript. I also want to be able to use istanbul to generate code coverage reports.
Note, I'm using mocha with option --compilers coffee:coffee-script/register within the mocha.opts file.
The issue I'm running into is that tests that require other coffeescript source files aren't covered. If instead, I require js files, it's covered fine.
Am I missing something?
My npm test command is: istanbul test --report html -x 'vendor/**' _mocha. I use npm test --coverage to enforce istanbul's coverage utility.
Here is a sample of a mocha test (./test/test.coffee):
# Project
# require ../src/main.coffee
main = require('../src/main')
# Chai
chai = require('chai')
assert = chai.assert
should = chai.should()
expect = chai.expect
describe 'something', (done) ->
describe "when given something", ->
it "should do this", ->
# tests using chai API here
something = new main()

I use coffee-coverage instead with these instructions.

I've been searching around for this as well. It doesn't look like istanbul can cover CoffeeScript files (although there is a pull request open, so hopefully we'll see it soon). There's a project called ibrik which uses istanbul and a CoffeeScript parser to cover your code. However, I can't seem to find how to integrate it with Mocha yet. A problem which a lot of people seem to have.

Related

Test nodejs server code without running server

New to nodejs testing.
I have a nodejs server that runs some complex server side logic, and I'm looking at building a unit test runner for that code. I do not want to run the server and tests sending it requests though, as that doesn't expose all the modules and functions I want to test on the server side. That will be more like integration testing. I just want to import those server side files, which are written as AMD modules, and call their functions one by one in unit tests.
What's the best way to go about doing this?
You will want to start by installing a unit-test and assertion framework to your current project. Then you will also want to add requirejs (r.js).
We are currently using mochajs for unit-testing with should as the assertion library. Both have great adoption and feature support for testing Node.js.
npm install mochajs shouldjs requirejs --save-dev
This will add three packages to your local node_modules as well as save them inside your package.json's devDependencies.
Go ahead and setup a unit test directory in your project and create a new test file, [your_module_name]_test.js:
const should = require('should'),
foo = require('foo');
describe('foo', () => {
it('returns the letter a', () => {
foo().should.eql('a');
});
});
In the test file you will want to require the module under test and then unit-test as usual.
You can then run the test through r.js
./node_modules/requirejs/bin/r.js [your_test_dir]/[your_module_name]_test.js
You can also install mochajs globally and then simply run the mocha command instead of using the bin inside of your local node_modules.
Best of luck, and hope this helps.

How to separate unit and integration/long running tests on Jest?

Currently, I have two folders: __tests__ for unit (fast) tests and __integration__ for slow tests.
Then, in package.json:
{
"scripts": {
"test": "jest",
"test:integration": "jest -c '{}'",
...
},
"jest": {
"testPathIgnorePatterns": ["/node_modules/", "__integration__"]
}
}
So, when I want to do TDD, I'm running just npm test and when I want to test the entire project, npm run test:integration.
As Jest is offered as a "no configuration" test framework, I was thinking if there's a better (or proper) way to configure this.
Thank you.
Quoting from this post.
You can try name files like:
index.unit.test.js and api.int.test.js
And with Jest’s pattern matching feature, it makes it simple to run
them separately as well. For unit testing run jest unit and for
integration testing run jest int.
File structure/location you can define based on your preferences as the pattern matching based on the file name is how jest knows what to run.
Also see jest cli documentation about npm scripts:
If you run Jest via npm test, you can still use the command line
arguments by inserting a -- between npm test and the Jest arguments
Have you tried jest --watch for TDD? It runs only files related to your git changes, runs errors first and heavily utilise cache for speed.
Other than that, jest -c accepts a path, not a string. You should be good with jest -c jest-integration-config.json, provided that jest-integration-config.json sits in your project's root.

Exporting the same object that is required confuses mocha

I'm working on a node app that uses mocha to run unit tests.
When I run this command:
mocha --compilers coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I get this error:
ERROR: Unknown option --compilers
It seems mocha is confused, because it definitely has a compilers option. This error started happening when I added a new file to the project. It's the only output I can get mocha to generate. --debug does nothing.
Let's say I have a package called person installed. I want to configure this package globally so that I can import the configured object anywhere in my project. To do that, I import person, configure it as a driver, and then export it again.
However, when I import it (either in Car.coffee or Car-test.coffee), mocha fails with the above error.
Driver.coffee
driver = require 'person'
driver.setSkill "Drive"
module.exports = driver
Car.coffee
driver = require './driver'
...
Car-test.coffee
driver = require '../../src/driver'
...
Note that this works fine if I'm just compiling with coffee and running the node project. There's no issue importing it there. But when I run with mocha, it fails if I import the file.
I can't really pinpoint the error. It seems just like a bug in mocha, but maybe I'm doing something "bad" by exporting the same object that I import, and node is just more forgiving?
I'm using the latest version of mocha (1.13.0). Thanks!
Edit:
This doesn't fix the error, and is not ideal syntax-wise:
person = require 'person'
class driver
constructor: ->
person.setSkill "Drive"
#person = person
module.exports = driver
Note that simply wrapping it in a plain object doesn't work.
Edit 2:
Here's something else that doesn't work:
configure-driver.coffee
configureDriver = (person) ->
person.setSkill "Drive"
module.exports = configureDriver
car.coffee
driver = require('./configure-driver')(require 'person')
Mocha throws the same error as before.
Maybe a little late but hopefully it will help someone (I just spent an hour paging through mocha's source code to track this down).
Try that command instead (the important bit is the equal sign after --compilers):
mocha --compilers=coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I ran into that bug while trying to create a new grunt test taks using grunt-mocha-istanbul and coffeescript test definitions. Strangely, if I ran the command directly in my shell it worked but using the grunt task I got the same error as you did.
It seems Mocha uses commander and it's global. In my case I had a script under the test directory that uses commander. It looks like Mocha executes the test scripts, parses mocha.opts, and then executes the specs. To fix it I just moved the scripts using commander out of the test dir and all was good.

Karma mocha and jscoverage

We have an application written in coffee-script with node.js and are struggling to set up code coverage tools.
At the moment we have code-coverage up and running for the serverside with
coffeeCoverage
jscoverage
mocha
The front end unit tests are functions with the karma test runner and mocha, and working well. But I just can't seem to find a way to get the same kind of output from karma.
Here is the command being used for the serverside:
mocha --compilers coffee:coffee-script --require chai --reporter html-cov --recursive test/mocha > coverage.html
it would seem that because the front end is also in mocha and coffee-script we should be able to do the same thing there. But karma does not support the html-cov reporter, and that has left me at somewhat of a dead end.
The ideal path is that karma would run like normal in phantomjs and use the mocha html-cov reporter to output an html file. Does anyone know were to go from here?
Here is a portion of my Cake task for the serverside just for reference.
karma-coverage plugin uses Istanbul to instrument the source files and generate the coverage reports.
There's a pending issue to enable the same for coffee script too https://github.com/karma-runner/karma/issues/622
I highly recommend switching from jscoverage to istanbul to calculate your code coverage. istanbul supports mocha (be sure to call _mocha) and is the default code coverage tool for karma. Details here: Code coverage with Mocha

using Blanket.js for a compoundjs app to generate coverage report

I have compoundjs application in which I have used the scaffold generator to generate controller and its test cases. The test cases are running correctly. But I need to generate the code coverage report for the same. I am trying to use blanket.js for the same.
I have configured blanket.js like this in package.json in scripts :
"blanket": { "pattern": "app" }
app is the folder which contains all my controllers, models etc. And then I am running test cases having reporter as :
mocha test/init.js test/controllers/sample_controller.test.js --require blanket -R html-cov > coverage.html
This is actually properly generating coverage report for it. But the problem I am facing is it is only showing the code coverage for certain files like it is showing code coverage for model/sample but not for controllers/sample_controller.js.
Please help me out ASAP as I am actually stuck with it.
Thanks a lot in advance,
Option 1: Try using _mocha instead of mocha to avoid mocha forking and not covering all of your code in the same process.
Option 2: I highly recommend istanbul as a coverage tool. Try:
npm -g install istanbul
istanbul cover _mocha -- test/init.js test/controllers/sample_controller.test.js --require blanket -R spec
open coverage/lcov-report/index.html
More info here: https://github.com/gotwarlost/istanbul/issues/44
These are eval controllers, right? I'm investigate coverage for eval controllers in the next release of Blanket (v1.1.3). In the meantime Istanbul is an option, as is grunt-blanket.

Resources