Getting Code Coverage through integration Tests in Node

Christian Kaatz
ITNEXT
Published in
2 min readSep 10, 2020

--

About 2 years ago I started a project which I had to rapidly develop due to time constraints. The project itself went from a CLI tool backed by a file db into a web app with an API powered backend but still a file db. At a certain point, I decided to move the data into a persistent storage, which also happened in two steps, first MongoDB on mlab and then moved over to MongoDB Atlas.

I knew that I am going to face technical debt very soon — especially during those rewrites - but couldn’t afford to spend too much time on writing tests as long as I wasn’t sure whether the project has potential to be publicly launched.

It is now live at qzmkr.com and can be used to run multiple choice tests for public or closed audiences based on invites.

Testing Strategy

As mentioned before, I came across the c8 tool by Benjamin Coe from npm. His tool makes it possible to make use of setting the NODE_V8_COVERAGE path and output code coverage information directly from V8 and adding Istanbul reporter support. Without special Istanbul/nyc or other coverage tools it is possible to run a testsuite against your node service and get detailed coverage data.
I already maintained a postman collection to test my ReSTful API which also can be used from the cli via newman as a test scenario.
The last remaining bit was to startup the server with c8, once up, run the testsuite and tear down the server again and return the exit code of the testsuite to be able to run it in CI.

IMPORTANT!

In order to have support for the env variable NODE_V8_COVERAGE to take effect, it requires at least node 10.12

It is important to mention, that the nodejs service needs to gracefully shutdown, so that V8 can finish up writing the coverage reports without an abrupt process exit.

For that I started to listen for SIGINT and SIGTERM on the process, disconnect the database and stop the server gracefully.

How-To

npm install --save-dev newman c8

# run your service (change your js accordingly)
c8 node server.js

# run your tests with newman
newman -e postman-environment.json run testsuite.postman-collection.json

Result

Now I am able to maintain a certain level of stability as I keep my postman collections up to date and run my integration testsuite in CI. For complex algorithms and logic, I still keep my unit tests. But for most of API related code, I don’t have to mock the world anymore and still get useful coverage output and therefore confidence.

Code

Example code can be found on github

Links

Initial Post by Benjamin Coe

--

--

Dedicated Solution Engineer with focus on agile software development, team culture and evolutionary architectures.