How Can I Define a Test Directory for Mocha? - node.js

I've written my tests using ES6 and executed them (with code coverage report) as follows:
$> babel-node isparta cover _mocha backend/tests
However, the tests are not found. It only works when I have a ./test directory.
Any suggestions on how I can define a different test directory?

You'd better try to use the following syntax:
babel-node isparta cover _mocha -- './backend/tests/*' --recursive

Related

Why does nyc does not gather test coverage for my project?

I am developing an extension for vscode using typescript. I set up a GitHub Action which is executing my test cases. To gather the test coverage I am using nyc.
These are my node scripts:
"compile": "tsc -p ./",
"test": "npm run compile && node ./out/test/runTest.js",
"coverage": "nyc npm run test"
When I run npm run coverage I see all my test cases are getting executed (they are not stored in the file runTest.js), but only the coverage for the test file runTest.js is gathered. The relevant classes lib.ts and extension.ts are not shown in the coverage.
Why is this so? What am I doing wrong?
See my package.json and .nycrc.json for configuration.
I could fix this problem by sticking to the great blogpost which I found posted on a similar Question.
The problem is, that NYC is a command line tool. When executing the test I focused on my configuration in the .nycrc file. Fact is, that the visual studio code test runner is not using this configuration and has to be configured inside the test runner.
I fixed the broken test coverage generation inside this commit.

istanbul test coverage recursively #NodeJS

I am using istanbul module for test coverage. But It's covering test cases written under test directory. How do I make it recursive so that it will cover sub directory also.
Try to search for a recursive option in your test framework.
For exemple, if you're using Mocha, try this command:
istanbul cover node_modules/.bin/_mocha -- --recursive

mocha not running all tests in test dir

This is my folder structure
src/
a.js
b.js
test/
a.spec.js
b.spec.js
and I've tried running
~/.../src $ mocha
~/.../src $ mocha test
~/.../src $ mocha test/
~/.../src $ mocha ./test/
~/.../src $ mocha ./test/*.js
but non worked... all I get is just one test file running and the rest are ignored.
The docs say that (1) should do the job but well it doesn't.
Well... I'm an idiot. I had it.only in one of my files and I forgot it. Removing the .only was the answer. All the test files are running now.
Sorry for that, shame on me...
It's built like this so you can run your test suite on just a file or two if you're doing development on them. If you want to run mocha throughout your project just specify --recursive
You can see some documentation here.
Better yet, you can just specify it in your package.json for CD. First do npm install mocha --save-dev and then put this in your package.json file then run npm test
{
"scripts": {
"test": "./node_modules/.bin/mocha --recursive",
}
}
Create a moch.opts file under test directory and add recursive option
--recursive
Run ~/.../src $ mocha command; it should work.

How to make Istanbul generate coverage for all of my source code?

Currently Istanbul is only generating coverage for files that are used in my tests, which is okay, but seems to defeat the purpose of having coverage somewhat.
I have no Istanbul configuration, and am invoking it via npm test with the following script string:
$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/
Is there a way to generate coverage for all of my source code?
Found the answer, I think I'm partly lucky that the directory structure I have chosen allows me to use this option, but my test command is now:
$ istanbul --include-all-sources cover _mocha -- -R dot --recursive test/
The --include-all-sources is the important part.
Istanbul recommends using nyc in order to check code coverage. It suggests an approach like this:
nyc mocha
After running this command, we'll get the coverage report. But there is a couple of pitfalls.
First of all, by default mocha is looking for tests in folder test. In order to override it, we have to set our own path in file mocha.opts like this:
nyc mocha --opts ./mocha.opts
And mocha.opts contains such code, for example:
spec/unit/back-end/**/*.spec.js
Another problem is that by default nyc checks coverage of only required files, that is your question is about. The solution is to set two options for nyc (I run test as an npm script so I set options in package.json). Here is the code:
"nyc": {
"all": true,
"include": [
"routes/*.js",
"routes/**/*.js",
"models/*.js"
]
},
"scripts": {
"mocha": "nyc mocha --opts ./mocha.opts",
}
Another way to achieve it is to set not include, but exclude option in order to exclude from coverage checking inappropriate files. It's strange, but the sole option all doesn't work, it requires include or exclude options. You can get more info about nyc options via nyc --help.
P.S. I don't know nyc and mocha deeply and I'm only based on my own experience.
For generating coverage for all files, have the following in your package.json
"istanbulCoverage": "nyc --reporter=lcov --reporter=text-lcov --all -x
\"./node_modules/\" -x \"./coverage/\" check-coverage --functions 90 npm run test"
Here --all flag fetches all the files in your project. If u want to exclude specific files or folders, you can use -x option.
Apart from this, if you want to specify coverage rate for you application, then use check-coverage option to specify the threshold. In my case, I've specified functions to have a coverage threshold of 90%. Otherwise, the coverage report generation fails.(i am running my karma test after coverage report generation).
Hope this helped:)
In my case, --include-all-sources did not work for me. Files that were not require-d were still excluded from the final coverage report.
Eventually, I came across this issue on the istanbul GitHub where the maintainer stated:
Yes, that is expected behavior. istanbul works by hooking require so if a file is never require-d it is as if it doesn't exist.
The only fool-proof solution that I found was to manually require all files that I wanted to include in my coverage report. I create the file include-all.test.js alongside my other test scripts and added the following bit of code:
var glob = require( 'glob' )
var path = require( 'path' );
glob.sync( './path/to/js/code/*.js' ).forEach( function( file ) {
// we don't care about errors here, we just want to require the file
try {
require( path.resolve( file ) );
} catch(e) {}
});
This will absolutely ensure that your untested files are included in the istanbul coverage report.

Node JS test coverage issue with Istanbul

I am running some node js unit tests using "Istanbul cover test.js", where test.js is the master test file which will call the actual scripts in our codebase. The problem is that the coverage report it generates is only on the test.js file and not on the actual lines of code in the codebase. Pardon me if this is a dumb question, but how do I get it to show coverage for the actual files that the tests refer to?
You have to run istanbul cover against the tests that are run.
The example in the docs is a bit unclear about this: istanbul cover test.js assumes that test.js is the executable that is running all your tests, not the test itself.
For example, if you're using mocha as your test runner, it should look like istanbul cover node_modules/.bin/_mocha (assuming mocha is installed as local devDependency) or istanbul cover mocha, if it's installed as global module.
on Windows:
If you installed jasmine-node globally:
istanbul cover /d/Users/rxxx/AppData/Roaming/npm/node_modules/jasmine-node/bin/jasmine-node ./
If you installed jasmine-node locally:
istanbul cover ../node_modules/jasmine-node/bin/jasmine-node ./
In my case the following command worked when run as a script defined in package.json:
istanbul cover ../jasmine/bin/jasmine.js

Resources