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://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.
author | gotgame |
---|---|
permlink | creating-unit-tests-for-your-node-js-projects-using-mocha |
category | hive-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"} |
created | 2020-06-22 07:11:45 |
last_update | 2020-06-22 07:11:45 |
depth | 0 |
children | 6 |
last_payout | 2020-06-29 07:11:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 5.284 HBD |
curator_payout_value | 5.265 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,376 |
author_reputation | 23,969,707,386,372 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,107,874 |
net_rshares | 26,422,437,079,576 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
xpilar | 0 | 1,280,980,043,337 | 50% | ||
alinix | 0 | 143,215,312 | 3.85% | ||
develcuy | 0 | 5,528,112,544 | 30% | ||
chesatochi | 0 | 270,614,748,748 | 50% | ||
elizacheng | 0 | 16,792,741,598 | 9% | ||
gbenga | 0 | 7,322,599,151 | 30% | ||
khussan | 0 | 873,019,092 | 37% | ||
jagged | 0 | 65,996,617,463 | 25% | ||
samminator | 0 | 81,469,828,274 | 70% | ||
marpa | 0 | 649,588,612 | 7.5% | ||
btcsam | 0 | 12,327,412,669 | 100% | ||
pixelfan | 0 | 12,049,411,432 | 2% | ||
tykee | 0 | 2,053,012,155 | 50% | ||
tfame3865 | 0 | 33,246,833,944 | 50% | ||
qsyal | 0 | 869,872,655 | 50% | ||
syedumair | 0 | 93,603,828,304 | 10% | ||
nainaztengra | 0 | 52,682,905,061 | 7% | ||
devcoin | 0 | 6,246,741,959 | 30% | ||
kimzwarch | 0 | 8,959,477,233 | 4% | ||
bboyady | 0 | 6,648,317,861 | 25% | ||
vlemon | 0 | 215,951,710,649 | 7% | ||
veenang | 0 | 3,400,614,797 | 7.5% | ||
davidke20 | 0 | 79,536,998,030 | 50% | ||
carrieallen | 0 | 24,465,382,599 | 30% | ||
sawcraz.art | 0 | 768,949,441 | 30% | ||
takeru255 | 0 | 591,865,949 | 30% | ||
majes.tytyty | 0 | 341,488,012,777 | 30% | ||
reveur | 0 | 2,516,118,653 | 15% | ||
jlsplatts | 0 | 3,698,907,429 | 0.6% | ||
vishalsingh4997 | 0 | 2,577,126,498 | 50% | ||
harley-hot | 0 | 979,324,188 | 25% | ||
doifeellucky | 0 | 325,956,875,998 | 100% | ||
josediccus | 0 | 45,270,520,819 | 10% | ||
faisalamin | 0 | 58,245,579 | 3.85% | ||
lifecruiser | 0 | 11,253,723,703 | 50% | ||
alvinauh | 0 | 48,260,646,725 | 50% | ||
mtl1979 | 0 | 1,544,994,173 | 50% | ||
tcpaikano | 0 | 5,116,246,281 | 40% | ||
drmincu | 0 | 1,046,539,104 | 3.85% | ||
iptrucs | 0 | 39,176,024,954 | 25% | ||
mikitaly | 0 | 23,817,447,917 | 100% | ||
guruvaj | 0 | 14,336,220,840 | 18% | ||
fourfourfun | 0 | 1,987,905,757 | 7.5% | ||
fortunex | 0 | 12,538,482,302 | 50% | ||
adityajainxds | 0 | 1,760,365,456 | 3.33% | ||
mervin-gil | 0 | 630,825,134 | 30% | ||
gandhibaba | 0 | 33,835,673,579 | 10% | ||
bala41288 | 0 | 47,830,704,957 | 20% | ||
wilhb81 | 0 | 90,022,842,812 | 25% | ||
rbalzan79 | 0 | 23,321,224,727 | 80% | ||
lanzjoseg | 0 | 139,722,266,408 | 100% | ||
iamowomizz | 0 | 859,450,113 | 100% | ||
happy-soul | 0 | 227,393,151,424 | 25% | ||
jadams2k18 | 0 | 31,985,495,795 | 100% | ||
pizzapai | 0 | 8,972,687,651 | 50% | ||
yurilaya | 0 | 1,735,837,263 | 50% | ||
arrozymangophoto | 0 | 63,662,709,919 | 15% | ||
adenijiadeshina | 0 | 9,228,560,880 | 50% | ||
zoniacb | 0 | 2,084,247,248 | 100% | ||
retinox | 0 | 3,094,937,195 | 18.23% | ||
wisejg | 0 | 944,269,532 | 30% | ||
peerzadazeeshan | 0 | 9,498,668,820 | 22.5% | ||
josevas217 | 0 | 48,210,859,107 | 50% | ||
phbr | 0 | 1,663,184,921 | 50% | ||
paragism | 0 | 3,705,610,331 | 15% | ||
therising | 0 | 7,876,944,801,931 | 50% | ||
mariusfebruary | 0 | 1,057,808,422,544 | 50% | ||
ritxi | 0 | 59,279,131,959 | 50% | ||
crypto.piotr | 0 | 461,572,233,833 | 37% | ||
minerthreat | 0 | 2,953,965,344 | 15% | ||
aro.steem | 0 | 522,877,336 | 3.85% | ||
akdx | 0 | 13,701,517,768 | 25% | ||
archisteem | 0 | 2,513,129,831 | 7.5% | ||
truthtrader | 0 | 5,432,571,634 | 50% | ||
tysler | 0 | 118,490,275,610 | 95% | ||
dses | 0 | 23,648,857,917 | 60% | ||
karamyog | 0 | 3,602,263,782 | 100% | ||
juanmolina | 0 | 70,958,141,621 | 100% | ||
bigpower | 0 | 163,662,032,610 | 50% | ||
cyberspacegod | 0 | 1,181,761,622 | 30% | ||
adeljose | 0 | 4,080,225,736 | 70% | ||
indianapatt | 0 | 583,170,973 | 30% | ||
rakison2 | 0 | 91,080,939,845 | 50% | ||
pinkgirl4 | 0 | 597,579,723 | 15% | ||
erica005 | 0 | 1,023,074,852 | 18% | ||
deathcross | 0 | 31,216,830,202 | 100% | ||
steemitcentral | 0 | 200,055,712,947 | 70% | ||
perazart | 0 | 3,211,612,615 | 30% | ||
oakshieldholding | 0 | 58,458,196 | 50% | ||
laissez-faire | 0 | 48,810,042 | 100% | ||
alokkumar121 | 0 | 8,372,865,132 | 25% | ||
aleestra | 0 | 2,381,097,477 | 15% | ||
anira.art | 0 | 3,961,078,471 | 30% | ||
bichen | 0 | 1,800,321,131 | 100% | ||
sasaadrian | 0 | 12,854,755,748 | 20% | ||
reverseacid | 0 | 6,131,493,549 | 37% | ||
capybaraexchange | 0 | 1,279,974,541 | 30% | ||
cwow2 | 0 | 535,156,677,900 | 37% | ||
atnep111 | 0 | 9,933,768 | 3.85% | ||
abneagro | 0 | 1,311,128,926 | 15% | ||
dashand | 0 | 0 | 0.9% | ||
veronik28dic | 0 | 774,532,410 | 30% | ||
mfarinato | 0 | 320,155,900 | 3.85% | ||
variedades | 0 | 575,486,062 | 3% | ||
reflektor | 0 | 2,392,826,415,594 | 50% | ||
steemegg | 0 | 4,596,713,735 | 12.5% | ||
bippe | 0 | 846,702,988,272 | 50% | ||
hingsten | 0 | 1,125,843,347,491 | 50% | ||
fusion.lover | 0 | 20,154,021,364 | 100% | ||
a-secondchance | 0 | 6,445,461,123 | 50% | ||
limka | 0 | 38,146,223 | 100% | ||
mia-cc | 0 | 6,422,870,233 | 50% | ||
help.venezuela | 0 | 96,690,368,415 | 50% | ||
pedrobrito2004 | 0 | 1,324,899,191 | 3.85% | ||
kryptogames | 0 | 217,110,509,957 | 50% | ||
maximcracker | 0 | 0 | 3.85% | ||
mfblack | 0 | 16,921,315,407 | 100% | ||
dwinf | 0 | 827,426,756 | 100% | ||
helgalubevi | 0 | 3,745,222,109 | 15% | ||
ritch | 0 | 4,347,805,279 | 100% | ||
bitsharesorg | 0 | 584,722,431 | 3.85% | ||
eliezerfloyd | 0 | 1,392,080,687 | 13.5% | ||
omnivori | 0 | 3,276,033,650 | 90% | ||
shimozurdo | 0 | 1,054,788,991 | 7.5% | ||
tomlee | 0 | 3,341,115,735 | 15% | ||
lrcconsult | 0 | 33,201,034,217 | 50% | ||
geohagasconjr | 0 | 642,871,255 | 100% | ||
contestcoin | 0 | 1,079,228,524 | 100% | ||
helengutier2 | 0 | 6,048,986,262 | 7.5% | ||
abh12345.stem | 0 | 483,093,752 | 50% | ||
jesus68 | 0 | 655,637,130 | 7.5% | ||
project.hope | 0 | 5,051,645,077,486 | 30% | ||
crymaze | 0 | 978,945,887 | 25% | ||
crypmace | 0 | 1,170,127,961 | 25% | ||
project-goblin | 0 | 0 | 3.85% | ||
dark-queen | 0 | 1,059,556,627 | 25% | ||
lcrestrepo | 0 | 3,443,600,297 | 15% | ||
steemitasclub | 0 | 3,933,776,023 | 15% | ||
saravm82 | 0 | 1,143,443,464 | 15% | ||
spliddash | 0 | 2,790,119,899 | 15% | ||
urtrailer | 0 | 8,700,234,168 | 3% | ||
omegarojo827 | 0 | 534,555,939 | 15% | ||
blackpassion | 0 | 1,888,476,749 | 50% | ||
writertales | 0 | 700,846,207 | 7.5% | ||
ilidan | 0 | 978,945,954 | 25% | ||
taverngames | 0 | 21,179,435,443 | 50% | ||
emp2-00com | 0 | 3,727,246,706 | 15% | ||
angegar | 0 | 5,833,072,642 | 100% | ||
javb | 0 | 1,068,211,194 | 50% | ||
ph-fund | 0 | 403,440,497,773 | 100% | ||
splatts | 0 | 1,709,780,630 | 3% | ||
elsan.artes | 0 | 2,082,702,232 | 15% | ||
hope.venezuela | 0 | 2,615,656,206 | 100% | ||
awah | 0 | 15,709,402,470 | 50% | ||
aiplusfinance | 0 | 98,199,770 | 3.85% | ||
bilpcoinbpc | 0 | 646,976,517 | 25% | ||
beemengine | 0 | 6,551,072,250 | 3.85% | ||
brage | 0 | 10,702,142,077 | 50% | ||
robertcc | 0 | 546,708,359 | 15% | ||
alebrijes | 0 | 176,476,930,683 | 15% | ||
gitplait | 0 | 68,067,416,928 | 100% | ||
mynima | 0 | 1,221,898,531 | 100% | ||
mellowy3llow | 0 | 37,461,785 | 3.85% | ||
sunsan | 0 | 1,799,974,464 | 15% | ||
cajasocial | 0 | 16,301,532,959 | 100% | ||
beckie96830 | 0 | 1,012,368,323 | 15% | ||
defi.campus | 0 | 57,961,463,248 | 50% | ||
ph-support | 0 | 1,064,203,430,616 | 100% |
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)
author | gitplait-mod1 |
---|---|
permlink | qcbotk |
category | hive-175254 |
json_metadata | {"users":["gitplait"],"links":["https://hive.blog/trending/hive-103590","https://discord.com/invite/CWCj3rw"],"app":"hiveblog/0.1"} |
created | 2020-06-22 10:22:36 |
last_update | 2020-06-22 10:22:36 |
depth | 1 |
children | 1 |
last_payout | 2020-06-29 10:22:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 300 |
author_reputation | 64,455,719,431 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,110,188 |
net_rshares | 0 |
Thank you for the nice words, will be sure to check your community out. And I am glad that you found this post useful
author | gotgame |
---|---|
permlink | qcbvf5 |
category | hive-175254 |
json_metadata | {"app":"hiveblog/0.1"} |
created | 2020-06-22 12:45:09 |
last_update | 2020-06-22 12:45:54 |
depth | 2 |
children | 0 |
last_payout | 2020-06-29 12:45:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 117 |
author_reputation | 23,969,707,386,372 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,112,283 |
net_rshares | 0 |
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)!
author | hivebuzz |
---|---|
permlink | hivebuzz-notify-gotgame-20200624t130553000z |
category | hive-175254 |
json_metadata | {"image":["http://hivebuzz.me/notify.t6.png"]} |
created | 2020-06-24 13:05:51 |
last_update | 2020-06-24 13:05:51 |
depth | 1 |
children | 0 |
last_payout | 2020-07-01 13:05:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,184 |
author_reputation | 369,695,575,484,425 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,151,213 |
net_rshares | 0 |
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,
author | project.hope |
---|---|
permlink | qcfh3o |
category | hive-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"} |
created | 2020-06-24 11:26:09 |
last_update | 2020-06-24 11:26:09 |
depth | 1 |
children | 2 |
last_payout | 2020-07-01 11:26:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 666 |
author_reputation | 3,880,876,099,761 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,149,801 |
net_rshares | 0 |
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.
author | gotgame |
---|---|
permlink | qcg86h |
category | hive-175254 |
json_metadata | {"users":["project.hope"],"app":"hiveblog/0.1"} |
created | 2020-06-24 21:11:09 |
last_update | 2020-06-24 21:19:42 |
depth | 2 |
children | 1 |
last_payout | 2020-07-01 21:11:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 273 |
author_reputation | 23,969,707,386,372 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,158,715 |
net_rshares | 12,112,615,191 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
crypto.piotr | 0 | 12,112,615,191 | 1% |
cyu :)
author | crypto.piotr |
---|---|
permlink | qch9zw |
category | hive-175254 |
json_metadata | {"app":"hiveblog/0.1"} |
created | 2020-06-25 10:47:51 |
last_update | 2020-06-25 10:47:51 |
depth | 3 |
children | 0 |
last_payout | 2020-07-02 10:47:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 6 |
author_reputation | 27,396,789,428,606 |
root_title | "Creating Unit Tests For Your Node.js Projects Using Mocha" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,168,058 |
net_rshares | 0 |