![]() |
VOOZH | about |
Mocha is a testing framework for Javascript running on Node.js. The frameworks make it easier to test asynchronous Javascript concepts in the browser. Mocha is widely used for testing Javascript codes before deploying them onto the server.
Installation of the module:
npm install mocha
Hooks in Mocha: There are namely six hooks used in the testing by this framework to set up or load up preconditions used in the testing.
Mocha Structure:
Example: Create a test directory in your project. Inside the test directory create two files named helper.js and create_test.js respectively.
Project Directory: The project directory should look something like this:
Now inside the package.json file change the "test" field to "mocha" since the testing framework is mocha.
The helper.js file contains the before() function which is to be executed before all the test cases.
Filename: helper.js
The create_test.js file contains all the test cases that we are going to check using the framework. It contains a describe() function which includes all the test cases defined by it() function.
Filename: create_test.js
Steps to run the code:
npm test
Output:
Need for Mocha Once the code is deployed on the server side, it is very difficult and costly to make any changes. User satisfaction is very important and therefore the code should go through vigorous testing before it is production-ready.