Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tests in Insomnia #108

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions app/_how-tos/write-content-type-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: Write tests for content types in Insomnia
related_resources:
- text: Write tests for headers in the response in Insomnia
url: /how-to/write-headers-in-response-test/
- text: Write tests for HTTP status codes in Insomnia
url: /how-to/write-http-status-tests/
- text: Write tests for data types in Insomnia
url: /how-to/write-data-type-tests/
- text: Automate tests in Insomnia
url: /how-to/automate-tests/
- text: Chain requests in Insomnia
url: /how-to/chain-requests/

products:
- insomnia

tags:
- test-apis

tldr:
q: How do I write content type tests in Insomnia?
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite.

prereqs:
inline:
- title: Create and configure a collection
include_content: prereqs/create-collection
icon_url: /assets/icons/menu.svg
cleanup:
inline:
- title: Clean up Insomnia
include_content: cleanup/products/insomnia
icon_url: /assets/icons/insomnia/insomnia.svg
---

## 1. Create a test suite

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

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

## 2. Create a top-level content type in body test

Now you 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)".
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request.
1. The following Javascript checks if the top-level array is present in the body of the response. Enter the following in the Javascript for your test:
```javascript
const response1 = await insomnia.send();
const body = JSON.parse(response1.data);
const item = body[1];
expect(item).to.be.an('object');
```
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.

## 2. Create a nested content type in body test

Now you 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)".
1. From the **Select a request** drop down, select the **GET Fetch more details about a flight** request.
1. The following Javascript checks if the nested `meal_options` array is present in the body of the response. Enter the following in the Javascript for your test:
```javascript
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');
```
Comment on lines +65 to +71
Copy link
Contributor Author

@cloudjumpercat cloudjumpercat Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: This was created with ChatGPT. Although the test passes, I don't fully trust it or understand it. Is meal_options really nested? Is this really testing the right thing?

Also, my gut is telling me that some of these lines might be extraneous/unnecessary.

1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.
74 changes: 74 additions & 0 deletions app/_how-tos/write-data-type-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: Write tests for data types in Insomnia
related_resources:
- text: Write tests for headers in the response in Insomnia
url: /how-to/write-headers-in-response-test/
- text: Write tests for HTTP status codes in Insomnia
url: /how-to/write-http-status-tests/
- text: Write tests for content types in Insomnia
url: /how-to/write-content-type-tests/
- text: Automate tests in Insomnia
url: /how-to/automate-tests/
- text: Chain requests in Insomnia
url: /how-to/chain-requests/

products:
- insomnia

tags:
- test-apis

tldr:
q: How do I write data type tests in Insomnia?
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite.

prereqs:
inline:
- title: Create and configure a collection
include_content: prereqs/create-collection
icon_url: /assets/icons/menu.svg
cleanup:
inline:
- title: Clean up Insomnia
include_content: cleanup/products/insomnia
icon_url: /assets/icons/insomnia/insomnia.svg
---

## 1. Create a test suite

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

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

## 2. 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 (top-level)".
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request.
1. The following Javascript checks if an array is present in the top-level body of the response. Enter the following in the Javascript for your test:
```javascript
const response1 = await insomnia.send();
const body = JSON.parse(response1.data);
expect(body).to.be.an('array');
```
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.

## 3. 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)".
1. From the **Select a request** drop down, select the **GET Fetch more details about a flight** request.
1. The following Javascript checks if the first string in the `meal_options` array is present in the body of the response. Enter the following in the Javascript for your test:
```javascript
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');
}
```
Comment on lines +64 to +73
Copy link
Contributor Author

@cloudjumpercat cloudjumpercat Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: This was created with ChatGPT. Although the test passes, I don't fully trust it or understand it. Is meal_options really nested? Is this really testing the right thing?

Also, my gut is telling me that some of these lines might be extraneous/unnecessary.

1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.
57 changes: 57 additions & 0 deletions app/_how-tos/write-headers-in-response-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Write tests for headers in the response in Insomnia
related_resources:
- text: Write tests for HTTP status codes in Insomnia
url: /how-to/write-http-status-tests/
- text: Write tests for content types in Insomnia
url: /how-to/write-content-type-tests/
- text: Write tests for data types in Insomnia
url: /how-to/write-data-type-tests/
- text: Automate tests in Insomnia
url: /how-to/automate-tests/
- text: Chain requests in Insomnia
url: /how-to/chain-requests/

products:
- insomnia

tags:
- test-apis

tldr:
q: How do I write tests for response headers in Insomnia?
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite.

