Server Testing Stack with Chai, jsdom, and SuperTest

Hasan Gökçe
3 min readDec 10, 2020

Learn technologies to test on NodeJS server using Express.

Keywords

api responses, nodejs, express, test framework, Chai, jsdom, SuperTest, check status codes, check error messages

1. Introduction

Server tests are commonly used for API responses. But there is no limit, we can use it for server testing on any server response. Like checking status and error messages. In this post, we will examine a suite of technologies including Chai, jsdom, SuperTest on a JavaScript-based server. And will also use async/await keyword to be able to make asynchronous calls.

If you use test-driven development, if your tests fail at the feature level, it’s time for server level tests.

2. Test Framework: Chai

Instead of using Chai, we can use NodeJS’s default assertion library. For example, you have an array named foo. And you want to test if the array includes a specific element.

assert.ok(foo.includes(bar));

For enhancing readability, we can extend the default, built-in Node assertion library with Chai.

const {assert} = require('chai');

Using Chai, we can use this phrase like :

assert.include(foo, bar);

It also includes another type of text checking

assert.include('foobar', 'bar'); // Evaluates to true

For example, to verify the JavaScript pop method to work properly, we can use Chai as below.

3. Testing HTML Responses

In this example, we are going to work on a server that serves HTML codes as a response. It is possible to use include() for searching a string on the HTML code. But it is heavy, we need a parser library, for example; jsdom library. By using jsdom library, we can improve performance. For this example, we use jsdom functionality inside a function called parseTextFromHTML. By doing so, we improved the readability of our test code, but using it like this is not an obligation.

Above, we test #bar element whether it includes ‘hello’ or not. The question was, Does this element include the string of ‘Hello’? We leveraged jsdom library.

After running “npm test”

4. Async / Await

By using async / await keywords, we can do multiple operations without waiting for any other function's response. Node allows us to do this. Without using async/await our code is:

Without Async / Await
Async / Await
npm test

5. SuperTest

This library was designed for NodeJS. And it works well with Chai and Mocha libraries. We called it and assigned the response variable.

const request = require(‘supertest’);

To use it we pass app object inside supertest (assigned to request). To we make a GET request:

await request(app)
.get(‘/’)
.send();

It is possible making the POST request. We chain any properties or inputs to the request object and used the send:

await request(app)
.post(‘/messages’)
.type(‘form’)
.send({author, message});

6. Summary

We covered a set of technologies to able to run tests on a NodeJS server.

  • Chai: a library for extending the Node’s built-in assertion library.
  • jsdom: a library for interacting dom elements returned by the server response.
  • async / await: a pattern more readable asynchronous code
  • SuperTest: a library for making request to the server.

7. A test using all the technologies.

--

--