2.0.3 • Published 5 months ago

msw-storybook-addon v2.0.3

Weekly downloads
27,436
License
MIT
Repository
github
Last release
5 months ago

Features

  • Mock Rest and GraphQL requests right inside your story.
  • Document how a component behaves in various scenarios.
  • Get a11y, snapshot and visual tests using other addons for free.

Full Documentation

Quick Start

Install msw & storybook addon.

npm i -D msw msw-storybook-addon

Enable msw on storybook by adding these lines in ./storybook/preview.js

import { addDecorator } from '@storybook/react'
import { initializeWorker, mswDecorator } from 'msw-storybook-addon'

initializeWorker()
addDecorator(mswDecorator)

Generate service worker for msw in your public folder.

npx msw init public/

Refer MSW official guide for framework specific paths.

Start storybook with that public folder.

npx start-storybook -s public -p 6006

Mock API calls in a story.

import { rest } from 'msw'

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

SuccessBehavior.parameters = {
  msw: [
    rest.get('/user', (req, res, ctx) => {
      return res(
        ctx.json({
          firstName: 'Neil',
          lastName: 'Maverick',
        })
      )
    }),
  ],
}

The msw parameter takes an array of handlers as shown in MSW official guide.

2.0.3

5 months ago

2.0.2

8 months ago

2.0.1

8 months ago

2.0.0

8 months ago

2.0.0-beta.2

9 months ago

2.0.0-beta.1

11 months ago

2.0.0-beta.0

12 months ago

1.9.0

1 year ago

1.10.0

1 year ago

2.0.0-next.0

2 years ago

2.0.0-next.1

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago