Skip to content

Instantly share code, notes, and snippets.

@twhitbeck
Created October 14, 2022 10:07
Show Gist options
  • Save twhitbeck/e006f4b1f97222156555d91bb85cd844 to your computer and use it in GitHub Desktop.
Save twhitbeck/e006f4b1f97222156555d91bb85cd844 to your computer and use it in GitHub Desktop.
An example of using `msw` with ladle
import { setupWorker, rest } from "msw";
export const ExampleStory = () => {
...
};
ExampleStory.decorators = [
(Story) => {
const [ready, setReady] = React.useState(false);
React.useEffect(() => {
const worker = setupWorker(
rest.get("/fake", (req, res, ctx) => {
// define your handler
})
);
worker.start().then(() => {
setReady(true);
});
}, []);
if (!ready) {
return <>Loading...</>;
}
return <Story />;
},
];
@parzonka
Copy link

parzonka commented Dec 5, 2022

Hi @twhitbeck, thanks, for the useful example!

I got it running using npx ladle build and npx ladle preview, but I have to add the mockServiceWorker.js after each build to the ./build directory, which can be automatized.

Did you got it working with npx ladle serve ? I tried to add the worker script to different locations like vite, public etc. but it was still not found. In case you have found a solution you could drop a note here, thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment