Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,33 @@ describe('query', () => {
unsubscribe()
})

it('fetch should refetch successfully after the previous fetch errored', async () => {
const key = queryKey()

let shouldFail = true
const queryFn = vi.fn(() => {
if (shouldFail) {
return sleep(10).then(() => Promise.reject(new Error('fail')))
}
return sleep(10).then(() => 'success')
})

queryClient.prefetchQuery({ queryKey: key, queryFn, retry: false })
await vi.advanceTimersByTimeAsync(10)

const query = queryCache.find<string>({ queryKey: key })!
expect(queryFn).toHaveBeenCalledTimes(1)
expect(query.state.status).toBe('error')

shouldFail = false
const refetchPromise = query.fetch({ queryKey: key, queryFn, retry: false })
await vi.advanceTimersByTimeAsync(10)

await expect(refetchPromise).resolves.toBe('success')
expect(queryFn).toHaveBeenCalledTimes(2)
expect(query.state.status).toBe('success')
})

it('fetch should throw an error if the queryFn is not defined', async () => {
const key = queryKey()

Expand Down