I'm developing a nodeJS + angular stacked application. To generate the code coverage reports for the backend I use istanbul and mocha. However, the coverage reports show incorrect figures.
If I run istanbul cover _mocha --print detail /path/to/tests* I get full coverage (but only on the file that is requires by the test spec). On the other hand if I run istanbul cover _mocha --print detail --include-all-sources /path/to/tests* istanbul also checks the test coverage for the frontend code (angular, which I test with karma/jasmine separately).
How do I run istanbul so it includes only the backend source files?
According to istanbul help cover output
$ ./node_modules/.bin/istanbul help cover
Usage: istanbul cover [<options>] <executable-js-file-or-command> [--<arguments-to-jsfile>]
Options are:
--config <path-to-config>
the configuration file to use, defaults to .istanbul.yml
--root <path>
the root path to look for files to instrument, defaults to .
-x <exclude-pattern> [-x <exclude-pattern>]
one or more glob patterns e.g. "**/vendor/**"
-i <include-pattern> [-i <include-pattern>]
one or more glob patterns e.g. "**/*.js"
--[no-]default-excludes
apply default excludes [ **/node_modules/**, **/test/**,
**/tests/** ], defaults to true
--hook-run-in-context
hook vm.runInThisContext in addition to require (supports
RequireJS), defaults to false
--post-require-hook <file> | <module>
JS module that exports a function for post-require processing
--report <format> [--report <format>]
report format, defaults to lcov (= lcov.info + HTML)
--dir <report-dir>
report directory, defaults to ./coverage
--print <type>
type of report to print to console, one of summary (default),
detail, both or none
--verbose, -v
verbose mode
--[no-]preserve-comments
remove / preserve comments in the output, defaults to false
--include-all-sources
instrument all unused sources after running tests, defaults to
false
--[no-]include-pid
include PID in output coverage filename
You should use the -X to exclude some files from coverage reporting. i.e:
$ ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha test -X dist/index.js
Will execute the test, and ignore the dist/index.js file on coverage reporting
Do you have your backend code and your frontend code in separate directories? For example /test/api and /test/dashboard or whatever. If you keep your code separate, you can tell istanbul to report on each at a time like so:
istanbul cover _mocha test/api/**/*.js
Makes sense? Would that work for you at all?
Let me know.
I also faced a similar situation and hence thought it's worth sharing if it could help someone though its an old post. Istanbul takes the current directory (.) as a coverage directory while running the command. In order to include just a specific directory to coverage scope use "--root /dir/" option. This would generate coverage report only for the files in that directory.
Related
I made a React app using Create-React-App. I have a testing script in my React app's package.json like this:
"test": "node -r #babel/register -r #babel/polyfill **/*.test.js | tap-color",
This catches and executes the files in src/ like src/App.test.js, but not header.test.js in src/layout/header/header.test.js. And what if I add even more layers of folders? Is there a regex that will catch all folders in src/ and their subfolders no matter how nested with a file ending of .test.js?
Edit: I found this question on StackOverflow. According to that answer you would have to write:
"test": "node -r #babel/register -r #babel/polyfill 'src/**/*.test.js' | tap-color",
which unfortunately matches nothing for me. I'm on Mac.
The question you refer to is not especially useful for your case. My answer there works because Mocha has been designed to pass the patterns you give to it to the glob library for interpretation. So when you do mocha 'src/app/**/*.tests.js' the quotes prevent the shell from interpreting the pattern, Mocha gets src/app/**/*.tests.js as the first pattern given to it, which it gives to glob to get a list of files to actually run. Your case is different at least one crucial way: glob is not involved so there is nothing that can correctly interpret **.
Your first attempt is consistent with what happens when you are using a shell that does not understand **. It is interpreted exactly the same as *. So the shell interprets **/*.test.js as */*.test.js, expands this pattern and passes the result to node.
In your second attempt, you quote the pattern but that does not help you because node does not do pattern interpretation. It tries to load a file at path src/**/*.test.js, interpreted literally. This is not what you want.
I'm not sure what the compatibility implication with Windows are, but you could replace 'src/**/*.test.js' with $(find src -type f -name '*.test.js'). (See the man page for details.) At run-time, the shell will replace this with the result of the find command.
Or for greater simplicity, and less risk of platform issues creeping up, you could use babel-tap like this:
babel-tap 'src/**/*.test.js' | tap-color
If you use babel-tap, there's actually no need for using find because internally babel-tap calls on facilities that use the glob library to interpret the file names passed to it.
I've focused on the file pattern issue but I'm not seeing how what you're trying to do would work, even without the pattern issue. Consider this command, and assume that the files exist:
node -r #babel/register -r #babel/polyfill src/a.test.js src/b.test.js
This is not telling Node to run src/a.test.js and src/b.test.js. Rather, it tells node "run the script src/a.test.js and pass to it the parameter src/b.test.js". I've not used tap very much but I don't recall it working this way. Using babel-tap like I show above also avoids the problem here.
When using #vue/cli-plugin-unit-jest, I am receiving coverage reports each time I run my unit tests, regardless of whether I have the --coverage flag in the execution line or not. I do not want to receive coverage reports on all of my untested files. When searching for an answer online, there are numerous questions about how to turn that feature on, not turn it off. I can't find it in the documentation either.
How do you disable the Coverage on Untested Files feature in Jest?
Disabling coverage similar to enabling it, just prefix the pattern with an ! like so:
{
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**",
"!**/folder-with-untested-files/**"
]
}
Or disable coverage all together with "collectCoverage": false.
If that does not work, then you have this params overridden somewhere in your code.
"collectCoverage": false
in jest.config.js
You can also suppress coverage from the command line. The package I'm working with provides a test script, and I was able to pass the collectCoverage option in as a flag. The relative path here works because my test runner is called by npm and that should set the working directory to the root of my project:
npm run test -- path/to/your.spec.js --collectCoverage=false
And the other way around, you can specific a single file to collect coverage from. It'll override any broad-ranging glob you may have already defined in your project's test config files. One reminder, you collect coverage from your source file, not your spec file. And one other reminder, you can list pretty much any file you want in that coverage option, so make sure you get it right:
npm run test -- path/to/your.spec.js --collectCoverageFrom=path/to/your/source/file.js
Package.json
testw": "jest --watch --collectCoverage=false"
watches the test files for change
npm command
npm run testw Yourfilename.js
"collectCoverage": false
in package.json, will disable coverage, collection, As mentioned by #Herman you can also put ! before file pattern in value of property collectCoverageFrom in package.json
In my case, in package.json I have this statement collectCoverage:false and still I was getting errors. Then I realized I also have collectCoverageFrom line and I removed it since I did not need it. After removing the below line it worked as a charm.
"collectCoverageFrom": [
...,
...
]
I'm trying to output some test coverage (lcov) reports from istanbul into sonarqube to analyse our test coverage using thoughtworks GO. There is a coverage/html folder being output but sonarqube reports the following error No coverage property. Skip Sensor. Here's my properties file, what am I missing ?
sonar.projectKey=transformers.allspark.ui
sonar.projectName=Transformers Allspark UI
sonar.projectVersion=1.0
sonar.host.url=https://sonarqube-security.test.ctmers.io
sonar.sources=.
sonar.projectBaseDir=.
sonar.language=js
sonar.sourceEncoding=UTF-8
sonar.javascript.lcov.reportPath=coverage/html
Thanks for responding. I've managed to fix this by replacing the last line with this : sonar.javascript.lcov.reportPath=coverage/html/lcov.info. It appears that sonarqube needs a path to a file (lcov.info) and not just the folder. Also my istanbul configuration was wrong because it needed to output lcov and not just a standard report.
I have a +10K lines Backbone Marionette app and we are running tests and coverage through Karma.
I would like to include all the sources so that we can have a better idea of what it is not covered by our tests.
I have been passing the includeAllSources option in the karma configuration but I still don't see karma showing the results for all files (the report only show +3K lines cover, more or less the amount of lines that we know we have test for).
Am I doing something wrong? Is there another way to include all sources?
There use to be a Karma plugin that was able to handle this but the plugin is not longer working (modified to make it run, but the results are still the same).
Is there are way to pass the --include-all-sources option to Istanbul while running it from Karma?
Try this plugin: https://github.com/kopach/karma-sabarivka-reporter. It includes files specified by pattern to coverage statistic. So, you can be sure, that you have all your source files under coverage statistic control.
Install npm install --save-dev karma-sabarivka-reporter
And update karma.conf.js similar to this:
reporters: [
// ...
'sabarivka'
// 'coverage-istanbul' or 'coverage' (reporters order is important for 'coverage' reporter)
// ...
],
coverageReporter: {
include: [
// Specify include pattern(s) first
'src/**/*.(ts|js)',
// Then specify "do not touch" patterns (note `!` sign on the beginning of each statement)
'!src/main.(ts|js)',
'!src/**/*.spec.(ts|js)',
'!src/**/*.module.(ts|js)',
'!src/**/environment*.(ts|js)'
]
},
This github issue seems to be addressing your issue and this pull request seems to fix it in version 0.5.2 of the karma-coverage plugin.
I hope you're using an earlier version and just upgrading solves your problem!
You just need to add includeAllSources: true to your coverageReporter, the Reporter Options.
Like this:
coverageReporter: {
includeAllSources: true
...
}
Hiho,
I've got a problem with my Mocha configuration. I've got ES6 code which should be compiled by Babel and then I want to get coverage (in LCOV format) of this ES6 code.
My approach to this problem was to use mocha, mocha-lcov-reporter, babel and blanket packages. Code structure is:
-- src
----- ...
-- test
----- spec
-------- something.spec.js
-------- ...
----- blanket.js
Where specs are in test/spec directory (matches also *.spec.js pattern) and blanket.js is:
require('blanket')({
pattern: require('path').join(__dirname, '..', 'src')
});
Command which I prepared is:
./node_modules/.bin/mocha $(find test -name '*.spec.js') --recursive --compilers js:babel/register -r test/blanket -R mocha-lcov-reporter
So, it should run Mocha tests for all *.spec.js files, compiling them by Babel and starting test/blanket.js file before.
After starting this command I get Error: Line 1: Unexpected reserved word error from esprima.js. When I run it without requiring test/blanket file it run without problems, but ofc I haven't coverage.
Has anyone tried to do so? Do you have any ideas how to do it?
Okey, problem already resolved, but without Babel (native ES6 instead); I've done it another way. I've used istanbul-harmony and mocha packages. Then the command is:
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(find test -name '*.spec.js') -R spec -u exports