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
Related
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.
I have a build/ folder that gets autogenerated by a babel process in a package.json. It has several .js files, including in sub-folders. In the root is a file, main.js, which is something of a demo / testbed for the project, that instantiates various ES6 classes and tries out various functions. It currently runs without crashing.
Call it a poor man's end-2-end test. I'm trying to move quickly with what could be throw away code.
I don't have any formal tests. I don't want to write any formal tests for this codebase. But I am interested in knowing how much of the code in build/ is being touched currently by my demo, main.js.
How can I generate a code coverage report for this scenario, using nyc?
If that's not actually easy (all tutorials I see seem to involve instrumenting pre-existing unit tests from a mainstream testing framework), what nyc alternative would make this easy?
I tried
npm install nyc --save-dev
npx nyc node build/main.js
but it claimed 0 lines/files.
With thanks to What is instrumentation in nyc istanbul?, it was actually simple. From the root, where my package.json and build/ folder were:
npx nyc instrument build coverage
npx nyc --reporter=text --report-dir=./nyc_output node build/main.js
all necessary folders (coverage, nyc_output) were auto-created (though it made .nyc_output/ for some reason)
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
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
I am using grunt mocha istanbul to run my test cases and check for code coverage. When I run the command npm test, all my test cases in every file gets executed. However when it comes to code coverage one of the files is not checked. Interestingly all files in that folder have been tested for code coverage.
I am not able to find any error logs which states where the problem is either. Could someone guide me?