API Testing Tools in JavaScript
Tags: javascript, testing, api, pactumjs
Published: 2021-06-06 12:03:00

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.

- [Chakram](http://dareid.github.io/chakram/)
- [Frisby.js](https://docs.frisbyjs.com/)
- [PactumJS](https://pactumjs.github.io/)
- [SuperTest](https://github.com/visionmedia/supertest)

> Find other options at the end.

### Chakram

{% github dareid/chakram %}

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.

```js
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

{% github vlucas/frisby %}

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.

```js
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

{% github pactumjs/pactum %}

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.

```js
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

{% github visionmedia/supertest %}

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.

```js
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** | ❌ | ❌ | ✔️ | ❌ |


## Others

- [autokin](https://github.com/ariesb/autokin)
- [freddo](https://github.com/meeshkan/freddo)
- [just-api](https://github.com/kiranz/just-api)
- [ply](https://github.com/ply-ct/ply)
- [teakozi](https://github.com/jpthethi/teakozi)