Simple TDD in NodeJS with 5 Steps

Jeff Simons Decena
3 min readJul 5, 2020

--

You need to start TDD in NodeJS right now! Some asks why? I ask, why not?

I started writing the TDD in Laravel — currently the most famous PHP web framework, with simple instructions on how to start a PHP programmer’s test driven development journey. Now, I would like to share how we can have the same pattern in NodeJs.

In any programming language, test driven development can be really daunting. But, if you put your heart to it and think of the future of the project for both the maintainers and yourself, you would persevere into always put a test in every bit of code you put into your application.

For this tutorial, I will use AdonisJS framework which is very similar to Laravel in code structure. Once installed, run npm install to install the needed libraries.

Set up the test environment

Step 1

You need to set up the TEST environment for AdonisJS for it to work — Set up the test here. Once set up, you can run adonis test to run the default test.

info: serving app on http://127.0.0.1:4000Example
✓ make sure 2 + 2 is 4 (2ms)
PASSEDtotal : 1
passed : 1
time : 6ms

Create your first test case

Step 2

Let us, create our own test. In the example.spec.js , just rename the test name from make sure 2 + 2 is 4 into it can make a user.

With the above code, we just simply imported the User model and created a random data object that we passed on the create method on the User model.

If we run adonis test and we will get:

info: serving app on http://127.0.0.1:4000User unit test
✖ it can make a user (8ms)
ERRORS1. it can make a user
expected 'johndoe' to equal undefined
"johndoe" => undefined
FAILEDtotal : 1
failed : 1
time : 13ms

This is exactly what we want, to FAIL. In every TDD, you want your initial test to fail since you have not done any implementations. If you run the test command and it passes the first time, you are doing it WRONG.

Debug what is going on

Step 3

What could be the error? If we console.log() the user constant, it will show:

We need to install the sqlite3 because by default, AdonisJS is persisting the data in sqlite when the DB_CONNECTION in the ENV is not set. Check the database.js config below.

Now, run npm install sqlite3 --save . Once installed and saved, run the test command again: adonis test

Further debugging what’s going on

Step 4

It would throw an error again, why? because we have not created a migration file to create the schema for our users table.

This is PERFECTLY good! If you get this error, you are in the right track!

If you look at the database/migrations directory, we already have the user migration file but it still throwing an error. Why? This is because the test runner is not running our migration files. To do this, we need to modify our vowfile.js

We have to un-comment the ace , the call to migration run and migration reset. Once you have done that, you can run again the adonis test command.

Hoping there is light at the end of the tunnel

Step 5

As you can see, it is now PASSING! Pat yourself at the back mate, you have DONE it!

Parting words. NodeJs is an async programming language and it is so fast that it throws the ERROR first before it would RUN your actual code. :)

Next tutorial would be the REPOSITORY design pattern in NodeJS. So keep reading!

Happy programming!

PS.

The plan here would be: testing, create repositories, running automated test, creating docker image, deploy the application. :)

--

--

Jeff Simons Decena

Senior Fullstack Engineer, React Native developer and HL7 Health Informatics professional