Mock api calls from Storybook Mock api calls from Storybook reactjs reactjs

Mock api calls from Storybook


xhr-mock should work for local testing where you probably don't want to make requests across the internet.

Outside of testing, if you are waiting on the real endpoints to be built you could use Mock/it (https://mockit.io) in development. You can claim your own dedicated subdomain and swap it out later for the real one. Disclaimer: this is a side project I recently released and would love any feedback on it!


You can use xhr-mock instead of axios-mock-adapter.

Utility for mocking XMLHttpRequest.

Great for testing. Great for prototyping while your backend is still being built.

Works in NodeJS and in the browser. Is compatible with Axios, jQuery, Superagent >and probably every other library built on XMLHttpRequest

import mock from 'xhr-mock';storiesOf("My Component", module).addWithInfo("Simulator",() => {    mock.post('', {      status: 500,      body: '{}'    });    return <MyComponent />    },    {})

Additionaly you need to add jquery script in preview-head.html file in storybook

1) https://www.npmjs.com/package/xhr-mock


I've started using json-server to intercept API calls. You have to start it in one tab, and start storybook in another, but it is pretty cool.