Blog

Explaining Test Driven Development (TDD) Part 3: A Real Example Using NodeJS

By Gorilla Logic

Blog
Favorite
Technical

Now that you understand what TDD is and how the TDD cycle works, here, in part 3, I will outline an example of TDD using NodeJS. I will begin by setting up the dependencies, then creating the project and, finally, I will show you actual code using NodeJS.

Set up dependencies

It is time to apply the concept of TDD in an app using Node JS. For the purposes of this exercise, let’s use the same calculator example outlined in part 2. We are going to use the following technologies:

  • Mocha as a test runner
  • Chai as an assertion library

 

This example makes the following assumptions:

  • NodeJs installed, refer to this link if you do not have it.
  • Knowledge about testing (Pretty basic, you do not need to be an expert). Specifically, those concepts I outlined in part 1 and part 2 in my previous posts. 
  • Basic knowledge about NodeJs

 

To begin, the first step is to install Mocha. Mocha is a test framework used in NodeJs projects. This step is really easy. You simply run an npm command in the terminal:

$ npm install -g mocha

Now, we need Chai in our project. Chai works as our assertion library. To install Chai, run in the terminal:

$ npm install chai —save–dev

(In this last step, we are installing development dependencies. That is the reason for the parameter dev) 

Creating the NodeJS project

For the purposes of this example, we are going to work with an ExpressJs project. We can use Express Generator in order to create a base project. Let’s run the following in the terminal:

$ express tdd_test

 

Then install dependencies:

$ npm install

 

The first thing you can do in order to facilitate the process is to modify your package.json. Let’s modify the “test” script. What you need now is to find the section of scripts and add the following:

 

“scripts”: {
 “start”: “node ./bin/www”,
 “test”: “mocha test”
},

 

Now, each time that we use the command npm test, it will automatically call mocha.

Coding…

You need to create a test folder in the root of the project. By default, mocha looks inside a “test” folder for test classes. Also, you can point the route of your tests, but for the purposes of this example, let’s use the default one. Inside that folder, let’s create a file called “index.test.js”. This will work as primary class for our test.

Now that we have all the structure ready, let’s apply the cycle of TDD that you learned about in part 1 and part 2 of my posts:

1. Write a test for a desired feature.

In the folder called test, create a subfolder called “controllers”. Here, we will store all of our controller tests. Then, create a file called “controller.test.js”. Remember, as I stated above, our purpose is to create a test that evaluates an “add” function. Therefore, we should create all the logic for that test. If you remember from my previous post, the logic is as follows: “Given two numbers, it should return the correct result for that operation”.

 

Explaining Test Driven Development Part 3: A Real Example Using NodeJS

Basically, we are importing our test library (chai) and then the module of the feature that we will create soon in the next step. Then, in the test, we are passing two numbers to our “add” function and we expect that the result will be “3”. So far, we have not created any code for our feature, just the test.

 

2. Run that test and watch how it fails!

So far we have not created any code for our feature, just the test. If we run the command to test:

$ npm test

It definitely will fail, showing something like this in the console:

Explaing TDD Part 3: A Real Example Using Node JS

 

Do not be afraid, it is totally normal! The error is telling us that “addOp” is not a function, because we have not created the function yet.

 

3. Implement the feature (Just the structure, no logic yet).

Here we need to create the declaration of the function, not more, just the header declaration. In order to do this, let’s create a folder called “controller” in our root folder. Then, create a file called “operations.js” and declare the function, like this:

Explaing TDD Part 3: A Real Example Using Node JS

We have declared just the header, not logic yet. That means that now we are able to import that function and pass it two variables, but it will not do anything because there is no logic declared.

 

4. Run the test and watch how it fails again!

Let’s run the test command again:

$ npm test

And watch how it fails! You should be able to see something like this in the console:

Test Driven Development (TDD): An Example Using NodeJS

 

5. Implement the logic, syntax, etc.

In this step, we are going to complete our function by adding the required logic.

According to the requirement, we have to receive two numbers, complete the operation and return a result. It is that simple. Let’s create the code:

Test Driven Development (TDD): An Example Using NodeJS

6. Finally, watch the test PASS!

At this point, just relax and execute the test command again:

$ npm test

Test Driven Development (TDD): An Example Using NodeJS

Enjoy watching how the test passes!

 

Now that we have our function created with all the appropriate tests (this case, just one), we are done. If necessary, to create a new function (substrate for example) we will have to follow the entire cycle again. It is true that this process takes more time in our development cycle, but at the end of the process, TDD should ensure that most functionalities are working well. 

I hope you enjoyed this series on Test Driven Development. Subscribe to our blog and stay tuned for more tutorials in the future. If you have questions, please leave a comment below and check out another example here in my personal github.

At Gorilla Logic we hire only the best software engineers. Think you have what it takes to be a Gorilla? Check out our careers section and follow us on facebook by clicking below!

Explore More Resources

The Code to Success: Women's Essential Role in Technology The Code to Success: Women's Essential Role in Technology
The Power of Emotional Design: Bringing Products from Good to Great The Power of Emotional Design: Bringing Products from Good to Great
An introduction to the Electron framework An introduction to the Electron framework

Ready to be Unstoppable? Partner with Gorilla Logic, and you can be.

TALK TO OUR SALES TEAM

代做工资流水公司汕头查询入职流水邢台贷款工资流水 代做桂林代开企业对公流水荆州银行流水单代办厦门贷款银行流水模板泰安在职证明价格莆田代做收入证明柳州转账银行流水价格沈阳房贷收入证明打印曲靖办薪资流水单泉州制作银行流水账单铜陵开企业对公流水佛山查询工资代付流水商丘代做车贷工资流水重庆查公司银行流水无锡房贷工资流水 报价株洲薪资流水单开具大庆代做入职银行流水南宁代开车贷银行流水宜春打自存银行流水扬州工资流水单制作哈尔滨房贷银行流水临沂企业对私流水制作深圳工资流水代办台州银行流水修改代开汕头入职银行流水公司企业贷流水公司南昌开工资流水单潮州代做签证银行流水柳州贷款流水公司香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化