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 🙏

© 2026 – Pkg Stats / Ryan Hefner

swaggest-test

v1.4.1

Published

better swagger tests

Readme

swaggest-test

Swaggest test is a Swagger API specification based testing protocol. It utilizes Swagger JSON files to build a series of requests and check responses to those requests. Adding additional tests is as easy as adding additional documentation, making it simple to build and deploy unit tests without separating the docs.

Here at Motel we started to use Swagger more to build out and document our APIs. Once we found swagger-test, we loved it, we just needed to cover a few bases.

We've created this module to create a more complete set of features that better fit our use cases. Features we include in addition to swagger-test:

This allows us to test the actual values and data that are being returned, instead of just testing status codes.

Usage

Setup

npm install swaggest-test

Constructing Tests

The tests are set up within the swagger.yaml file. Unfortunately, as of right now, swaggest-test only supports swagger.json files. You can use the swagger editor to convert from yaml to json, or just transfer this data to json.

The tests are within your endpoint definitions after a special identifier x-test. See the following example for clarification:

paths:
  /pets:
    get:
      description: Returns all pets from the system that the user has access to
      parameters: <blah>
      responses:
        '200': <blah>
      x-test:
        <blah>

Once you setup this section, you can fill it in with a list of values, very similar to how the the endpoint definition is setup. Take the following as an example:

  x-test:
    - description: Basic status test
      request: null
      response:
        '200': null

This is a test in its most basic form. This test will make a simple GET request to /pets and expect a '200' response from it.

Now that we have a simple test built, we need to run it!

Running Tests

The recommended way to run tests is with the swaggest-test library. This library contains an automated test runner that will perform all of your tests. The following is the most basic execution:

var swaggestTest = require('swaggest-test');
swaggestTest.runTests(__dirname + '/swagger.json');

Note that this library is written for mocha, not node, so you will need to save this file and run it with mocha. Mocha can be installed if needed with the following command:

npm install -g mocha

Once that's done, you can run your tests!

Parameters

Parameters have a simple key: value format in the Swagger yaml.

x-test:
  request:
    parameters:
      color: 'yellow'

Parameters are setup to auto-detect where they belong. What this means is that if you have a parameter named color in your query, you don't need to tell swaggest-test where to put color. You simply define color and fill it.

Arrays

Arrays simply follow YAML syntax.

x-test:
  request:
    parameters:
      colors:
        - 'yellow'
        - 'black'

Objects

Objects in parameters have a simple extended-key representation.

x-test:
  request:
    parameters:
      dogs.color: 'yellow'

This example would set the color member of the dogs object to yellow. It is important to note that the swaggest-test parser already dives 1 deep into the body. This means that if you have a body parameter named color, you don't need to specify body.color. swaggest-test will find the color parameter in the body and fill it automatically. However, if your parameter is deeper, such as body.dogs.color, you will have to specify dogs.color.

Variables

The most basic feature of swaggest-test is variables. Defining variables is simple, simply have a $ in front of your variable name in a given field. For example:

x-test:
  request:
    parameters:
      color: $color1

swaggest-test will auto-fill this with the given object. In order to specify this, you need to pass an object to the swaggest-test tester library as the 2nd object, like the following:

var swaggestTest = require('swaggest-test');
swaggestTest.runTests(__dirname + '/swagger.json', {color1: 'yellow'});

Variables are extremely powerful, especially if you need to do things like token authentication and don't want to commit that to information to Git.

Responses

Responses are setup in the same way as parameters, except that they are children of the response status, such as the following

x-test:
  response:
    '200':
      message: 'SUCCESS!'

This example will require a '200' response with a message of SUCCESS!. Everything else from the parameters, such as nested objects, variables, and arrays carries over.

Type Checking

swaggest-test also tests the types of all of the responses. This means that if you have a member of the response object that is defined in your documentation to be a string, we verify that it actually is a string. There is no way to turn this off, because we believe that if this fails, you should update your documentation or your code, not tell swaggest-test to ignore it.

Definitions

Using $ref in parameters e.g. - $ref: '#/parameters/test' is allowed, as well as using $ref in the body schema, like schema: $ref: '#/parameters/test'.

Running Tests With flat-white

Swaggest-test includes a tool called flat-white which will run mocha based tests off of your swagger.json file, without the need for any setup. If you install swaggest-test globally via npm with npm install -g swaggest-test, you can run flat-white swagger.json and it will parse your swagger file and run your tests.

Have variables? Cool! Flat-white reads your environment variables, so you can either define them in your shell or create a file named .env that looks something like this

TOKEN=himom

This will fill in your $TOKEN variable with himom

References

This library was originally based off of swagger-test, but has been changed almost entirely.

Also huge thanks to @saminakh and her work on the Motel swagger-test branch. She did some great work and inspired me to continue making improvements to this library!