I have tried this answer to get coverage for a single component but it is not working. The test runs only for that component but no coverage?
Is there some other configuration that needs to be done anywhere?
I have tried:
npm test src/components/component1/my-component.test.tsx --coverage --collectCoverageFrom=src/components/component1/my-component.test.tsx
Anything I am doing wrong?
The collectCoverageFrom takes in glob patterns as argument, so you need adjust that path string with "".
I have successfully run this command with code coverage from one file with
node_modules/.bin/jest ComponentTest.test.ts --coverage --collectCoverageFrom="path/Component.tsx"
It also it may be that your "test" cmd you are running from package.json is obscuring the additional arguments, try directly calling jest from modules to see if thats the case.
This worked to me with create-react-app from windows cmd:
npx react-scripts test src/components/common/__test__/Dropdown.test.tsx --coverage --collectCoverageFrom=src/components/common/Dropdown.tsx
I'm designing my tests using the Behavior Driven Development (BDD) approach using Gherkin syntax and running my tests with Cucumber JS.
I'm using Cucumber Studio to share reports and keep synced with my business stakeholders, and management.
Recently I needed to get test coverage reports for the project, and made some research but couldn't decide which library to use to get coverage reports and how.
So far I've found JSCover, Cucumber Reports, and Istanbul for test coverage reports, but I'm not sure how to use them exactly and which would be best for my case to use with Cucumber JS.
After several trials, I've figured out that it is pretty simple to use Istanbul JS to see code coverage.
I've followed the instructions on the website and install Istanbul's JavaScript library nyc using:
yarn add -D nyc
Then, I've updated my scripts in the package.json as the following:
...
"scripts": {
"test": "cucumber-js ...",
...
"coverage": "nyc yarn test"
},
...
And when I run yarn coverage it runs the tests with wrapping by nyc and creating a coverage report as the following:
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
I'm working on an existing project but I am new to Javascript. The project can be found here, it is JsPsych and I've searched the documentation here and cannot find the answer (http://docs.jspsych.org/)
I don't understand how they are doing unit tests. I have found the "testing" folder and you can view it below, here.
JsPsych test folder
Here's an example called "load.test.js"
const root = '../';
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-single-stim.js');
test('jsPsych should be in the window object', function(){
expect(typeof window.jsPsych).not.toBe('undefined');
});
Does anyone recognize this type of unit test? Is this supposed to be paired with some software? Can someone explain how I would actually run this?
The tests are run with Jest. You can see that jest is called as the test command in package.json.
To run the tests, install node and npm on your system. Then run npm install in the repository's main directory. Then you can run npm test.
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.