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

pistolet

v1.0.0-alpha.8

Published

[![Build Status](https://travis-ci.org/FrenchHipster/pistolet.svg?branch=master)](https://travis-ci.org/FrenchHipster/pistolet) [![npm version](https://badge.fury.io/js/pistolet.svg)](https://badge.fury.io/js/pistolet) [![lerna](https://img.shields.io/bad

Downloads

24

Readme

Pistolet

Build Status npm version lerna

Pistolet (pronounced pistol-eh) is a Javascript testing tool to create mock API responses.

Installation

$ npm install pistolet --save-dev

Available Plugins

Pistolet does not come with any integration out-of-the-box, but has plugins to do so:

General Usage

describe('your test suite', () => {
  beforeAll(() => {
    // Start Pisolet, once per test suite
    server = new Pistolet([
      'scenario/from/json/file',
      new ClassScenario(),
      objectScenario
    ]);
  });

  // Required, otherwise the server keeps running and occupies the TCP port
  afterAll(() => server.stop());

  // Clears the request history, and resets scenarios (in case they are stateful)
  afterEach(() => server.reset());
});

Note: This example uses Jasmine, although this is not a requirement.
You are more than welcome to use it with other test suites, and contributing examples would be greatly appreciated.

Override existing scenarios

Pistolet allows to override the scenarios previously defined.
These overrides have a shorter lifespan and will be discarded after calling reset().

beforeAll(() => server = new Pistolet(['default/scenario']));

it('should test an edge case', () => {
  server.override('additional/scenario');
});

Writing scenarios

JSON Files

JSON scenarios are the easiest to write and use.

Make sure to set the dir property in the configuration for Pistolet to know where to find these files.

{
  "name": "The name is purely optional, for developers convenience",
  "request": {
    "method": "GET",
    "path": "/your/api/path"
  },
  "response": {
    "status": 200,
    "data": {
      "some": "response"
    }
  }
}

Javascript/Typescript Scenarios

import { Mock, Request, Response, Scenario } from 'pistolet';

export const SampleObjectScenario: Scenario = {
  mocks: [/* ... */],
  next(request: Request, response: Response, match: Mock) {
    // Simply accept and send the mock
    response.status(200).send(match);
    return true;
  }
};

export class SampleClassScenario implements Scenario {
  mocks: Mock[] = [/* ... */];
  next(request: Request, response: Response, match: Mock) {
    // Wait a second, and fail
    setTimeout(() => {
      response.status(503).send({ errorMessage: 'Some Error' });
    }, 1000);
    return true;
  }
}

At the end of next(), you can return:

  • A Mock object (same type as the match parameter), which will be sent immediately
  • A Promise<Mock> object, which will be sent when the promise resolves
  • true, to indicate the scenario has a custom logic and will handle the response
  • false or undefined, to indicate that there was no match

JSON format

URL vs path & query

URL matching will perform a simple string comparison:

{
  "method": "GET",
  "url": "/api/search?q=criteria"
}

You can also use path and query to the same effect:

{
  "method": "GET",
  "path": "/api/search",
  "query": {
    "q": "criteria"
  }
}

Using query parameters will perform an object comparison, which is not sensitive to the order in which the parameters come in the URL.

URLs, paths and query parameters can use regular expressions:

{
  "method": "GET",
  "path": "/\\/api\\/search\\/\\w+/g",
  "query": {
    "q": "/\\w+/g"
  }
}

Delayed responses

Pistolet will return the reponse after delay milliseconds if the property is present:

{
  "response": {
    "delay": 200,
    "data": { "delayed": "response", "after": "200ms" }
  }
}