Storybook - mocking API calls


How to mock API calls for your components with Storybook MSW addon

26 Nov, 2022 · 5 min read

Sometimes, our components might call API endpoints, which could add unnecessary load to our Storybook side of things.

In those cases, we might want to intercept those calls and mock them instead of returning them.

And luckily for us, Storybook has a mocking addon we can use for this.

Setting up the loader

I will add a super simple data loader to the demo stories/Page.jsx file.

It’s simply to demo the use case, so I’ll be adding a data fetching mechanism like so:

export const Page = () => {
  const [user, setUser] = React.useState();
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(false);

  useEffect(() => {
      .then((response) => response.json())
      .then((formattedData) => {
      .catch(() => {
  }, []);

  if (loading) {
    return <p>loading...</p>;

  if (error) {
    return <p>Something went wrong, try again later</p>;

  return (
        onLogin={() => setUser({ name: 'Jane Doe' })}
        onLogout={() => setUser(undefined)}
        onCreateAccount={() => setUser({ name: 'Jane Doe' })}

        <h2>Pages in Storybook</h2>
          { => {
            return <li key={}>{post.title}</li>;

This will call the API and render a list of post titles. When it fails, it shows an error message.

As you can see, this would call every single time we call this story.

So let’s see how we can mock this instead!

Installing the MSW addon

As mentioned, we’ll be using the MSW addon, which stands for: “Mock Service Worker”, it adds a service worker that will intercept and mock our requests.

To install it, run the following command.

npm i msw msw-storybook-addon -D

Then we’ll need to initialize the service worker:

npx msw init public/

Now you’ll need to head over to the .storybook/preview.js file and add the following lines.

import { initialize, mswDecorator } from 'msw-storybook-addon';

// Initialize MSW

// Provide the MSW addon decorator globally
export const decorators = [mswDecorator];

And now we’ll be able to mock specific requests in our stories.

Mocking on component level

Let’s start by mocking on a component level. This is common as we will have multiple stories that need the same mock.

As we learned before, we can use the parameters option and mock the call we have.

export default {
  title: 'Example/Page',
  component: Page,
  parameters: {
    msw: [
        (_req, res, ctx) => {
          return res(
                userId: 1,
                id: 1,
                  'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
                body: 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto',

If we run our story, we can see the component will render only one item. That’s always the one we describe here.

Mocked API request

Mocking a fail

We might also want to test what happens when the API returns an error, and for this, we can use a per-story option.

Let’s create a new story, so we have it separate from the existing ones.

export const FailedResponse = Template.bind({});
FailedResponse.parameters = {
  msw: [
    rest.get('', (_req, res, ctx) => {
      return res(ctx.delay(800), ctx.status(403));

And if we open that story, we see the loading text for 800 milliseconds, after which the error is presented.

Mock error status

As you can see, this per-story idea overwrites our main component loading.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Storybook - Additional integrations

29 Nov, 2022 · 2 min read

Storybook - Additional integrations

Storybook - Examples

28 Nov, 2022 · 2 min read

Storybook - Examples

Join 2099 devs and subscribe to my newsletter