Jest ---outputFile removes CLI coverage output - javascript

We use jest for unit testing in our repo, and include config for code coverage:
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts', 'infrastructure/**/*.ts'],
Normally this results in CLI output for coverage when running the tests. Recently however we've added Danger JS integration on our PR build, and the plugin we're using to report on Jest tests relies on the --outputFile switch for the jest command i.e. jest --outputFile test-results.json --json.
With --outputFile specified code coverage is still run and results are put in the coverage directory, but the CLI output doesn't include coverage, only pass / fail information. Is it possible to have both the outputFile and coverage to stdout?

Related

Mocha - Chai Unit Terst report generation - NodeJS

I am doing Unit Testing using Mocha chai for sample NodeJS project. Followed by the below reference.
https://www.sitepoint.com/unit-test-javascript-mocha-chai/
And I could run the tests successfully using terminal. It is showing success/failure test status.
But, Test Report is not generated.
I used the below command in terminal to run the test
npm run test
How do I generate the report for the test.
Try nyc (https://www.npmjs.com/package/nyc). Install as a dependency along with mocha and if your tests pass, this will generate the coverage report for you like a breeze.
I personally prefer using jest (https://www.npmjs.com/package/jest) instead of mocha/chai+nyc, it comes pre-equipped with reporting feature and pretty simple to use as well but it's your choice that matters here.
You can have a look a similar question for more reading about nyc usage: Code coverage with Mocha

How to get jest coverage only for changed files?

I could not find my requirement for coverage in the jest docs. I have tried the following options but could not find the required solution to get jest coverage only for changed code.
npm test -- --coverage --onlyChanged
This runs only changed tests but shows coverage for full suite.
npm test -- --coverage --changedSince=base-branch
This runs all tests and shows coverage for full suite.
Found this discussion and it seems this issue is fixed. I am not
sure why this is not working though?
Jest supports this out of the box.
jest --coverage --changedSince=master --coverageThreshold='{"global":{"statements":"50","branches":"50","functions":"50","lines":"50"}}'
The above command will only calculate the coverage for the code which was changed as compared to your master branch.
For this changed code you can also set the threshold coverage.

How can I use Mocha without removing Ava?

One of my co-workers added this Ava package to our setup, and it's done something I've never seen a Node package do before: interfere with other packages! Now when I try to run Mocha I get:
$ node_modules/mocha/bin/mocha test/
Test files must be run with the AVA CLI:
$ ava node_modules/mocha/bin/_mocha
I get that Ava would like to run my Mocha tests, but if I wanted that I'd run ava mocha not mocha. And because a co-worker is using it I can't simply uninstall the package.
Is there any way I can run plain Mocha tests on a machine with Ava installed?
One of the files in test/ imports ava and the imported code will recognise that it's not being run with the correct tooling and throw an error.
Might be worth subdividing your test/ directory to keep tests associated with their respective runners.
test/
ava/
SomeAvaTests.js
mocha/
SomeMochaTests.js
This way you can safely run mocha test/mocha/ and vice versa without worrying about treading on each other's toes.

How to combine mocha tests with mocha-phantomjs tests

Test cases:
server/
appTest.js
client/
clientTest.js
clientBundle.js
clientTest.html
All are mocha tests. For client tests, I use sinon, and webpack them to generate a bundle - clientBundle.js and run it using mocha-phantomjs for headless testing.
Now I have defined npm test as mocha test/server && mocha-phantomjs test/clientTest.html. It gives me results for both of them separately. How can I combine them into one - say mocha test/all.js.
Tried require()s inside test/all.js. mocha-phantomjs cannot be used as a library inside node. It can only be used as a CLI.

How to get the code coverage report using Jest?

Is there a way to have code coverage in the JavaScript Jest testing framework, which is built on top of Jasmine?
The internal framework does not print out the code coverage it gets. I've also tried using Istanbul, blanket, and JSCover, but none of them work.
When using Jest 21.2.1, I can see code coverage at the command line and create a coverage directory by passing --coverage to the Jest script. Below are some examples:
I tend to install Jest locally, in which case the command might look like this:
npx jest --coverage
I assume (though haven't confirmed), that this would also work if I installed Jest globally:
jest --coverage
The very sparse docs are here
When I navigated into the coverage/lcov-report directory I found an index.html file that could be loaded into a browser. It included the information printed at the command line, plus additional information and some graphical output.
UPDATE: 7/20/2018 - Added links and updated name for coverageReporters.
UPDATE: 8/14/2017 - This answer is totally outdated. Just look at the Jest docs now. They have official support and documentation about how to do this.
#hankhsiao has got a forked repo where Istanbul is working with Jest. Add this to your dev dependencies
"devDependencies": {
"jest-cli": "git://github.com/hankhsiao/jest.git"
}
Also make sure coverage is enabled in your package.json jest entry and you can also specify formats you want. (The html is pretty bad ass).
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"],
}
See Jest documentation for coverageReporters (default is ["json", "lcov", "text"])
Or add --coverage when you invoke jest.
Jan 2019: Jest version 23.6
For anyone looking into this question recently especially if testing using npm or yarn directly
Currently, you don't have to change the configuration options
As per Jest official website, you can do the following to generate coverage reports:
1- For npm:
You must put -- before passing the --coverage argument of Jest
npm test -- --coverage
if you try invoking the --coverage directly without the -- it won't work
2- For yarn:
You can pass the --coverage argument of jest directly
yarn test --coverage
This works for me:
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"]
},
"scripts": {
"test": "jest --coverage"
},
Run:
yarn/npm test
You can run npx jest --coverage -- path/to/your/file.spec.js
that will show coverage for affected files
If you want to view this in browser you can do as follows,
Go to Browser and CMD+O.
Navigate to your repo and search for coverage/lcov-report/index.html
Then you can visually see all the coverage areas.
You can also refer to this link below, for more information
https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j
Check the latest Jest (v 0.22): https://github.com/facebook/jest
The Facebook team adds the Istanbul code coverage output as part of the coverage report and you can use it directly.
After executing Jest, you can get a coverage report in the console and under the root folder set by Jest, you will find the coverage report in JSON and HTML format.
FYI, if you install from npm, you might not get the latest version; so try the GitHub first and make sure the coverage is what you need.
If you are having trouble with --coverage not working it may also be due to having coverageReporters enabled without 'text' or 'text-summary' being added.
From the docs: "Note: Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output." Source
Configure your package.json file
"test": "jest --coverage",
Now run:
yarn test
All the test will start running and you will get the report.
I had the same issue and I fixed it as below.
install yarn npm install --save-dev yarn
install jest-cli npm install --save-dev jest-cli
add this to the package.json "jest-coverage": "yarn run jest -- --coverage"
After you write the tests, run the command npm run jest-coverage. This will create a coverage folder in the root directory. /coverage/icov-report/index.html has the HTML view of the code coverage.

Categories

Resources