Using SuperTest to test API’s

I have deployed this API. It’s a google cloud function that returns the minimum supported version for a fictional mobile app. This blog will walk you through how you can build a test harness in SuperTest for testing this API.

What is SuperTest?

SuperTest is a node javascript package that is used for testing API’s, you will need to install node and npm (node package manager) to use SuperTest. Here are the Windows instructions and the Mac instructions.

Explaining the API

This API can let the user know if they need to update their app or if there is an upcoming outage. It’s a common feature with finance apps when the mobile app needs to be taken offline to support a back end deployment.

Here’s some sample notifications asking the user to update or to try again later.
Sunsuper often has weekend deployments and I get in app notifications alerting me to this.

It’s a GET request, returns an android version, an iOS version and an optional scheduled or outage message. You can copy paste this URL into your browser and hit this service.

https://australia-southeast1-buggybank.cloudfunctions.net/min-app-version

Here’s a sample payload when there is an upcoming scheduled message:

{
    Android: "1.0.0",
    iOS: "2.1.0",
    scheduledMessage: "Our service will be down this Friday from 8pm AEST to 10pm"
}

There isn’t much code needed to create this API, it’s not much more than this:

var minAndriodVersion = "1.0.0"
var minIOSVersion = "2.1.0"

exports.minVersion = (req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ "Android": minAndriodVersion, "iOS": minIOSVersion, "scheduledMessage": "Our service will be down this Friday from 8pm AEST to 10pm" }));
  res.status(200);
};

Let’s build a test harness

Add the API to an abstraction function, this makes your tests easier to read. Make sure SuperTest and typescript are in your package.json file.

import request from 'supertest'

export async function checkMinSupportedVersionAPIReturns200(
) {
    const response = await request('https://australia-southeast1-buggybank.cloudfunctions.net/')
        .get(`min-app-version`)
        .expect(200)
}

Import the abstraction function. Then you can call your test. You can checkout this code on this branch on github.

import {checkMinSupportedVersionAPIReturns200} from "./APIs/checkMinSupportedVersionAPIReturns200";

describe ('Smoke test APIs', ()=> {
    test ('Check API is up and running', async() => {
        await checkMinSupportedVersionAPIReturns200()
    })
})

Test for a failing test

If you’d like to see what the API is returning set up a incorrect body expectation. Add this expectation to the checkMinSupportedVersionAPIReturns200 function. This will cause the test to fail but show what response it got from the API.

expect(response.body).toEqual({ some: 'response body' });

Checking the cloud

I can now see this API being called in my google cloud platforms logs:

I was surprised how easy it was to get started. The next blog post in this series will be on how to add a POST request to this framework and how to incorporate an event queuing system.

2 comments

Leave a Reply