Write tests for data types in Insomnia

Uses: Insomnia
TL;DR

Legacy unit tests are hidden by default since Insomnia 13.3. After you enable them and add an API Collection, you can create a new test suite for the API Collection and then create individual tests in the suite.

Prerequisites

Download and install Insomnia.

For this tutorial, you’ll need an API Collection in Insomnia. An API Collection is essential for this tutorial to function, but creating one isn’t the focus of this guide. Use the following to add a pre-configured API Collection to Insomnia:

Run in Insomnia

This pre-configured API Collection contains:

v13.3+ Legacy unit tests are hidden by default.

If an API Collection already contains legacy unit tests, its Tests tab still appears automatically. Otherwise, to follow this guide, go to Preferences > General > Application and enable Show legacy unit tests, which shows the Tests tab on every API Collection.

For new work, use pre-request and after-response scripts instead. Scripts are the recommended way to test APIs in Insomnia.

Create a test suite

Before you create a test, you need to create a test suite for your API Collection.

  1. If your API Collection doesn’t have a Tests tab, go to Preferences > General > Application and enable Show legacy unit tests.
  2. On the API Collection screen, click the Tests tab.
  3. In the sidebar, click New test suite.

Create a top-level data type in body test

Now we can test if a data type is returned in the top-level request body.

  1. Click New test and enter a name for the test, such as “Data type in body (top-level)”.
  2. From the Select a request drop down, select the GET KongAir planned flights request.
  3. Enter the following JavaScript to check if an array is present in the top-level body of the response:
    const response1 = await insomnia.send();
    const body = JSON.parse(response1.data);
    expect(body).to.be.an('array');
  4. Click the Play icon next to the test. In the preview to the right, you should see that the test passes.

Create a top-level data type in body test

Now you can test if a data type is returned in the top-level request body.

  1. Click New test and enter a name for the test, such as “Data type in body (nested)”.
  2. From the Select a request drop down, select the GET Fetch more details about a flight request.
  3. Enter the following JavaScript to check if the first string in the meal_options array is present in the body of the response:
    const response1 = await insomnia.send();
    const body = JSON.parse(response1.data);
    expect(body).to.be.an('object');
    expect(body).to.have.property('meal_options'); 
    expect(body.meal_options).to.be.an('array');
    if (body.meal_options.length > 0) {
        expect(body.meal_options[0]).to.be.a('string');
    }
  4. Click the Play icon next to the test. In the preview to the right, you should see that the test passes.

Cleanup

If you created a new collection and want to delete it, navigate to the collection, click it’s name in the sidebar on the right, and then select Delete.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!