Skip to content
Merged
Show file tree
Hide file tree
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
76 changes: 38 additions & 38 deletions packages/query-core/src/__tests__/queryClient.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
CancelledError,
MutationObserver,
QueryClient,
QueryObserver,
Expand Down Expand Up @@ -993,54 +994,25 @@ describe('queryClient', () => {
describe('cancelQueries', () => {
test('should revert queries to their previous state', async () => {
const key1 = queryKey()
const key2 = queryKey()
const key3 = queryKey()
await queryClient.fetchQuery({
queryKey: key1,
queryFn: () => 'data',
})
try {
await queryClient.fetchQuery({
queryKey: key2,
queryFn: async () => {
return Promise.reject<unknown>('err')
},
})
} catch {}
queryClient.fetchQuery({
queryClient.setQueryData(key1, 'data')

const pending = queryClient.fetchQuery({
queryKey: key1,
queryFn: () => sleep(1000).then(() => 'data2'),
})
try {
queryClient.fetchQuery({
queryKey: key2,
queryFn: () =>
sleep(1000).then(() => Promise.reject<unknown>('err2')),
})
} catch {}
queryClient.fetchQuery({
queryKey: key3,
queryFn: () => sleep(1000).then(() => 'data3'),
})

await vi.advanceTimersByTimeAsync(10)

await queryClient.cancelQueries()

// with previous data present, imperative fetch should resolve to that data after cancel
await expect(pending).resolves.toBe('data')

const state1 = queryClient.getQueryState(key1)
const state2 = queryClient.getQueryState(key2)
const state3 = queryClient.getQueryState(key3)
expect(state1).toMatchObject({
data: 'data',
status: 'success',
})
expect(state2).toMatchObject({
data: undefined,
error: 'err',
status: 'error',
})
expect(state3).toMatchObject({
data: undefined,
status: 'pending',
fetchStatus: 'idle',
})
})

test('should not revert if revert option is set to false', async () => {
Expand All @@ -1060,6 +1032,34 @@ describe('queryClient', () => {
status: 'error',
})
})

test('should throw CancelledError for imperative methods when initial fetch is cancelled', async () => {
const key = queryKey()

const promise = queryClient.fetchQuery({
queryKey: key,
queryFn: async () => {
await sleep(50)
return 25
},
})

await vi.advanceTimersByTimeAsync(10)

await queryClient.cancelQueries({ queryKey: key })

// we have to reject here because we can't resolve with `undefined`
// the alternative would be a never-ending promise
await expect(promise).rejects.toBeInstanceOf(CancelledError)

// however, the query was correctly reverted to pending state
expect(queryClient.getQueryState(key)).toMatchObject({
status: 'pending',
fetchStatus: 'idle',
data: undefined,
error: null,
})
})
})

describe('refetchQueries', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,12 @@ export class Query<
fetchStatus: 'idle' as const,
})
// transform error into reverted state data
return this.state.data!
// if the initial fetch was cancelled, we have no data, so we have
// to get reject with a CancelledError
if (this.state.data === undefined) {
throw error
}
return this.state.data
Comment on lines -561 to +566
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes makes sure we never resolve with undefined for imperative methods like queryClient.fetchQuery(...). The bang operator ! was a dead giveaway that this could happen 😅

}
}
this.#dispatch({
Expand Down