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

fetchmock

v1.0.1

Published

window.fetch mock object inspired by Angular.js $httpBackend mock.

Downloads

65

Readme

fetchmock Build Status

window.fetch mock object inspired by Angular.js $httpBackend mock.

Install

npm install fetchmock

Note: Requires ES6 promises (you can install them via bower or npm npm install es6-promise or bower install es6-promise)

API

Note: fetchmock substitutes window.fetch with a mocked implementation of fetch for testing. Once you include this file the real fetch will be lost.

FetchMock

FetchMock is available at window and can be used to setup the mocks with expectations.

expect

expect is where the magic of FetchMock happens. You can add expectations, that is, a request you are expecting to be made by the client. This method does not have any side effect, just adds the expectation to the list of expectations. To trigger the validation of expectations you should use flush.

/**
 * Establishes a request expectation and the result it will be returned from it
 * @param {string} method Method of the request
 * @param {RegExp} pattern URL pattern to match the request
 * @param {Object} result Object with all the result data
 * @param {boolean} json Encode body as JSON
 */

The result object passed to expect can have 3 items:

  • status (Number and mandatory): status of the response
  • body (Object or String and mandatory): body of the response
  • headers (Object and optional): headers of the response

Example:

describe('a GET request to /users', function () {
  it('should be made', function () {
    // Our expectation
    FetchMock.expect('GET', /\/users$/, {
      status: 200,
      body: {
        users: ['Hugh', 'Chuck', 'Delilah']
      },
      headers: {
        'Content-Type': 'application/json'
      }
    }, true);

    // function that calls fetch on /users
    requestUsers();

    // When we call flush all the requests are resolved (and thus, the content of the promises is fulfilled)
    // and the expectations are matched. If any request is not flushed (because it matches no expectation)
    // or an expectation is not matched by any request
    // this method will throw an error
    FetchMock.flush();
  });
});

expectGET

expectPOST

expectPUT

expectPATCH

expectDELETE

expectHEAD

These are shorthand methods for FetchMock.expect('MY METHOD', ...).

Example:

describe('a GET request to /users', function () {
  it('should be made', function () {
    FetchMock.expectGET(/\/users$/, {
      status: 200,
      body: {
        users: ['Hugh', 'Chuck', 'Delilah']
      },
      headers: {
        'Content-Type': 'application/json'
      }
    }, true);

    requestUsers();

    FetchMock.flush();
  });
});

reset

Resets all the requests and expectations.

describe('a GET request to /users', function () {
  afterAll(function () {
    // Reset requests and expectations after all tests are executed
    FetchMock.reset();
  });

  it('should be made', function () {
    FetchMock.expectGET(/\/users$/, {
      status: 200,
      body: {
        users: ['Hugh', 'Chuck', 'Delilah']
      }
    }, true);

    requestUsers();
    FetchMock.flush();
  });
});

flush

Flushes all the requests. Iterates over all the requests made with fetch and tries to match them with the expectations. If a request matches an expectation the request will be resolved or rejected with the content of the result passed to the expectation. If any request is not flushed (because it matches no expectation) or an expectation is not matched by any request this method will throw an error. When expectations are matched they are in reverse add order, that is, if you add an expectations for /users/ and then an expectation for /users/list/ only the last one will be matched.

You can see examples of usage of flush in previous examples.