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

with-fetch-mock

v0.1.0

Published

Library for mocking requests in scoped manner

Downloads

2

Readme

This is a wrapper around fetch-mock which allows to set up expectations for api calls more easily.

Features overview:

  • Returns a promise which can used to block test from finishing early.
  • Captures thrown errors to be used as a reason for test failure.
  • Captures every request to assist in debugging.
  • Request matching is performed through assertions.
  • Supports async functions in scenario and mocks.
  • Supports many shortcuts carried over from fetch-mock.

Usage

Import the wrapper:

const withMock = require('with-fetch-mock');

withMock returns a promise and accepts following arguments:

  • scenario callback which makes requests and verifies responses
  • one or more mock which can be function or a value

Scenario can be an asyn

Promise returned by withMock will not fulfill until:

  • All declared mocks have been called
  • Promises returned by scenario and mock callbacks have been fulfilled

Mocks can be declared as one of the following forms:

  • number which will be used as the status code of the response with empty body
  • string which will be used as the body of the response with status code 200
  • object with magical properties which will be used to create a Response object:
    • body response body
    • status response status code
    • headers response headers
    • throws a rejection reason for the promise returned to fetch call
    • sendAsJson can be set to false in order to prevent body from being forcefully converted to string
  • object without magical properties which will be converted to string and used as the response body
  • Response which will be resolved to from the Promise returned to the fetch call
  • Promise which resolves to one of the values described above
  • function which returns one of the values described above

Arguments supplied to fetch will be passed to the mock function:

  • input request url (string)
  • init request settings (object)
    • method the request method (string)
    • headers request headers (object)
    • body request body (string, Blob, or FormData)
    • and other request settings...

Intended usage

Return the promise created by withMock to prevent your tests from finishing early and to ensure that meaningful failure messages will be produced in response to assertion errors inside the scenario or mock callbacks.

Any callback may return a promise or be declared as an async function. Resolution of the top level promise will be delayed until promise returned by callback is fulfilled. Callback promise rejection will be propagated to the top level promise.

Example

it('must make some requests', () => {
    return withMock(
        async () => {
            const posts = await fetch('/posts/18')
                .then(response => response.json());

            expect(posts).toEqual({
                title: 'Some random post',
            });

            const response = await fetch('/posts', {
                method: 'POST',
                body: JSON.stringify({
                    title: 'A work of art',
                }),
            });

            expect(response.status).toBe(201);
        },
        (url) => {
            expect(url).toEqual('/posts/18');

            return { title: 'Some random post' };
        },
        (url, options) => {
            expect(url).toEqual('/posts');
            expect(options.method).toEqual('POST');
            expect(options.body).toEqual(
                JSON.stringify({
                    title: 'A work of art',
                })
            );

            return 201;
        }
    )
});