Write tests for content types in Insomnia

Uses: Insomnia
TL;DR

After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite.

Prerequisites

Download and install Insomnia.

For this tutorial, you’ll need a collection in Insomnia. A 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 collection to Insomnia: Run in Insomnia

This pre-configured collection contains:

Create a test suite

Before we create a test, we need to create a test suite for our collection.

To do this, click the Tests tab and click New test suite in the sidebar.

Create a top-level content type in body test

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

  1. Click New test and enter a name for the test, such as “Content 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 the top-level array is present in the body of the response:
    const response1 = await insomnia.send();
    const body = JSON.parse(response1.data);
    const item = body[1];
    expect(item).to.be.an('object');
    
  4. Click the Play icon next to the test. In the preview to the right, you should see that the test passes.

Create a nested content type in body test

Now we can test if a content type is returned in the nested request body.

  1. Click New test and enter a name for the test, such as “Content 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 nested 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.have.property('meal_options'); 
    expect(body.meal_options).to.be.an('array');
    expect(body).to.be.an('object'); 
    
  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.

Something wrong?

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!