Unit Testing a Node.js Application with the Jasmine Testing Framework

When building an application, regardless if it is mobile, web, or neither, it is a good idea to come up with a collection of tests that can be used in a continuous integration scenario. I must admit, I don’t have the best habits when it comes to writing tests, but I’m sure my applications would be significantly better if I did.

Node.js is one of those technologies where there are a million different ways to solve a problem. That said, there are a few testing frameworks available, for example, Mocha with Chai, QUnit, and Jasmine. There is no wrong approach to this, but for this example we’re going to be focusing on Jasmine.

To get a better understanding of writing tests with Jasmine, it makes sense to start with a fresh project and work our way into it.

Create a new Node.js project with your Command Prompt or Terminal with the following command:

npm init -y

The above command will leave us with a package.json file. There are a few ways to get started with Jasmine. You could install it globally, or locally to the project as a development dependency. When it comes to continuous integration, it might be more logical to use it as a development dependency.

From the command line, execute the following command:

npm install jasmine --save-dev

Make sure that your command line path is the same as the project’s package.json file.

To use the Jasmine package, it can either be ran by using the long nasty path to the executable JavaScript file within the node_modules directory, or we can configure our project scripts to reference it.

Open the project’s package.json file and edit the scripts section to look like the following:

In the above example we have a single suite of several tests. The suite is like a grouping of tests, in this case our grouping is for testing the add function. Our tests are a little random in the above example, but we’re testing to see if our function result matches a desired number. We can have multiple expectations per every it chunk. The second test is to make sure the function doesn’t return a result that matches a desired input.

In our example, the first test will succeed, but the second will fail.

Now let’s look at a more realistic example, one using an API with HTTP and Express.

Before we can start using Express, it needs to be downloaded as a dependency within our project. From the command line, execute the following:

Before we jump into what our test is doing, we need to take a step back and figure out what Jasmine has to offer when it comes to testing. Remember, describe was for a test suite and it was for a particular test.

We also have access to things like beforeAll, beforeEach, afterEach, and afterAll. These are useful for test preparation and test cleanup.

In this group of tests we issue an HTTP request against our endpoint before trying to test anything. Because it is an asynchronous event, we need to work with the done callback. The done callback says, don’t move along to each test until we’re ready.

Using the results from the request, we can test however we please. For example, it might be useful to test the status codes, response body, or specific errors.

Go ahead and use npm test to see it in action.

Conclusion

You just saw how to use Jasmine to test various points of your Node.js application. The tests can be against functions or API endpoints if you’re creating your own server.

There are other types of tests that can be run in your Node.js application, but that explanation is best saved for another day.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.