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

supertest-declarative

v0.6.1

Published

Declarative method of writing supertest scripts including multi-step REST API tests

Downloads

16

Readme

supertest-declarative

Making REST API testing a little less arduous.

Installation

npm install --save-dev supertest-declarative

Usage


const supertestDeclarative = require('supertest-declarative');
const express = require('express');

const run = supertestDeclarative.agent(app);

run({

  // a hook to run BEFORE beginning to run the test suit
  before: function () {
    // can return Promise
  },

  // a hook to run BEFORE each and every test
  beforeEach: function () {
    // can return Promise
  },

  // a hook to run AFTER the test suit runs
  // you could use this for some tear-down logic
  after: function () {
    // can return Promise
  },

  // a hook to run BEFORE each and every test
  afterEach: function () {
    // can return Promise
  },

  // things to add to each and every request in this suite by default
  common: [{

    // headers added to each and every request in this suite
    // can be overwritten in individual tests or requests
    headers: {
      'Accept': 'application/json',
      'Content-Type', 'application/json'
    },

    // url for all requests in this suite
    // can be overwritten in individual tests or requests
    url: '/users'
  }],


  // list of tests to run
  // each test can contain one or more requests that are executed
  // serially, passing the previous response to each subsequent
  // request
  tests: [{

    // a message to be displayed as the test('message', ...); argument of
    // tape test
    message: 'creates account with email and password',

    // run some code BEFORE the requests begin executing
    before: function () {
      // can return a Promise
    },

    // run some code BEFORE EACH and every request
    // the previous response is passed in as the only argument
    beforeEach: function (response) {
      // can return a Promise
    },

    // run some code AFTER the requests begin executing
    after: function() {
      // can return a Promise
    }

    // list of requests to execute
    requests: [
      // ... previous request definitions
      {
        // run some code BEFORE this request
        // useful to process the previous request
        // and add information to the new request
        // use the `response.next` object to modify the next request
        before: function(response) {
          // example: modify the next URL
          response.next.url = `/users/${response.body.id}`
          // example: add authorization header
          // response is a supertest response, so all methods are avilable
          response.next.Authorization = response.get('Authorization');
          // can return a Promise
        },

        // run some code AFTER this request
        // response represents the response of THIS request
        after: function (response) {
          // do something with response
          // can return a Promise
        }

        // a request body to send
        body: {
          email: '[email protected]',
          password: 'JustTesting1234*'
        },
        // the request method get, post, put, etc..
        method: 'post',

        // some assertions to run
        expected: {
          // expected status code
          status: 201,
          // expected headers
          headers: {
            // strings are supported
            'Content-Type': 'application/json',
            // regex is supported
            Authorization: /^Bearer .*$/
          },

          // assert a body literal
          // this does an object deep equal
          // so all properties must match exactly
          body: {
            id: 1,
            name: 'Matthew'
          },
          // -- OR --

          // assert some properties
          // note that only properties present here are run as assertions
          // so dynamic properties can be ignored or specified as regex
          bodyProperties: {
            id: /^\d+$/,
            name: 'Matthew'
          },

          // run some generic assertions
          // using any assertion framework you desire
          assert: function(res) {
            assert(res.body.name === 'Matthew', 'Name must be Matthew');
            // can return a Promise
          }
        }
      },
      // ... other request definitions
    ]
  }];
});