-
Bug reportDescription / Observed BehaviorThis bug arises from the interaction of MSW, Jest, and SWR, so I'm not positive that it's an issue in SWR. But (part of) it manifests as an SWR error, so I'll start by logging it here. A unit test that uses MSW to mock responses for a component that uses SWR may log the following errors on shutdown.
Expected BehaviorNo errors on shutdown. Repro Steps / Code Examplehttps://github.com/joshkel/swr-msw-jest-bug Clone, run Additional ContextFrom what I can tell, here's the sequence of events:
It seems to me that SWR should arguably skip any on-error-retry logic at this point, since it's been unmounted? SWR version: 2.0.4 This problem started when upgrading from MSW 1.0.1 to 1.1.0. If I should raise an issue there instead, or in addition to, this project, please let me know. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 5 replies
-
@koba04 , I see that you moved this into a discussion, but it seems to me that it's a bug within SWR? If all useSWR hooks have been unmounted, I don't believe it should be triggering onErrorRetry at all. |
Beta Was this translation helpful? Give feedback.
-
We're reading |
Beta Was this translation helpful? Give feedback.
-
Commenting here that I've raised a PR that addresses these issues that I've also been experiencing. Hopefully we can find a way to land these changes. |
Beta Was this translation helpful? Give feedback.
-
Upvote, I have the same issue |
Beta Was this translation helpful? Give feedback.
-
You could provide your own isVisible implementation for testing environment. /** @jest-environment jsdom */
import * as React from 'react'
import { Hello } from './test'
import { fireEvent, render, screen } from '@testing-library/react'
import { rest } from 'msw'
import { setupServer } from 'msw/node'
import { SWRConfig } from 'swr'
const renderWithSWRConfig = (component) => {
const map = new Map()
return render(
<SWRConfig
value={{
provider: () => map,
isOnline() {
/* Customize the network state detector */
return true
},
isVisible() {
/* Customize the visibility state detector */
return true
},
initFocus(callback) {},
initReconnect(callback) {},
}}
>
{component}
</SWRConfig>
)
}
const server = setupServer(
rest.get('/example', (req, res, ctx) => {
console.log('hello')
return res(ctx.json('hello world'))
})
)
server.listen()
afterAll(() => server.close())
test('hello world', async () => {
renderWithSWRConfig(<Hello />)
const textbox = screen.getByRole('textbox')
fireEvent.change(textbox, { target: { value: 'query' } })
fireEvent.change(textbox, { target: { value: '' } })
fireEvent.blur(textbox)
}) |
Beta Was this translation helpful? Give feedback.
-
@promer94 Thank you so much for that snippet, I have been dealing with this for months and could not figure out how to mock that out! |
Beta Was this translation helpful? Give feedback.
You could provide your own isVisible implementation for testing environment.