Back to articles

testing an express.js app

One of the biggest changes I had when I moved from an agency role to my current one is around test and assurance.

I also moved away from Scala/play to Express.js on a project so I had to spend a bit of time working out what to test, and how to test it, thankfully pretty much everything in Express is a function so can be easily broken down to test.

My setup is as following

mocha test examples

Roughly speaking my tests are currently split into three sections libs, controller functions and integration.

Libs - broadly covers anything, object creators, form validation, helpers.

Controllers functions - these are tested without spinning up the application, using fake request and respond objects. This allows testing implementation of libs and general controller logic.

Integration tests - originally I was spinning up a local app to test against, this was leading to issues blocking other CI jobs, it was much easier just to run against a development environment. These in my case give assurance at a much higher level, these check that a user can login, and perform the various transaction with backend services.

Outside of actually testing I am using xo to do ESlint, this helps ensure code consistency in terms of style across the project.

These are split off as separate jobs on Jenkins Libs/Controllers/XO are all part of a build and deployment script, while integration tests are run as a separate job as they have backend dependencies.

I also started using Chai for assert, this provides more sugar around the assertion langue, which I find is a little easier to read.

I’ve used code coverage tools in the past usually applies to the whole project, with Istanbul it only counts files you have actually used in your test suite. This helped identify a two things; untested code and unused code.

There are a few other modules that can also help with testing that I haven’t needed to use yet

Also, you may have noticed the lack of testing around HTML, accessibility and content, broadly these are covered off by acceptance tests, however, I will add this in a separate post.