Created
October 14, 2022 10:07
-
-
Save twhitbeck/e006f4b1f97222156555d91bb85cd844 to your computer and use it in GitHub Desktop.
An example of using `msw` with ladle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />; | |
}, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @twhitbeck, thanks, for the useful example!
I got it running using
npx ladle build
andnpx ladle preview
, but I have to add themockServiceWorker.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 likevite
,public
etc. but it was still not found. In case you have found a solution you could drop a note here, thanks in advance!