One of the most tedious in tasks when building an application is writing unit tests.  In fact, most programmers immediately frown when this word is even mentioned.  However, when working on multiple projects that you may not get to finish, or someone else will pick it up on down the road, it’s always nice to include testing. How can you possibly expect any future developer to take over your code in a time efficient manner if they can’t test to make sure nothing is broken?  That being said, I’ve been guilty of being a selfish programmer for some time, but lately, I’ve started circling back on my practices and being sure to include a unit test.  Since I’ve been building a lot of node servers lately I will start with writing a unit test for a node server that utilizes a Sequelize database connection. Sequelize has quickly become my favorite javascript ORM and you can learn more about it here.

There are multiple unit testing libraries for node and the one I’ve used in the past was Mocha, but I’ve taken a different approach lately and started using the NodeUnit library, which can be found here. I like this library for being so incredibly simple and quick to set up.  You can start writing unit test immediately using just the tools on your command line to execute them or pull the library into a local file to execute from there.  For this example include “nodeunit” in your package.json so that we can create a reporter and execute one file to run all of our tests.

To start writing unit test the first step is to create a home for our test to live.  This is typically done in a /test folder within the root of the project.

Testing NodeJS  

Within this folder I generally start off by creating a master file that will run all my test contained inside the folder.  Create a new javascript file and name it “1.init_test.js”, for test I generally number my files to keep them in a proper hierarchy.  The final code for the initialize file looks like this.

Test NodeJS with Sequelize

Lines 3 and 4 simply set up our global variables to be used.  Line 3 is the standard file system util that comes with node.js and line 4 creates a Node Unit reporter.  The reporter object runs a collection of Node Unit tests.  Node Unit includes a variety of reporters and the ability to create your own, but for this example we just need the default reporter to run these test.

Line 6, sets up our initialize function and within this function, we want to check every file in our current directory (line 10) and filter out all test files and add it to our collection of tests.

Now for the actual files within the unit test, we’ll need to create a new javascript file.  For this example, I want to write a unit test for my authentication controller.  Following my own convention, I name the file after what I am testing.  For example, the Authentication controller will be named: “authentication.controller.test.js”.  I’m going to test two functions within this controller and go over how this executed by node unit.  My code for authentication.controller.test.js is as follows.

Test NodeJS with SequelizeTest NodeJS with Sequelize

All unit tests in nodeunit are simply an object which includes our test.  We have three functions within this module: setUp, createUserTest, and authorizeUserTest.  The setUp function will be called before each test by nodeunit, we need this function to setUp the connection to our database and sync our models with Sequelize so that the controller functions can flow as normal.  If anything is passed back to the callback function in setUp the test will automatically fail.  Each test in nodeunit is run in a series by order of definition so first, we’ll want to create a user here and then test whether we can authorize our user properly.  Since this controller is a node endpoint we need to pass in a request and response to properly call the methods.  That ResponseFactory object is a simple utility class to return a mock response object.  Now when going to the terminal and execute this test by calling “node init.test.js” we will see the following output.

Test NodeJS with Sequelize

I went ahead and changed the password in the authorize test to be something invalid and we will see the following output.

Test NodeJS with Sequelize

I could write another unit test in this file to include this as a failure and test the error response to be invalid and make sure my fail conditions fail properly as this one just did.  Basically, any type of response that goes on in your application should theoretically have a unit test associated with it, and it’s good to have something you can reliably run to expose issues correctly before they show up in production.

Related Posts

Ben Barrett

Ben Barrett

Ben is an experienced software developer working in .NET for Windows Mobile 6.0 and HTML and Javascript for the Windows Mobile 8 platform as well as with Java for back-end services. Ben graduated from Auburn University with a B.S. in Computer Engineering.