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

mocha-axios

v1.4.2

Published

http assertions for mocha using axios

Downloads

173

Readme

mocha-axios

NPM Tests

HTTP assertions for mocha using axios.

About

This is a simplified HTTP testing library designed to decrease the amount of time you spend writing end-to-end tests. Designed to drop into mocha tests, backed by axios, written with ES6 async/await.

Installation

$ npm install --save-dev mocha mocha-axios

Usage

const app = require('./example-app')
const integration = require('mocha-axios');

describe('some-awesome-API', function () {
  it('should login correctly', integration({
    app,
    req: {
      method: 'POST',
      url: '/login',
      data: {
        method: 'email',
        email: '[email protected]',
        password: 'correct-horse-battery-staple',
      },
    },
    res: {
      status: 200,
      headers: {
        'X-Auth-Token': 'e409413fd5b4bad63f0ee4093b0b0e9b',
      },
      data: {
        user: {
          id: '1',
          username: 'jdrydn',
        },
      },
    },
  }));
});

API

integration({

  // Any "app" that can be passed into http.createServer
  // For integration tests this is an express API
  app,
  // app: express(),

  // The request object should be an object that can be dropped into axios
  // See https://www.npmjs.com/package/axios#request-config
  req: {
    method: 'GET',
    url: '/',
    params: { one: 'two' },
  },

  // The response object is a similar-ish axios response object
  res: {
    // Include a status property to check the status of the response
    status: 200,

    // Include a headers property to check individual headers of the response
    headers: {
      // Including a regex value to test it
      'Content-Type': /json/,
      // Include a string value to exact-match it
      'X-Powered-By': 'Express',
    },

    // Include a data property to check the body of the response
    // If you don't set the responseType in the req then JSON is assumed
    data: 'Hello, world!',
  },

  // You can specify a before function, which can return a Promise, to execute before anything actually happens
  before() {},
  // And you can specify a after function, which can return a Promise, to execute after all assertions have been made
  after() {},
})

Extensions

You can optionally write your own extensions for mocha-axios, by registering them globally like so:

const integration = require('mocha-axios');

integration.with('auth', {
  before(username, req) {
    req.headers['x-auth-token'] = createSignedTokenFor(username);
  },
});

it('should fetch Malcom\'s inbox', integration({
  app,
  auth: '[email protected]',
  req: {
    method: 'GET',
    url: '/user/2321/inbox',
  },
  res: {
    status: 200,
  },
}));

modify

To make it easier to mock HTTP endpoints where the data may change over time, there's a way to mock certain values in the response data to fixed values. Please note: this will only overwrite values that have the same type, e.g. strings numbers etc, in order to correctly return errors regarding missing properties / unusual properties!

This requires lodash, either the entire library (if you're already using it) or the two individually packaged _.get _.set, so don't forget to install them (with --save-dev if you're not going to use it yourself!)

const integration = require('mocha-axios');

// Set res-modify to anything you want
integration.with('res-modify', require('mocha-axios/withModify'));

it('should fetch a member profile', integration({
  app,
  req: {
    method: 'GET',
    url: '/member/2321/',
  },
  res: {
    status: 200,
    modify: {
      'user.id': 'some-id',
      'user.created_date': 'YESTERDAY',
    },
    data: {
      user: {
        id: 1,
        username: 'jdrydn',
        created_date: 'YESTERDAY',
      },
    },
  },
}));

A list of known extensions is coming soon!

More