API Testing Tools in JavaScript

asaianudeep

Anudeep

Posted on June 6, 2021

API Testing Tools in JavaScript

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.

Chakram

GitHub logo dareid / chakram

REST API test framework. BDD and exploits promises

Chakram

Build Status Test Coverage Code Climate Gitter

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();
  });
}); 


Enter fullscreen mode Exit fullscreen mode

Pros

  • Easy to use
  • Simple Testing Style
  • Extendable & Customizable

Cons

  • API Chaining is inelegant

Frisby.js

GitHub logo vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.

Frisby

NPM CI

Frisby.js

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);
});
Enter fullscreen mode Exit fullscreen mode

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
…
Enter fullscreen mode Exit fullscreen mode

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);
});


Enter fullscreen mode Exit fullscreen mode

Pros

  • Easy to use & setup
  • Good Community Support
  • Extendable & Customizable

Cons

  • Only CRUD
  • API Chaining is inelegant

PactumJS

GitHub logo pactumjs / pactum

REST API Testing Tool for all levels in a Test Pyramid

logo

PactumJS

Build Coverage Downloads Size Platform

Stars Twitter

REST API Testing Tool for all levels in a Test Pyramid


PactumJS Demo









PactumJS is a REST API Testing Tool used to automate e2e, integration, contract & component (or service level) tests.

  • ⚑ Swift
  • 🎈 Lightweight
  • πŸš€ Simple & Powerful
  • πŸ› οΈ Compelling Mock Server
  • πŸ’Ž Elegant Data Management
  • πŸ”§ Extendable & Customizable
  • πŸ“š Clear & Comprehensive Testing Style
  • πŸ”— Component, Contract & E2E testing of APIs

----------

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
Enter fullscreen mode Exit fullscreen mode

or you can simply use

npx pactum-init
Enter fullscreen mode Exit fullscreen mode

----------

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);
});


Enter fullscreen mode Exit fullscreen mode

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 logo 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.

code coverage Build Status Dependencies PRs Welcome MIT License

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
Enter fullscreen mode Exit fullscreen mode

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(
…
Enter fullscreen mode Exit fullscreen mode

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);
});
});

Enter fullscreen mode Exit fullscreen mode




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

πŸ’– πŸ’ͺ πŸ™… 🚩
asaianudeep
Anudeep

Posted on June 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

API Testing Tools in JavaScript
javascript API Testing Tools in JavaScript

June 6, 2021