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.
REST API test framework. BDD and exploits promises
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.
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 (){varresponse=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;returnchakram.wait();});});
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.
constfrisby=require('frisby');it('should be a teapot',function(){// Return the Frisby.js Spec in the 'it()' (just like a promise)returnfrisby.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.
constfrisby=require('frisby');constJoi=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.
constfrisby=require('frisby');it ('POST should return a status of 201 Created',function (){returnfrisby.post('http://api.example.com/posts',{title:'My New Blog Post',content:'<p>A cool blog post!</p>'}).expect('status',201);});
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
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 ()=>{awaitspec().post('https://jsonplaceholder.typicode.com/users').withHeaders('Authorization','Basic xxxx').withJson({name:'bolt',email:'bolt@swift.run'}).expectStatus(200);});
π· Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
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:
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.