create account

Creating Unit Tests For Your Node.js Projects Using Mocha by gotgame

View this thread on: hive.blogpeakd.comecency.com
· @gotgame ·
$10.55
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

![enter image description here](https://i.ibb.co/7nK3CZz/new.png)

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

![enter image description here](https://i.ibb.co/St1NkPT/new.png)

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://medium.com/serverlessguru/how-to-unit-test-with-nodejs-76967019ba56)
- [https://mochajs.org/](https://mochajs.org/)
- [https://github.com/mochajs/mocha](https://github.com/mochajs/mocha)
- [https://www.npmjs.com/package/mocha](https://www.npmjs.com/package/mocha)

I really hope you found this post useful, drop your comments and opinions below, thanks for reading.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 104 others
properties (23)
authorgotgame
permlinkcreating-unit-tests-for-your-node-js-projects-using-mocha
categoryhive-175254
json_metadata{"tags":["technology","ocd","neoxian","gems","palnet","kurator","curie"],"image":["https://i.ibb.co/7nK3CZz/new.png","https://i.ibb.co/St1NkPT/new.png"],"links":["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"],"app":"hiveblog/0.1","format":"markdown"}
created2020-06-22 07:11:45
last_update2020-06-22 07:11:45
depth0
children6
last_payout2020-06-29 07:11:45
cashout_time1969-12-31 23:59:59
total_payout_value5.284 HBD
curator_payout_value5.265 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,376
author_reputation23,969,707,386,372
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,107,874
net_rshares26,422,437,079,576
author_curate_reward""
vote details (168)
@gitplait-mod1 ·
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](https://hive.blog/trending/hive-103590) and Chat with us on [Discord](https://discord.com/invite/CWCj3rw)
properties (22)
authorgitplait-mod1
permlinkqcbotk
categoryhive-175254
json_metadata{"users":["gitplait"],"links":["https://hive.blog/trending/hive-103590","https://discord.com/invite/CWCj3rw"],"app":"hiveblog/0.1"}
created2020-06-22 10:22:36
last_update2020-06-22 10:22:36
depth1
children1
last_payout2020-06-29 10:22:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length300
author_reputation64,455,719,431
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,110,188
net_rshares0
@gotgame · (edited)
Thank you for the nice words, will be sure to check your community out. And I am glad that you found this post useful
properties (22)
authorgotgame
permlinkqcbvf5
categoryhive-175254
json_metadata{"app":"hiveblog/0.1"}
created2020-06-22 12:45:09
last_update2020-06-22 12:45:54
depth2
children0
last_payout2020-06-29 12:45:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length117
author_reputation23,969,707,386,372
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,112,283
net_rshares0
@hivebuzz ·
Congratulations @gotgame! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@gotgame/upvotes.png?202006241302"></td><td>You distributed more than 55000 upvotes. Your next target is to reach 56000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your board](https://hivebuzz.me/@gotgame) And compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/farewell-lizziesworld"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/MdTLbKWz-cover.png"></a></td><td><a href="/hivebuzz/@hivebuzz/farewell-lizziesworld">The Hive community is in mourning. Farewell @lizziesworld!</a></td></tr></table>

###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22109%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/109)!
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-gotgame-20200624t130553000z
categoryhive-175254
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-06-24 13:05:51
last_update2020-06-24 13:05:51
depth1
children0
last_payout2020-07-01 13:05:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,184
author_reputation369,695,575,484,425
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,151,213
net_rshares0
@project.hope ·
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](https://peakd.com/hive-175254/@project.hope/3-ways-of-joining-our-efforts-project-hope-economy-explained).

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,
properties (22)
authorproject.hope
permlinkqcfh3o
categoryhive-175254
json_metadata{"users":["gotgame","project.hope"],"links":["https://peakd.com/hive-175254/@project.hope/3-ways-of-joining-our-efforts-project-hope-economy-explained","https://discord.gg/uWMJTaW"],"app":"hiveblog/0.1"}
created2020-06-24 11:26:09
last_update2020-06-24 11:26:09
depth1
children2
last_payout2020-07-01 11:26:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length666
author_reputation3,880,876,099,761
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,149,801
net_rshares0
@gotgame · (edited)
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.
👍  
properties (23)
authorgotgame
permlinkqcg86h
categoryhive-175254
json_metadata{"users":["project.hope"],"app":"hiveblog/0.1"}
created2020-06-24 21:11:09
last_update2020-06-24 21:19:42
depth2
children1
last_payout2020-07-01 21:11:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length273
author_reputation23,969,707,386,372
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,158,715
net_rshares12,112,615,191
author_curate_reward""
vote details (1)
@crypto.piotr ·
cyu :)
properties (22)
authorcrypto.piotr
permlinkqch9zw
categoryhive-175254
json_metadata{"app":"hiveblog/0.1"}
created2020-06-25 10:47:51
last_update2020-06-25 10:47:51
depth3
children0
last_payout2020-07-02 10:47:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation27,396,789,428,606
root_title"Creating Unit Tests For Your Node.js Projects Using Mocha"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,168,058
net_rshares0