Note: Unit testing is only available for Design Documents at this time.
Insomnia provides a way to test your APIs, all within the Test tab in Documents. Organize multiple tests under test suites and run them all at once.
Unit tests in Insomnia rely on the Mocha framework, and Chai for assertions.
Test Suites are made up of multiple tests that can all be run with one click.
In the middle panel, add individual tests. These will all belong to your Test Suite.
Unit tests are individual tests made up of one or more requests and an expected outcome.
Rename a unit test by double clicking on the unit test name and changing the contents. The value is saved automatically when you click outside the editable area.
To delete a unit test, click on the trashcan icon next to the individual test. You’ll be asked to confirm deletion.
Test the response payload by accessing the .data
attribute of the response variable.
Manually add the following JavaScript to an individual test. Access the JavaScript code editor by clicking the left-side dropdown arrow on a test.
const response = await insomnia.send();
expect(response.data).to.be.an('string');
By default, response.data
will be a string. To validate it as JSON, convert response.data
to JSON using JSON.parse
.
Manually add the following JavaScript to an individual test. Access the JavaScript code editor by clicking the left-side dropdown arrow on a test.
const response = await insomnia.send();
const body = JSON.parse(response.data);
expect(body).to.be.an('array');
Since unit tests rely on the Chai library for assertions, test properties after converting a response payload to JSON.
Manually add the following JavaScript to an individual test. Access the JavaScript code editor by clicking the left-side dropdown arrow on a test.
const response = await insomnia.send();
const body = JSON.parse(response.data);
const item = body[0];
expect(body).to.be.an('array');
expect(item).to.be.an('object');
expect(item).to.have.property('id');
Refer to Chaining Requests and select the chained request from the Select Request dropdown.
Alter request values in the Debug tab.
You can now console.log
values in your unit test to the console.
Manually add the following JavaScript to an individual test. Access the JavaScript code editor by clicking the left-side dropdown arrow on a test.
const response = await insomnia.send();
const body = JSON.parse(response.data);
const item = body[0];
console.log(item);
Run unit tests in CI (like GitHub Actions or Azure DevOps) using git sync and Inso CLI with the inso run test
command.
Learn more about Continuous Integration in Insomnia.