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

storybook-msw-addon

v2.1.7

Published

An MSW (Mock Service Worker) addon including a control panel that enables interaction and manipulation of mock requests within Storybook. MSW 2.3.3 + Storybook 8

Downloads

28,600

Readme

Release

An MSW (Mock Service Worker) addon including a control panel that enables interaction and manipulation of mock requests within Storybook.

Features

  • Up to date with current MSW version (2.1.0)

Installing and Setup

Install MSW and the addon

With npm:

npm i msw storybook-msw-addon -D

Or with yarn:

yarn add msw storybook-msw-addon -D

Generate service worker for MSW in a project folder.

Keep in mind this influences the paths you can use for your api calls.

npx msw init ${path to project folder to be initialized by msw} --save

For example, the following command will init a service worker inside the api folder of your project.

npx msw init ./api --save

When running Storybook, you have to serve the folder where you have init the MSW service worker as an asset to Storybook. Refer to the docs if needed.

Add the addon to your project

Add the addon to the storybook config in ./storybook/main.js:

addons: [
    ...,
    "storybook-msw-addon",
  ],

Configure the addon

Enable MSW in Storybook by initializing MSW and providing the MSW loader in ./storybook/preview.js:

import { initialize, mswLoader } from "storybook-msw-addon";

// Initialize MSW
initialize();

// Provide the MSW addon loader globally. A loader runs before a story renders, avoiding potential race conditions.
export const loaders = [mswLoader];

Start Storybook

Remember to serve the public folder, or the path where you have init the MSW service worker

npm run storybook

Usage

The pass request handlers (https://mswjs.io/docs/basics/request-handler) into the handlers property of the msw parameter. This is commonly an array of handlers.

import { http, HttpResponse } from "msw";

export const SuccessBehavior = () => <UserProfile />;

SuccessBehavior.parameters = {
  msw: {
    handlers: [
      http.get(endpoint, () => {
        return HttpResponse.json({ results: results });
      }),
    ],
  },
};

Advanced Usage

WIP -- Advanced use cases are currently being test with the current version of this addon.

Configuring MSW

If you want to configure storybook-msw--addon, you can pass options to the initialize function.

For example, if you want MSW to bypass unhandled requests you can initialize the addon with the onUnhandledRequest: "bypass" option:

// preview.js
import { initialize } from "storybook-msw-addon";

initialize({
  onUnhandledRequest: "bypass",
});

Development scripts

  • yarn start runs babel in watch mode and starts Storybook
  • yarn build build and package your addon code

Acknowledgments

This addon was inspired both by msw-storybook-addon and storybook-addon-mock, motivating us to combine features of both addons in one place.