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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mock-switch

v0.1.1

Published

A minimalistic UI to enable switching between your prepared mock responses during runtime. Can be used with any client-side mock setup.

Downloads

22

Readme

mock-switch

This package consists of a minimalistic UI and a simple API to integrate with your mock setup. The UI enables you to switch between your prepared mock responses during runtime; where previously you would have had to comment-in and comment-out prepared lines of code in your source file, and rebuild your front-end to switch between different scenarios of mocked responses. This package can be used with any client-side mock setup; no matter if you use mock-service-worker, fetch-mock, xhr-mock, or axios-response-mock.

The mock-switch package is useful when demoing your front-end to show how it behaves in different scenarios and useful during development to trigger all front-end states your component is supposed to handle, but it is not meant to be used with mocks in automated tests which usually run headless anyway, and even when not, are not designed to query for ad-hoc user input.

You do not need to integrate mock-switch with all of your prepared mocks and all scenarios; you can add it selectively where you need the choices to be selectable during runtime.

usage

Add mock-switch to your package.json and install via npm or your preferred package manager. Import it in your mock setup file and call mockSwitch anywhere you want to enable switching between different choices via the UI.

The following example uses a mock-service-worker setup

import { http, HttpResponse } from "msw";
import { mockSwitch } from "mock-switch";

export const handlers = [
  http.get("user/isVerified", ({ request }) => {
    // here we enable switching between different scenarios
    switch (
      mockSwitch("respond to user/isVerified", [
        "verified user",
        "unverified user",
        "bad request",
        "server error",
      ])
    ) {
      case "verified user":
        return HttpResponse.json({ isVerified: true });
      case "unverified user":
        return HttpResponse.json({ isVerified: false });
      case "bad request":
        return new Response(null, { status: 400 });
      case "server error":
        return new Response(null, { status: 500 });
      default:
        return null;
    }
  }),
  http.get("user/isPremiumEnabled", ({ request }) => {
    // here no choices are supported
    return HttpResponse.json({ isPremiumEnabled: true });
  }),
];

The function mockSwitch(qualifier: string, choices: Array<string>) => selectedCoice: string takes two arguments: a qualifier string that identifies the intercepted route in a user readable way, and an array of choices the user can choose from. The selected choice is returned. The function executes and returns synchronously, if the user previously made no decision for the intercepted route the first choice is selected by default. The user can select any choice for any intercepted route in the UI prior to triggering the interaction that would result in the intercepted route. User decisions are persisted in session storage and therefore survive page refresh.

Currently no option to block a request while prompting the user for a decision is offered, but might be added in a future version (this would require an async variant of mockSwitch and would only work with mocking setups that support async resolver functions).

The qualifier and choices do not need to adhere to any syntax; to ensure your user decisions are mapped correctly to the intercepted routes your qualifiers should be distinct (different routes should have different qualifiers); and choices should be unique within the same group only (so it is fine to have a 'bad request' for route A and another 'bad request' for a different route B).

API documentation

Currently this README text is the only available documentation.

contributing feedback

If you think mock-switch is missing a feature or the documentation doesn't answer all your questions please visit the discussions section and provide your feedback. If you found a bug please visit the issues section and check whether your issue has been reported, and if not open a new issue. All contributions are expected to be reasonably formatted using markdown, edited for clarity, and reasonably complete to form the basis of a constructive discussion.