MSW is not intercepting in development in nextjs and graphql api #1049
-
|
Hey, https://github.com/themashcodee/msw-nextjs-graphql-testing |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 15 replies
-
|
Hey, @themashcodee. Thanks for reporting this. So the query is not intercepted because by the time it happens the worker is not done registering. I've spotted this by debugging the Lines 261 to 266 in 527add2 As the condition above states, if there are no clients (tabs where the worker is ready), bypass the request. That's precisely what's happening. The race condition between the worker registration and whichever requests an app may perform is still something we're working on solving. Currently, there's the request deferring logic that should capture and replay any requests happening in that race condition timeframe. That's clearly not what's happening in your case. At first, I suspected the custom For now, what I did is deferred the app's mounting (as described in the Deferred mounting page) so that your app mounts after the worker is ready: import 'styles/globals.css'
import type { AppProps } from 'next/app'
import { Apollo } from 'services/apollo-client'
import { useEffect, useState } from 'react'
const { API_MOCKING } = process.env
const MyApp = ({ Component, pageProps: pageProps }: AppProps) => {
const [shouldRender, setShouldRender] = useState(!API_MOCKING)
useEffect(() => {
async function initMocks() {
const { setupMocks } = await import('mocks')
await setupMocks()
setShouldRender(true)
}
if (API_MOCKING) {
initMocks()
}
}, [])
if (!shouldRender) {
return null
}
return (
<>
<Apollo>
<Component {...pageProps} />
</Apollo>
</>
)
}
export default MyAppI'd much prefer for this to be handled by the library but for the lack of such handling at the moment this is what I'd recommend you try. |
Beta Was this translation helpful? Give feedback.
-
|
I'm using |
Beta Was this translation helpful? Give feedback.
-
|
Still a big issue in 14 version! |
Beta Was this translation helpful? Give feedback.
-
|
still a problem for "next": "16.1.0", doesn't works with hmr. |
Beta Was this translation helpful? Give feedback.
-
UpdateFor anyone still experiencing this in modern versions of Next.js, the framework doesn't work well with MSW as it lacks proper entrypoints to be integrated with. There is nothing we can do on MSW's side and this rests entirely on the Next.js team. I trust they do their best and address issues to their best availability. It doesn't help sharing that you're still experiencing the issue. It might help bringing this up to the Next.js team's attention to show that there are users affected by this. If this ever gets solved, the mswjs/examples#101 example will be updated. Please follow any progress here. |
Beta Was this translation helpful? Give feedback.
Hey, @themashcodee. Thanks for reporting this.
So the query is not intercepted because by the time it happens the worker is not done registering. I've spotted this by debugging the
activeClientsIdsin themockServiceWorker.jsfile:msw/src/mockServiceWorker.js
Lines 261 to 266 in 527add2
As the condition above states, if there are no clients (tabs where the worker is ready), bypass the request. That's precisely wh…