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

mocked-api

v0.6.8

Published

A server to mock API responses with local JSON-files and configurable overrides.

Downloads

51

Readme

Mocked API

This server can mock the responses of an API with files on disk. Most likely to be used in automatic testing or in early development of an API-depending project.

Installation

$ npm install mocked-api --save-dev

Usage

Configuration

To get things running (to use it in your test-suite for example), initialize it like this:

import MockedApi from 'mocked-api';

const api = MockedApi.setup({
  port: 3000, // port where you want the API to run on
  dir: './mocks' // directory where your JSON-files live
});

api.start().then() => {
  console.log('API ready')
});

You can do this once (like in a script that's running your test-suite). The server will keep running until that process is killed, so you don't have to setup the server for every single test.

JSON-files

Place JSON-files in the configured directory to accomodate the responses. You can use nested directories to simulate a path-hierarchy. For example, the file at ./mocks/content/article/42.json will be served at http://localhost:3000/content/article/42.json for the configuration above. If your API prohibits you from adding an extension, either create a file with or without an extension and it will be resolved.

Custom responses

Once initialized, you can mutate responses with the following methods:

import { api } from 'mocked-api';

api
  .respondTo(path) // Defines the path you're about to change
  .andReplace(pointer, value) // Replace a property in your JSON-file. This is based on JSON-pointers, described in [RFC 6901](https://tools.ietf.org/html/rfc6901).
  .withStatus(status) // Custom status-code (`200`, `404`, `500`, etc)

api
  .onResponse((status, body) => {}) // Callback fired for every request, handy for debugging
  .reset() // Removes _all_ custom mutations

You can use these methods to make small changes in a response and test your UI for every little variation that you make. This way your tests can be small and specific, and still cover a lot of edge-cases.

The following example is based on mocha/chai/jsdom, but you can use it similarly in other environments:

import { api } from 'mocked-api';

describe('article', () => {
  beforeEach(() => {
    // Don't forget to reset the API if
    // you're overriding responses:
    api.reset();
  });

  describe('title', () => {
    describe('when it has a title', () => {
      it('renders the title', () => {
        api
          .respondTo('/content/article/42.json')
          .andReplace('/title', 'test title');

        return browser
          .go(`${baseUrl}/article/42`)
          .then(window => {
            expect(window.$('#title').text()).to.equal('test title');
          });
      });
    });

    describe('when it has no title', () => {
      it('renders no title', () => {
        api
          .respondTo('/content/article/42.json')
          .andReplace('/title', null);

        return browser
          .go(`${baseUrl}/article/42`)
          .then(window => {
            expect(window.$('#title').length).to.equal(0);
          });
      });
    });
  });
});

Two API's at the same time

You can run multiple instances of MockedApi simultaneously. For that, pass it a name when you setup each:

import MockedApi from 'mocked-api';
const userApi = MockedApi.setup({ name: 'user', port: 3000, dir: './mocks/user' });
const blogApi = MockedApi.setup({ name: 'blog', port: 3001, dir: './mocks/blog' });

Anytime you need one of those API's to override, make sure you use that same name:

import MockedApi from 'mocked-api';
const userApi = MockedApi.getByName('user');

describe('user', () => {
  beforeEach(() => userApi.reset());

  describe('when logged in', () => {
    it('shows avatar', () => {
      const kitty = 'http://placekitten.com/200/300';

      userApi
        .respondTo('/me')
        .andReplace('/image/src', kitty);

      return browser
        .go('/')
        .then(window => {
          expect(window.$('#avatar').attr('src')).to.equal(kitty);
        });
    });
  });

  describe('when not logged in', () => {
    it('does not show avatar', () => {
      userApi
        .respondTo('/me')
        .withStatus(401)

      return browser
        .go('/')
        .then(window => {
          expect(window.$('#avatar').length).to.equal(0);
        });
    });
  });
});

Standalone

You can also run the server standalone, from your CLI:

$ node_modules/.bin/mocked-api --port 6000 --dir ./mocks

This will serve JSON-files in ./mocks at localhost:6000, but that's it. Custom mutation of responses is not possible through the CLI.

Notes

  • POST/PUT/DELETE are not yet implemented. If you need one of those, please ping me and give me some time to work it out.
  • Don't let anyone tell you different: Setting up a test-suite in the javascript world is not always super duper easy. Let me know if you're having problems with MockedApi!