Using Mocha and Chai to test in JavaScript.

ham_codes
3 min readMar 26, 2021

I’ve been looking into testing my code for a little while now. When I looked into all the different ways to do this, I was a taken back by all the different ways to approach this topic. From automated tests, unit tests, manual tests I didn’t know where to start. So my first step was to look into unit tests and try them out. When I looked up information, I was met with Mocha and Chai as options to test my JavaScript code. Here is the start of my quest.

I decided to look on youtube to find a tutorial on testing my JavaScript code and here is what I found about Mocha. According to the official documentation, “Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.” So what does this mean and how can I really get started with all of this? The first thing I did was create a folder called mochatest and open that in my code editor, vscode. I then ran a few commands to get started…

The first thing I did in the terminal was run “npm install mocha chai — save-dev. This would set everything up in my package.json that I would need in order to use Mocha and Chai. Speaking of Chai…what is it? According to the official documentation from Chai themselves “Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.” Now that I had both set up I needed to create some files to begin working.

I started by creating an App.js file in my main directory where I would write some basic functions to test. In this file I wrote the following function…

The next step I took was to create a folder called “test” where I would keep the test file to test this function. I then created a file called appTest.js. I have found that convention is using the same name and adding test for the tests you want to run that way it is easy to find and helpful for other programmers looking at your code. Once inside this appTest.js I needed to write some code that would ensure that the function I wrote in App.js was functioning properly.

Inside of the appTest.js file I first needed to bring in a module called assert which, once again, according to the documentation “The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated.” I then made sure I set a variable for the app by writing “const app = require(‘../app’);”

From here I need to write the test using the following code which would test the sayHello function to see if it was working properly.

When that code is complete I can now go to the terminal and run “npm run test” which will show that we have one test passing. This was just a very basic introduction to testing, but hopefully I will have a lot more information about this topic as I have found it very interesting! Check out some of the links below for more information on how to test your code. Thanks for reading!

--

--