prereqs:
inline:
- title: Create and configure a collection
include_content: prereqs/create-collection
icon_url: /assets/icons/menu.svg
cleanup:
inline:
- title: Clean up Insomnia
include_content: cleanup/products/insomnia
icon_url: /assets/icons/insomnia/insomnia.svg
---

## 1. Create a test suite

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

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

## 2. Create a headers in response test

Now you can test if a header is returned in the response.

1. Click **New test** and enter a name for the test, such as "Header in the body".
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request.
1. The following Javascript checks if there are any headers in the response. Enter the following in the Javascript for your test:
```javascript
const response1 = await insomnia.send();
expect(Object.keys(response1.headers).length).to.be.greaterThan(0);
const body = JSON.parse(response1.data);
const item = body[1];
expect(item).to.be.an('object');
```
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.
53 changes: 53 additions & 0 deletions app/_how-tos/write-http-status-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Write tests for HTTP status codes in Insomnia
related_resources:
- text: Write tests for headers in the response in Insomnia
url: /how-to/write-headers-in-response-test/
- text: Write tests for content types in Insomnia
url: /how-to/write-content-type-tests/
- text: Write tests for data types in Insomnia
url: /how-to/write-data-type-tests/
- text: Automate tests in Insomnia
url: /how-to/automate-tests/
- text: Chain requests in Insomnia
url: /how-to/chain-requests/

products:
- insomnia

tags:
- test-apis

tldr:
q: How do I write HTTP status code tests in Insomnia?
a: After you add a collection, you can create a new test suite for the collection and then use the default Javascript test.

prereqs:
inline:
- title: Create and configure a collection
include_content: prereqs/create-collection
icon_url: /assets/icons/menu.svg
cleanup:
inline:
- title: Clean up Insomnia
include_content: cleanup/products/insomnia
icon_url: /assets/icons/insomnia/insomnia.svg
---

## 1. Create a test suite

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

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

## 2. Create a HTTP status code test

You can create a test that checks a request against a certain status code.

1. From the Test Suite you just created, click **New test**. Insomnia creates a default `Return 200` request for you:
```javascript
const response1 = await insomnia.send();
expect(response1.status).to.equal(200);
```
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request.
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes.
3 changes: 0 additions & 3 deletions app/_how-tos/write-tests-in-insomnia.md

This file was deleted.

1 change: 1 addition & 0 deletions app/_includes/cleanup/products/insomnia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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**.
11 changes: 11 additions & 0 deletions app/_includes/prereqs/create-collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1. Import the KongAir Flights spec in Insomnia:
<a href="https://insomnia.rest/run/?label=&uri=https%3A%2F%2Fraw.githubusercontent.com%2FKong%2FKongAir%2Frefs%2Fheads%2Fmain%2Fflight-data%2Fflights%2Fopenapi.yaml" target="_blank"><img src="https://insomnia.rest/images/run.svg" alt="Run in Insomnia"></a>
1. Click the **Settings** icon for **SPEC** in the sidebar, and then click **Generate collection**.
1. Click **Base environment** and then click the **Edit** icon.
1. Add the following content to the base environment:
```json
{
"base_url": "https://api.kong-air.com",
"flightNumber": "KA0284"
}
```
49 changes: 38 additions & 11 deletions app/_landing_pages/insomnia/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,8 @@ rows:

- header:
type: h2
text: "Get started"
text: "Features"
columns:
- blocks:
- type: card
config:
title: Organize and run your API tests
description: Write customizable Javascript tests directly in Insomnia. Organize your integration tests as needed and run them simultaneously to identify errors.
icon: /assets/icons/insomnia/checkbox-active.svg
cta:
text: Learn more
url: /how-to/write-tests-in-insomnia/
align: end
- blocks:
- type: card
config:
Expand Down Expand Up @@ -73,6 +63,43 @@ rows:
text: Learn more
url: /how-to/collection-runner/
align: end

- header:
type: h2
text: "Get started writing tests"
columns:
- blocks:
- type: card
config:
title: Write tests for HTTP status codes
cta:
text: Learn more
url: /how-to/write-http-status-tests/
align: end
- blocks:
- type: card
config:
title: Write tests for content types
cta:
text: Learn more
url: /how-to/write-content-type-tests/
align: end
- blocks:
- type: card
config:
title: Write tests for headers in the response
cta:
text: Learn more
url: /how-to/write-headers-in-response-tests/
align: end
- blocks:
- type: card
config:
title: Write tests for data types
cta:
text: Learn more
url: /how-to/write-data-type-tests/
align: end
- header:
type: h2
text: Learn
Expand Down
16 changes: 16 additions & 0 deletions app/assets/icons/insomnia/insomnia.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.