Creating Unit Tests For Your Node.js Projects Using Mocha
While working on any JavaScript project, it is important to write tests for your project features/components as much as it is important to create the project features themselves.
Unit testing in Node.js is a process where by the developer in charge of a project creates a module that helps to test whether a feature that was added in the project was working as it should be.
Most of the time, unit tests are usually performed on individual functions of a feature or project and each function/feature is referred to as a unit.
Node.js provides an easy way for developers to create unit tests for their project features through a very effective package known as Mocha.
Mocha.js is a wonderful JavaScript test framework with wonderful features that helps to simplify the process of unit testing.
Using Mocha
In order to use Mocha to create unit tests in your projects, you need to install the package.
To install Mocha, run this command in your project terminal
npm install mocha
Moving forward, we are going to create a new directory in the root directory of our project and name it test
.
In the test
directory, create a file new.test.js
.
Now, we are going to test a function in the file to show how Mocha works.
We are going to use a npm package to run a test that checks whether the square root of any passed value is returning the correct data.
Let;s install the package by running the command
npm install mathjs
in new.test.js
add the following code
const { sqrt } = require('mathjs')
const assert = require('assert');
describe('Square root Test', () => {
it('should return 2', () => {
assert.equal(sqrt(4).toString(), 2);
});
it('should return 3', () => {
assert.equal(sqrt(9).toString(), 3);
});
});
In package.json
, add this in your scripts
object
"test": "mocha"
In your terminal, run the command npm run test
in order to run the test we created
If the function we tested in returning the correct data, we should see the following result in the terminal log
If the function had failed, which it would have had we passed the wrong data such as in the example below
const { sqrt } = require('mathjs')
const assert = require('assert');
describe('Square root Test', () => {
it('should return 3', () => {
assert.equal(sqrt(4).toString(), 3);
});
it('should return 12', () => {
assert.equal(sqrt(9).toString(), 12);
});
});
Had we run the code above we would get a result similar to this
There are numerous ways in which one can use Mocha to conduct unit tests on ones project but I am unable to cover the full scope in this post.
If you are interested in implementing Mocha in your projects you can refer to the following resources to understand the nitty-gritties of the package
- https://medium.com/serverlessguru/how-to-unit-test-with-nodejs-76967019ba56
- https://mochajs.org/
- https://github.com/mochajs/mocha
- https://www.npmjs.com/package/mocha
I really hope you found this post useful, drop your comments and opinions below, thanks for reading.
Thanks for illustrating out all this process. This is the kind of post we appreciate in our community.
Your post has been curated with @gitplait community account.
Join our Community on Hive and Chat with us on Discord
Thank you for the nice words, will be sure to check your community out. And I am glad that you found this post useful
Hello @gotgame
Thank you for posting within our hive.
Please spare few minutes and read how project.hope is organized and learn about our economy.
That would help you understand more our goals and how are we trying to achieve them. Hopefully you will join our community and become strong part of it :)
Do you use telegram or discord? If you do then join our server and give me a shout. I would gladly share with you goals of our community and introduce to others from our team.
Our discord server: https://discord.gg/uWMJTaW
Yours,
@project.hope team,
Hello @project.hope, thanks for dropping by. I am aware of how @project.hope economy works. I have been following for some time now. Would join the server and say hello soon.
Nice to read from you, thanks for all that you do, I am very glad to be a part of this community.
cyu :)
Congratulations @gotgame! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :
You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @hivebuzz:
Support the HiveBuzz project. Vote for our proposal!