npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

super-powered-api-testing

v2.1.3

Published

Sample REST API tests, written in Mocha + Chai

Downloads

10

Readme

Super-Powered API Testing

Mocha & Chai

Mocha and Chai are JavaScript libraries that make it easy to write complex API tests using an intuitive fluent syntax that's easy to learn.

Tests that run everywhere

Mocha and Chai give you an impressive list of options for running your tests. There's a CLI for running tests on the command line, which also works well for automated testing as well as CI/CD tools. There are also plugins for many popular IDEs and code editors, including VSCode, Atom, WebStorm, Visual Studio, and even Emacs. You can even run your tests in any web browser or mobile device. (more on that below...)

Mocha + Chai screenshots

Browsers & Mobile testing

This is an important feature that only Mocha and Chai offers. Browsers and mobile devices have many limitations and restrictions in place for security, privacy, and latency reasons, which your API may need to account for.

Are you sure that your API works with Same-Origin Policy and Content Security Policy? Have you properly implemented CORS? Does your API require any of the forbidden headers? You need to run your API tests on real-world client devices to be sure.

You can use tools such as Karma or Nightwatch to automate cross-browser testing and integrate it into your CI/CD pipeline. You can also use cloud-hosted device testing services such as SauceLabs or BrowserStack to test on a wider variety of devices.

SauceLabs test matrix

Some coding required

Whereas many other API testing tools let you build your tests using a graphical interface, Mocha and Chai are 100% JavaScript code. This makes it a great choice for people with coding skills, but perhaps not such a great option for teams with a diverse range of people who need to be able to create API tests.

There are numerous benefits to being 100% JavaScript though. As mentioned above, your tests can run anywhere, not just in a vendor-specific tool. You also aren't limited or restricted by the functionality of a tool. Many API testing tools don't support complex testing scenarios, such as conditional flows, loops, processing data files, setup/teardown steps, etc. But you can do all that and more in Mocha and Chai because it's just JavaScript.

You can also leverage numerous Chai plugins as well as the vast ecosystem of JavaScript libraries to help you with complex things like hasing and encryption or JSON Schema validation.

Demo Setup

In this demo, you'll run the API tests from the command-line, in an IDE, in a web browser, and via automated cross-browser testing.

Step 1. Install Node.js

The Mocha CLI requires Node.js 4.0 or greater.

Step 2. Download the demo code

Download this tarball file, which contains the sample tests and scripts to run them.

If you don't have a way to unpack the tarball, then you can use the following npx command:

npx decompress-cli super-powered-api-testing-2.1.3.tgz -o .

Step 3. Install dependencies

Open the demo code directory in a terminal and run the following command to install all dependencies. This will install Mocha and Chai, as well as the Karma test runner to run automated cross-browser tests.

cd /path/to/demo/code
npm install

Running the tests from the command-line

Open the demo code directory in a terminal and run the following command:

cd /path/to/demo/code
npm run mocha

This command runs the Mocha CLI, which will read the mocha.opts file. This file configures several Mocha options, such as timeouts and failure behavior. It also tells Mocha which test scripts to run.

The test/fixtures folder

This folder contains scripts that perform initialization logic and define helpers that are used by the tests

|Script file |Purpose |:-----------------------------|:----------------------------------------------------------- |test/fixtures/assertions.js |Defines custom Chai.js assertions, which eliminate redundancy and make our tests more readable (e.g. response.body.should.be.a.character()) |fixtures/before-each.js |Defines a Mocha beforeEach hook, which will reset the state before each test by deleting any data that were created by the previous test. |test/fixtures/environment.js|Sets environment variables, such as API_ROOT and API_KEY. These variables allow us to test against different instances of our API (e.g. dev, staging, prod) |test/fixtures/open-api.js |This script downloads the OpenAPI definition, so we can use it to test whether our API responses match the JSON Schemas. |test/fixtures/setup-chai.js |This script initializes Chai and loads the Chai-HTTP and Chai-JSON-Schema plugins.

CLI screenshot

Running the tests in an IDE

Many popular IDEs and text editors have built-in support for Mocha, so you can easily run your tests with the press of a button and see the results right in your IDE.

IDE screenshot

Running the tests in a web browser

Mocha and Chai can run in web browsers too, which is very important for testing your API in real-world conditions. You can even run your tests in mobile browsers, such as iOS and Android, and in hybrid mobile apps such as PhoneGap, Cordova, and React Native.

Just open the browser.html file in a web browser. Or run the following command to open it in your default browser:

cd /path/to/demo/code
npm start

The Mocha web UI automatically runs all of your tests and checks-off each one as they pass. If any tests fail, you'll see the reason for the failure. You can click on any test to see the source code for that test. You can also click the arrows on the far right side of the page to run a single test, which is great for debugging.

Browser screenshot

Running automated cross-browser tests

Mocha and Chai work great with cross-browser automation tools such as Karma or Nightwatch, which can easily be integrated into your CI/CD pipeline. You can also use cloud-hosted device testing services such as SauceLabs or BrowserStack to test on a wider variety of devices.

For this demo, we'll use Karma, which is a Node.js-based CLI. Karma is a powerful with lots of configuration options and hundreds of plugins, which can be daunting at first. For this demo, I've already created a karma.conf.js config file, so all you need to do is run the following command:

cd /path/to/demo/code
npm run karma

Depending on your operating system, Karma will automatically launch Chrome, Firefox, Safari, and/or Internet Explorer and run the tests in each browser. Once the tests are complete, Karma will close the browsers and show the results in the terminal window.

Karma can also run in a "watch" mode, where it will keep the browsers running instead of closing them after the tests finish. In this mode, it will monitor your source code for changes and will automatically re-run the tests each time you save.

Karma can be integrated into TravisCI, Jenkins, TeamCity, or any other CI/CD tool that can run Node.js scripts.

Karma screenshot