Skip to content

feat!: new addon architecture#182

Draft
kettanaito wants to merge 7 commits intomainfrom
feat/v3
Draft

feat!: new addon architecture#182
kettanaito wants to merge 7 commits intomainfrom
feat/v3

Conversation

@kettanaito
Copy link
Copy Markdown
Member

@kettanaito kettanaito commented Apr 2, 2026

Motivation

Rewrites the addon to be compatible and adhere to the best practices of Storybook addons.

Issues

Changes

  • Changes LICENSE.md. I'm implementing the new architecture from scratch and listing myself as the author, respectively. The old addon versions retain the old license with all the appropriate attributions.

Architecture

The addon is now used by importing the enableMocking function and calling it in your Storybook's addons array:

// .storybook/preview.ts
import { definePreview } from '@storybook/react-vite'
import { enableMocking } from '@msw/storybook'
import { http, HttpResponse } from 'msw'

export default definePreview({
  //       👇 Register the addon.
  addons: [enableMocking()],
  //           👇 Type-safe `msw` key.
  beforeEach({ msw }) {
    // 👇 Provide global handlers, if you want.
    msw.use(
      http.get('https://api.example.com/user', () => {
        return HttpResponse.json({ name: 'John Maverick' })
      }),
    )
  },
})

That's it. No need to initialize anything or manage worker/server instances. Everything is provisioned automatically for you with optimal defaults.

You can customize the MSW API created via enableMocking by providing it a custom setup function that returns any setup* instance:

enableMocking(async () => {
  const { setupWorker } = await import('msw/browser')
  const worker = setupWorker()
  await worker.start(CUSTOM_OPTIONS)
  return worker
})

Per-story handlers

You can add request handlers for individual stories:

// stories/homepage.stories.tsx
export const HomepageStory: Story = {
  beforeEach({ msw }) {
    msw.use(
      http.get('https://api.example.com/user', () => {
        return HttpResponse.json({ name: 'Alice Sunwell' })
      }),
    )
  },

Internal

  • Simplifies the repository structure. Drop the monorepo. Drop the docs and its deployment.
  • Migrates to modern tooling (pnpm, tsdown, Vitest).
  • Adds meaningful tests:
    • Stories tests that run on a controlled Storybook setup;
    • Play function tests via stories + Vitest.

Todos

  • Document the usage (README.md).
  • Add type testing to make sure using the addon extends global Storybook types correctly.
  • Set storybook.icon.
    • Deploy a *.png logo to MSW website, use that URL as the value.
  • Set the correct version in package.json so this releases properly.
  • Adds a release job. Run pnpm lint before publishing.
    • Align on the release model with @yannbf
  • Add a story test that provides a custom setup function to enableMocking in .storybook/preview.ts.
  • Drop support for Node.js.
  • Export addon function as default.
  • Support CSF 3 (/preview).
  • Consider a function to merge handlers from parameters.msw for easier migration.

Questions

  • Do we even need the support for msw/node? Both Storybook and its play functions run in the browser (via VBM).

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
msw-storybook-addon Error Error Apr 8, 2026 5:21pm

Request Review

@kettanaito kettanaito requested a review from yannbf April 2, 2026 15:52
@yannbf
Copy link
Copy Markdown
Collaborator

yannbf commented Apr 2, 2026

Great stuff @kettanaito!! I think we can/should drop support for node. This is definitely a breaking change for users who use the portable stories concept with Jest or Vitest sans-browser mode, but honestly the amount of affected users will be small in comparison to the ones that adopted new testing capabilities of Storybook + Vitest browser mode.

It would be amazing to have https://github.com/stackblitz-labs/pkg.pr.new set up for canary releases.

I'm overall quite excited to see this getting released, and I love to see msw being part of a story context so it's super easy for people to use it! We need to make sure types work well.

Let's chat in detail next week!

@kettanaito
Copy link
Copy Markdown
Member Author

Thanks for a quick review, @yannbf!

This is going to be a breaking change regardless so it's a great time to drop msw/node if it's no longer needed.

I will configure pkg-pr in a follow-up, let's keep this branch focused on the public APIs and tests.

@binarykitchen
Copy link
Copy Markdown

That's a great PR.

My public library is broken on Storybook level because of this error

Cannot set properties of undefined (setting 'activationPromise')

Source:
https://github.com/binarykitchen/videomail-client/blob/master/.storybook/preview.ts

image

(AI suggested a workaround I'm not happy with but have put in a separate branch https://github.com/binarykitchen/videomail-client/blob/storybook-preview-fix/.storybook/preview.ts)

@kettanaito
Copy link
Copy Markdown
Member Author

@binarykitchen, have you tried this branch? There's no more activationPromise involved so your issue seems to be related to the current version of the add-on. In that case, consult the readme and see what you are missing.

@binarykitchen
Copy link
Copy Markdown

binarykitchen commented Apr 7, 2026 via email

@kettanaito
Copy link
Copy Markdown
Member Author

@binarykitchen, I see what's happening. msw@2.13.0 has propagated to you as a part of the install and it broke the internals of the current version of the addon (#183). I was confused about your comment being on a WIP pull request that has nothing to do with either of those things.

Please follow #183 for updates on the issue. I will look into it once I have a moment. For now, you can pin msw to 2.12.x.

@binarykitchen
Copy link
Copy Markdown

Apologies for posting here @kettanaito - all clear now, thanks

Comment thread src/index.ts Outdated
Copy link
Copy Markdown

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome & very clean!!! Love the new version. Small comments below.

Would love to see a way for existing users to migrate cleanly to the new version, whether it's a codemod, prompt, or a backwards compatibility code snippet that they can paste into their .storybook/preview.ts file

Comment thread tests/base/.storybook/preview.ts Outdated
* fix: remove node.js support

* fix: export addon as the default export

* fix: support csf 3.0
@kettanaito kettanaito requested review from shilman and yannbf April 8, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants