Addon Mock (community version)
A storybook addon to mock fetch/XHR request
[!NOTE] This is a fork maintained by the community to provide support for Storybook 10 and beyond. This addon was originally developed by Linear Labs.
This addon allows you to mock fetch or XMLHttprequest requests in storybook. If your component depends on backend requests, and your backend requests are not ready yet to feed your component, this addon provides mock response to build your component.
Purpose
There are few packages those help the developers to mock the backend requests while building components. But those packages aren't integrated properly in storybook and also there's no scope to play with those requests in the storybook. Mostly, there's no playground to modify the response and see the changes in the storybook.
Highlights
storybook-addon-mock provides the following features.
Installation
This addon requires Storybook 11. Run the following command to install the addon and register it in your Storybook configuration:
npx storybook add @storybook-community/storybook-addon-mock
Manual installation
Install the addon:
npm i -D @storybook-community/storybook-addon-mock
Register it in your .storybook/main.ts file:
// .storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs-vite)
import { defineMain } from '@storybook/your-framework/node';
export default defineMain({
// ...rest of config
addons: [
// ...other addons
'@storybook-community/storybook-addon-mock',
],
});
Then register the addon's preview annotations in your .storybook/preview.ts file:
// .storybook/preview.ts
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs-vite)
import { definePreview } from '@storybook/your-framework';
import addonMock from '@storybook-community/storybook-addon-mock';
export default definePreview({
// ...rest of preview
addons: [addonMock()],
});
Previous Storybook versions
- Storybook 10: use version 10.x of this addon
- Storybook 9 and earlier: use
storybook-addon-mockinstead
Documentation
Older(2.*) version documentation
License
This project is licensed under the MIT License - see the LICENSE file in the source code for details.
