DEV Community

loading...
Cover image for API Testing Tools in JavaScript

API Testing Tools in JavaScript

Anudeep
#javascript #testing #opensource #pactumjs
Updated on ・3 min read

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 frisby --save-dev

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 = frisby.Joi; // Frisby exposes Joi for convenience
describe('Posts', function () {
  it('should return all posts and
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

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. It comes with a powerful mock server which can control the state of external dependencies.

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

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 pactum = require('pactum');

it('should save a new user', async () => {
  await pactum.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 visionmedia / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API.

SuperTest

Coveralls Build Status Dependencies PRs Welcome MIT License

HTTP assertions made easy via superagent.

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('express');
const app = express
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

Discussion (0)