Testing with Mocha
Mocha is a popular open source JavaScript test framework for both Node.js and the browser. Mocha comes with many features.
In this recipe, we're going to write test cases using Mocha for a simple calculator program.
Getting ready
- Let's first create a directory to work in and initialize our project directory:
$ mkdir testing-with-mocha $ cd testing-with-mocha $ npm init --yes
- We need to have a program that we can test. We'll create a small calculator program that we can use to learn how to unit test with Mocha. Create a file named
calculator.js
:$ touch calculator.js
- Now, we can add the following to
calculator.js
for our calculator program:module.exports.add = (number1, number2) => { return number1 + number2; }; module.exports.subtract = (number1, number2) => { return number1 - number2; }; module.exports.multiply = (number1, number2) => { return number1...