API testing is a type of software testing that focuses on determining if APIs meet expectations. It is critical for automating testing because APIs now serve as the primary interface to application logic.
Tools
Here is the list of popular tools available in JavaScript for API Automation Testing in alphabetical order.
Find other options at the end.
Supercharge Your Test Results with AI-Driven Insights from TestBeats
Anudeep γ» Aug 8
Chakram
Chakram
Chakram is no longer actively maintained, PRs are welcomed
Chakram is an API testing framework designed to perform end to end tests on JSON REST endpoints.
The library offers a BDD testing style and fully exploits javascript promises - the resulting tests are simple, clear and expressive. Chakram is built on node.js, mocha, chai and request.
This readme offers an introduction to the library. For more information, visit Chakram's documentation and tests which demonstrate all of Chakram's capabilities. In addition, example tests of publicly accessible APIs are available in the examples directory. If required, assistance can be found in the project's gitter chat room.
Features
- HTTP specific assertions. Allows testing of
- Status codes
- Cookie presence and value
- Header presence and value
- JSON values
- JSON structure (using the JSON schema specification)
- Compression
- Response times
- BDD formatting and hooks (e.g. beforeEach, afterEach)
- Promise based
- Pluginβ¦
Chakram is a REST API testing framework offering a BDD testing style and fully exploiting promises.
Chakram extends Chai.js, adding HTTP specific assertions. It allows simple verification of returned status codes, the compression used, cookies, headers, returned JSON objects and the schema of the JSON response.
describe("HTTP assertions", function () {
it("should make HTTP assertions easy", function () {
var response = chakram.get("http://httpbin.org/get");
expect(response).to.have.status(200);
expect(response).to.have.header("content-type", "application/json");
expect(response).not.to.be.encoded.with.gzip;
return chakram.wait();
});
});
Pros
- Easy to use
- Simple Testing Style
- Extendable & Customizable
Cons
- API Chaining is inelegant
Frisby.js
vlucas / frisby
Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
Frisby
Introduction
Frisby.js an API testing tool built on top of Jest that makes testing API endpoints easy fast and fun.
Installation
Install Frisby v2.x from NPM into your project:
npm install --save-dev frisby joi
Creating Tests
Simple Example
The minimum setup to run a single test expectation.
const frisby = require('frisby');
it('should be a teapot', function () {
// Return the Frisby.js Spec in the 'it()' (just like a promise)
return frisby.get('http://httpbin.org/status/418')
.expect('status', 418);
});
Nested Dependent HTTP Calls
A more complex example with nested dependent Frisby tests with Frisby's Promise-style then
method.
const frisby = require('frisby');
const Joi = require('joi');
describe('Posts', function () {
it('should return all posts and first post should have
β¦Frisby makes REST API testing easy, fast, and fun. Frisby.js comes loaded with many built-in tools for the most common things you need to test for to ensure your REST API is working as it should, and returning the correct properties, values, and types.
const frisby = require('frisby');
it ('POST should return a status of 201 Created', function () {
return frisby
.post('http://api.example.com/posts', {
title: 'My New Blog Post',
content: '<p>A cool blog post!</p>'
})
.expect('status', 201);
});
Pros
- Easy to use & setup
- Good Community Support
- Extendable & Customizable
Cons
- Only CRUD
- API Chaining is inelegant
PactumJS
PactumJS
REST API Testing Tool for all levels in a Test Pyramid
PactumJS is a REST API Testing Tool used to automate e2e, integration, contract & component (or service level) tests.
Documentation
This readme offers an basic introduction to the library. Head over to the full documentation at https://pactumjs.github.io
Need Help
We use Github Discussions to receive feedback, discuss ideas & answer questions.
Installation
# install pactum as a dev dependency
npm install --save-dev pactum
# install a test runner to run pactum tests
# mocha / jest / cucumber
npm install --save-dev mocha
or you can simply use
npx pactum-init
Usage
β¦PactumJS is a REST API Testing Tool for all levels in a Test Pyramid and used to automate e2e, integration, contract & component (or service level) tests.
const { spec } = require('pactum');
it('should save a new user', async () => {
await spec()
.post('https://jsonplaceholder.typicode.com/users')
.withHeaders('Authorization', 'Basic xxxx')
.withJson({
name: 'bolt',
email: 'bolt@swift.run'
})
.expectStatus(200);
});
Pros
- Lightweight
- Active & Growing
- Simple & Powerful
- Compelling Mock Server
- Elegant Data Management
- Advanced Retry Mechanism
- Extendable & Customizable
- Clear & Comprehensive Testing Style
- Component, Contract & E2E testing of APIs
Cons
- Limited Community Support
SuperTest
ladjs / supertest
π· Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
HTTP assertions made easy via superagent. Maintained for Forward Email and Lad.
About
The motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the lower-level API provided by superagent.
Getting Started
Install SuperTest as an npm module and save it to your package.json file as a development dependency:
npm install supertest --save-dev
Once installed it can now be referenced by simply calling require('supertest');
Example
You may pass an http.Server
, or a Function
to request()
- if the server is not
already listening for connections then it is bound to an ephemeral port for you so
there is no need to keep track of ports.
SuperTest works with any test framework, here is an example without using any test framework at all:
const request = require('supertest');
const express = require(
β¦SuperTest is built on a HTTP client called SuperAgent. The motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the lower-level API provided by superagent.
const request = require('supertest');
const express = require('express');
const app = express();
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
describe('GET /user', function() {
it('responds with json', function(done) {
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
Pros
- Simple & Powerful
- Good Community Support
Comparison
# | Chakram | Frisby.js | PactumJS | SuperTest |
---|---|---|---|---|
CRUD Operations | βοΈ | βοΈ | βοΈ | βοΈ |
Non CRUD Operations | β | β | βοΈ | βοΈ |
Global Request Setup | βοΈ | βοΈ | βοΈ | βοΈ |
Custom Assertions | βοΈ | βοΈ | βοΈ | βοΈ |
Exception Handling | β | β | βοΈ | β |
BDD Style | βοΈ | β | βοΈ | β |
Cucumber Support | β | β | βοΈ | β |
Inspectors | β | βοΈ | βοΈ | β |
Retry Mechanism | β | β | βοΈ | βοΈ |
Data Management | β | β | βοΈ | β |
Custom Reporting | β | β | βοΈ | β |
Mock Server | β | β | βοΈ | β |
Contract Testing | β | β | βοΈ | β |
Top comments (0)