Revise mocking setup instructions for React Native - #529
Revise mocking setup instructions for React Native#529coolsoftwaretyler wants to merge 5 commits into
Conversation
Updated the instructions for enabling mocking in React Native applications to include initialization before rendering the app component.
|
@coolsoftwaretyler is attempting to deploy a commit to the MSW Team on Vercel. A member of the Team first needs to authorize it. |
coolsoftwaretyler
left a comment
There was a problem hiding this comment.
Self review to make a couple quick edits
|
Thanks @coolsoftwaretyler. React Native integration certainly needs some love but all the pieces to make it work should already be there. Regarding no |
|
Hey @kettanaito - that still doesn't work, unfortunately. Any CleanShot.2026-07-20.at.09.50.54.mp4I'll see if I can either figure out what's going on with |
|
Hey @kettanaito - I am fairly positive we can't reliably await before registering a root component. React Native calls I think MSW/React Native directions may need one of two things:
I think updating the docs here is an easier path forward, but I'll keep trying a few things to see if I can iron it out. |
|
@coolsoftwaretyler, what if we keep the promise but drop function enableMocking() {
import('msw/node').then(({ setupServer }) => {
const server = setupServer()
server.use(...handlers)
server.listen()
})
}
enableMocking().then(() => registerRootComponent(App))Almost any promise-based compromise would be better than asking people to embed MSW into their component state. |
|
@kettanaito - that works, but it seems to require disabling lazy loading in Metro. I have to do: Otherwise I get errors like: This leaves us with two options:
|
Updated the React Native integration documentation to improve clarity on enabling mocking and polyfills.
| import "web-streams-polyfill/dist/polyfill" | ||
|
|
||
| if (typeof globalThis.MessageEvent === "undefined") { | ||
| globalThis.MessageEvent = class MessageEvent { | ||
| constructor(type, init) { | ||
| this.type = type | ||
| this.data = init?.data ?? null | ||
| this.origin = init?.origin ?? "" | ||
| this.lastEventId = init?.lastEventId ?? "" | ||
| this.source = init?.source ?? null | ||
| this.ports = init?.ports ?? [] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (typeof globalThis.BroadcastChannel === "undefined") { | ||
| globalThis.BroadcastChannel = class BroadcastChannel { | ||
| constructor(_name) {} | ||
| postMessage() {} | ||
| close() {} | ||
| addEventListener() {} | ||
| removeEventListener() {} | ||
| dispatchEvent() { | ||
| return true | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
note: leftover requirements from mswjs/msw#2367 (comment), which has been partially resolved
| ## Setup | ||
|
|
||
| Import the `setupServer` function from `msw/native` and call it, providing your request handlers as the argument. | ||
| ## Enable mocking |
There was a problem hiding this comment.
note: I collapsed this section down for a simpler code example but we could always expand it
|
@kettanaito - just got back from a work trip and incorporated some of this feedback. These docs updates are based on an example PR I have in an Ignite app |
Hey folks - love MSW. I recently used it on a large React Native project to some success and I'm trying to document some of the pain points of React Native integration (see this example PR integrating with Ignite: infinitered/mswtest#2).
I'm not exactly sure what the mechanism is, but if you
awaitanything before callingregisterRootComponent, Expo will fail to register the app entry. I have a demo of the issue hereI propose using some check at the top of the app tree, although another good approach might be to delay hiding the splash screen