Unit Test

Unit Testing can seem too much work, or too tedious, because is writing several test for each Unit (component) of the project, but this is very important work, here is an example of other people doing unit testing

Here we have two pilots doing the check list before the fly, and they actually go system by system making sure that all of them work, because it only takes one system fail to bring the whole plane down the hard way.

Imagine what would happen if one pilot never check the systems and the plane goes down, if the pilot survives, would you hire that pilot?

The same logic can be use for Web-Applications, and its developers, would you hire a developer who’s websites keep going down?


To make sure that our projects stay up the cloud, we test it component by component before we deploy to the cloud.

Now that we agree that this Component testing is important lest learn how to do it.

In unit testing we write the code to test a unit before we write the code for the unit.

— The code for this post is on GitHub: LINK

in the following example we will create a unit that will add two numbers.

First we will create the project place and the test code, and then we will write the unit code.

Here are the Terminal Commands to create the project and load the Mocha test framework.

After those changes your file: package.json shold look like

Now time to write a basic test.

In the file test.js write the following test.

Then we go to the terminal and type the following command

This means that we successfully run our tests and 0 of the tests pass, 1 fail, the next step is to create the code that will make this test pass, so in the file: math.js write the following code

Save all the changes and now lest go to the terminal and runt the test again.

Congratulations the test pass, now lest write another test for the same unit in the same file: test.js, in this new test when given the numbers 5 & 6 the unit should return 11, but it should do this without failing the previous test. so here is how the files test.js & math.js will be modify.

After we modify this files then we run the test in the terminal:

Congratulations the two test pass, now the next step would be to write as many tests as you think.

in the last test we are using the expect method.

Copyright 2017. All rights reserved.

Posted December 23, 2017 by Edmundo in category "TDD