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
Related
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 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.
I've been starting to use create-react-app for my React projects, and as a testing library it comes with Jest.
As part of my React apps, I like to create integration tests (as well as unit tests) as I find it useful to be able to check the happy paths in the app are working as expected. For example: Render (mount) a page using Enzyme, but only mock out the http calls (using Sinon) so that the full React/Redux flow is exercised at once.
Before using create-react-app, I've used Mocha for testing, and I found mocha-steps to be a great extension for integration tests, as it allows tests in a group to be executed in sequence, and handles stopping if a step fails without stopping the entire test run.
Question: Is there any way to get Jest to behave in a similar way? Specifically, I'd like to be able to specify a series of tests in a group (such as in a describe) and have them execute sequentially in order.
I've been looking through the Jest docs, or for any other libraries that extend it, but I've come up empty. For now, it feels like the only option is to have one large test which makes me sad, and I'd prefer not to swap out Jest for Mocha if I can avoid it.
Thanks!
jest does not currently support flagging specific tests to be run serially (like ava's test.serial).
This has been requested before but I don't think they are working on it.
The workaround is for you to identify which test files are to be run concurrently and which ones are to be run serially.
For example, I usually name my unit tests with *.test.js and my integration tests with *.spec.js.
I can then run my unit tests concurrently.
$ jest '(/__tests__/.*\\.test)\\.js$'
And I can run my integration tests serially.
$ jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand
I can combine the two in my package.json.
"scripts": {
"unit": "jest '(/__tests__/.*\\.test)\\.js$'",
"integration": "jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand",
"test": "npm run unit && npm run integration"
}
Running npm test can then run concurrent tests first and then the serial ones next.
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.
I want to run the tests related to this project on github, see folder test on https://github.com/node-opcua/node-opcua . Unfortunately I have no idea which testing framework was used and how to run all the tests?
First, run npm install
This will install the required dependencies.
You can then use
npm test
See here:
https://github.com/node-opcua/node-opcua/blob/master/package.json#L15
That will run the tests using Mocha.