How Test-Driven Development Benefits Your Team

Ardian Ghifari
5 min readFeb 25, 2020

--

If pain is nature’s way of saying “Stop!”, fear is nature’s way of saying “Be careful.” Being careful is good, but fear has a host of other effects: makes you tentative, makes you want to communicate less, makes you shy from feedback, makes you grumpy. As Kent Beck said in his book Test-Driven Development: By Example, test-driven development is a way of managing fear during programming.

There are two simple rules in TDD, write new code only if you first have a failing automated test and eliminate duplication. Those rules result in an order to the tasks of programming, Red, Green, and Refactor. Red means you should write a test that doesn’t work, or even doesn’t compile at first. Green means you should make the test work quickly, improving whatever necessary in the process. Refactor means you should eliminate all the duplication created in just getting the test to work. From what I conclude, there are two benefits of TDD: keeps you focused and easier refactoring.

I’m going to take an example from the book that I mentioned earlier. Let’s say you want to make a simple multiplication function for a currency. You will need a class of currency and a multiplication function inside it. Based on the requirements, the test function should look like this:

The function above won’t even compile, but that’s okay. That’s the point of Red step in TDD. You can start by fixing the code to get Green. By having an objective to pass the test, you will focus to just do the least things necessary, therefore you will focus on the requirement.

I get the chart above from page 89 of the book. It clearly shows that every refactoring need less number of changes. From the data I provided above, we can conclude that TDD leads to more focused development and cleaner refactoring

Implementing TDD in NodeJS with Jest

In the last two sprints, I have implemented TDD in my tasks in the project. I made several unit tests for the database. From my experience, looking for an easy explanation and example of Jest implementation is not easy, especially when dealing with other frameworks like Sequelize (for the database). So, let’s get into the steps.

First, make sure that you have already installed NodeJS, since most of the commands require npm (Node Package Manager, a command in terminal to access NodeJS open-source module). If you haven’t installed, you can download here.

Second, create a gitlab/github repository and store a directory for the test files. It is highly recommended that you use Visual Studio Code to write the code, since it has a good interface and many helpful features.

Third, create your test file. It must have extension like one of these:
.test.js
.spec.js

Since I got the tasks to build model files for the database, I will give you examples in testing Sequelize database

I add the lines above into my test file. App variable exports the directory that contains the files that I’m going to test. Request variable is used for routing test. Db variable exports the database that I created in another directory. The next lines create individual models to test each one of them. It’s okay if you haven’t create the files that you export or mock in the test files, because it is the purpose of TDD, so you will code only the least necessary to pass the test

The function above needs to be added if you use sequelize database. It will make sure that we are in the testing database (that you have to create after creating the test)

Suppose you want to make a human model. It requires two attributes, name and birth date. You can make it like this

First, I describe the name of the test. In the code above, I name it “Human unit test”. Second, I create a human mock data and store it in human_data. Third, I create function to check if the mock data match with the row in the database that I will fill with the mock data. Fourth, I create a unit test to test the create feature. In the unit test, I call the previous function to check if the create function really store the data in the parameter. Fifth, I create a unit test to check if the data that I stored before can be accessed using an attribute and it is not null.

After we write the unit tests for the database, it is important to delete all records in the testing database since the test will be used repeatedly for refactoring. However, you must pay attention to the tables that have constraints, such as foreign key constraint, because such table is referenced by another table. In the end, we close the database connection using the close function above

You can run your tests using command
npm test

You can run specific test using these commands
## Specific file
npm test path/to/file.test.js
## Specific describe
npm test -- -t '<describeString>'
## Specific describe and it
npm test -- -t '<describeString> <itString>'

You can run test watch mode (continuously testing) using command
npm run test:watch

You can show the coverage using command
npm test -- --coverage

I hope my article can help you understand TDD implementation using Jest. Thank you for reading my article.

Reference:

BECK, K., Test-Driven Development: By Example. Boston: Addison-Wesley, 2002.
My team’s gitlab repository readme file

--

--

Ardian Ghifari
Ardian Ghifari

No responses yet