diff --git a/docs/framework/react/reference/useQuery.md b/docs/framework/react/reference/useQuery.md index b02775a62cf..569e19cb8f4 100644 --- a/docs/framework/react/reference/useQuery.md +++ b/docs/framework/react/reference/useQuery.md @@ -8,6 +8,7 @@ const { data, dataUpdatedAt, error, + errorUpdateCount, errorUpdatedAt, failureCount, failureReason, diff --git a/docs/framework/solid/reference/useQuery.md b/docs/framework/solid/reference/useQuery.md index 876c5c99729..aaca4469971 100644 --- a/docs/framework/solid/reference/useQuery.md +++ b/docs/framework/solid/reference/useQuery.md @@ -8,6 +8,7 @@ const { data, dataUpdatedAt, error, + errorUpdateCount, errorUpdatedAt, failureCount, failureReason, diff --git a/packages/lit-query/src/tests/infinite-and-options.test.ts b/packages/lit-query/src/tests/infinite-and-options.test.ts index 6ef06b6cdc1..701ea5e34c4 100644 --- a/packages/lit-query/src/tests/infinite-and-options.test.ts +++ b/packages/lit-query/src/tests/infinite-and-options.test.ts @@ -364,7 +364,7 @@ describe('createInfiniteQueryController', () => { const nextPageResult = await infinite.fetchNextPage() expect(nextPageResult.isFetchNextPageError).toBe(true) - expect(nextPageResult.error).toBeInstanceOf(Error) + expect(nextPageResult.error).toEqual(new Error('next-page-failed')) await waitFor(() => infinite().isFetchNextPageError) expect(infinite().data?.pages).toEqual([0]) }) diff --git a/packages/lit-query/src/tests/mutation-controller.test.ts b/packages/lit-query/src/tests/mutation-controller.test.ts index e205ac05018..58ad85a5bf1 100644 --- a/packages/lit-query/src/tests/mutation-controller.test.ts +++ b/packages/lit-query/src/tests/mutation-controller.test.ts @@ -164,7 +164,7 @@ describe('createMutationController', () => { await waitFor(() => mutation().isPending) await expect(errorPromise).rejects.toThrow('negative-not-allowed') await waitFor(() => mutation().isError) - expect(mutation().error).toBeInstanceOf(Error) + expect(mutation().error).toEqual(new Error('negative-not-allowed')) }) it('M10: reset clears mutation state back to idle baseline', async () => { @@ -188,7 +188,7 @@ describe('createMutationController', () => { 'reset-target', ) await waitFor(() => mutation().isError) - expect(mutation().error).toBeInstanceOf(Error) + expect(mutation().error).toEqual(new Error('reset-target')) mutation.reset() expect(mutation().isIdle).toBe(true) @@ -221,7 +221,7 @@ describe('createMutationController', () => { expect(() => mutation.mutate(-1)).not.toThrow() await waitFor(() => mutation().isError) - expect(mutation().error).toBeInstanceOf(Error) + expect(mutation().error).toEqual(new Error('negative-not-allowed')) await expect(mutation.mutateAsync(-1)).rejects.toThrow( 'negative-not-allowed', diff --git a/packages/query-core/src/__tests__/timeoutManager.test.tsx b/packages/query-core/src/__tests__/timeoutManager.test.tsx index 79ea130db85..d40101555ee 100644 --- a/packages/query-core/src/__tests__/timeoutManager.test.tsx +++ b/packages/query-core/src/__tests__/timeoutManager.test.tsx @@ -93,7 +93,7 @@ describe('timeoutManager', () => { expect.stringMatching( /\[timeoutManager\]: Switching .* might result in unexpected behavior\..*/, ), - expect.anything(), + { previous: customProvider, provider: customProvider2 }, ) // 3. Switching again with no intermediate calls should not warn diff --git a/packages/query-devtools/src/__tests__/Explorer.test.tsx b/packages/query-devtools/src/__tests__/Explorer.test.tsx index 2ffc7cd1d93..5f2c1deab49 100644 --- a/packages/query-devtools/src/__tests__/Explorer.test.tsx +++ b/packages/query-devtools/src/__tests__/Explorer.test.tsx @@ -232,7 +232,7 @@ describe('Explorer', () => { ).toBeInTheDocument() expect(consoleError).toHaveBeenCalledWith( 'Failed to copy: ', - expect.any(Error), + new Error('denied'), ) }) diff --git a/packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts b/packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts index 477abc148be..2456fa9e62e 100644 --- a/packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts +++ b/packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts @@ -19,9 +19,9 @@ describe('usePrefetchInfiniteQuery', () => { queryClient, 'prefetchInfiniteQuery', ) - const queryFn = vi.fn(() => - Promise.resolve({ data: 'prefetched', currentPage: 1 }), - ) + const queryFn = () => + Promise.resolve({ data: 'prefetched', currentPage: 1 }) + const getNextPageParam = () => undefined const key = queryKey() @@ -30,7 +30,7 @@ describe('usePrefetchInfiniteQuery', () => { queryKey: key, queryFn, initialPageParam: 1, - getNextPageParam: () => undefined, + getNextPageParam, }, queryClient, ) @@ -40,7 +40,7 @@ describe('usePrefetchInfiniteQuery', () => { queryKey: key, queryFn, initialPageParam: 1, - getNextPageParam: expect.any(Function), + getNextPageParam, }) }) @@ -50,9 +50,8 @@ describe('usePrefetchInfiniteQuery', () => { queryClient, 'prefetchInfiniteQuery', ) - const queryFn = vi.fn(() => - Promise.resolve({ data: 'prefetched', currentPage: 1 }), - ) + const queryFn = () => + Promise.resolve({ data: 'prefetched', currentPage: 1 }) const key = queryKey() queryClient.setQueryData(key, { @@ -81,22 +80,26 @@ describe('usePrefetchInfiniteQuery', () => { ) const nestedRef = ref('value') const key = queryKey() + const queryFn = () => + Promise.resolve({ data: 'prefetched', currentPage: 1 }) + const getNextPageParam = () => undefined usePrefetchInfiniteQuery( { queryKey: [...key, nestedRef], - queryFn: () => Promise.resolve({ data: 'prefetched', currentPage: 1 }), + queryFn, initialPageParam: 1, - getNextPageParam: () => undefined, + getNextPageParam, }, queryClient, ) - expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith( - expect.objectContaining({ - queryKey: [...key, 'value'], - }), - ) + expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({ + queryKey: [...key, 'value'], + queryFn, + initialPageParam: 1, + getNextPageParam, + }) }) it('should prefetch infinite query again when query key changes reactively', async () => { @@ -107,33 +110,37 @@ describe('usePrefetchInfiniteQuery', () => { ) const keyRef = ref('first') const key = queryKey() + const queryFn = () => + Promise.resolve({ data: keyRef.value, currentPage: 1 }) + const getNextPageParam = () => undefined usePrefetchInfiniteQuery( () => ({ queryKey: [...key, keyRef.value], - queryFn: () => Promise.resolve({ data: keyRef.value, currentPage: 1 }), + queryFn, initialPageParam: 1, - getNextPageParam: () => undefined, + getNextPageParam, }), queryClient, ) - expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1) - expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith( - expect.objectContaining({ - queryKey: [...key, 'first'], - }), - ) + expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(1, { + queryKey: [...key, 'first'], + queryFn, + initialPageParam: 1, + getNextPageParam, + }) keyRef.value = 'second' await nextTick() expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2) - expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith( - expect.objectContaining({ - queryKey: [...key, 'second'], - }), - ) + expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(2, { + queryKey: [...key, 'second'], + queryFn, + initialPageParam: 1, + getNextPageParam, + }) }) it('should warn when used outside of setup function in development mode', () => { diff --git a/packages/vue-query/src/__tests__/usePrefetchQuery.test.ts b/packages/vue-query/src/__tests__/usePrefetchQuery.test.ts index 88b31026289..4cfcf832c4f 100644 --- a/packages/vue-query/src/__tests__/usePrefetchQuery.test.ts +++ b/packages/vue-query/src/__tests__/usePrefetchQuery.test.ts @@ -16,7 +16,7 @@ describe('usePrefetchQuery', () => { it('should prefetch query if query state does not exist', () => { const queryClient = new QueryClient() const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery') - const queryFn = vi.fn(() => Promise.resolve('prefetched')) + const queryFn = () => Promise.resolve('prefetched') const key = queryKey() usePrefetchQuery( @@ -37,7 +37,7 @@ describe('usePrefetchQuery', () => { it('should not prefetch query if query state exists', () => { const queryClient = new QueryClient() const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery') - const queryFn = vi.fn(() => Promise.resolve('prefetched')) + const queryFn = () => Promise.resolve('prefetched') const key = queryKey() queryClient.setQueryData(key, 'existing') @@ -57,20 +57,20 @@ describe('usePrefetchQuery', () => { const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery') const nestedRef = ref('value') const key = queryKey() + const queryFn = () => Promise.resolve('prefetched') usePrefetchQuery( { queryKey: [...key, nestedRef], - queryFn: () => Promise.resolve('prefetched'), + queryFn, }, queryClient, ) - expect(prefetchQuerySpy).toHaveBeenCalledWith( - expect.objectContaining({ - queryKey: [...key, 'value'], - }), - ) + expect(prefetchQuerySpy).toHaveBeenCalledWith({ + queryKey: [...key, 'value'], + queryFn, + }) }) it('should prefetch again when query key changes reactively', async () => { @@ -78,28 +78,28 @@ describe('usePrefetchQuery', () => { const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery') const keyRef = ref('first') const key = queryKey() + const queryFn = () => Promise.resolve(keyRef.value) usePrefetchQuery( () => ({ queryKey: [...key, keyRef.value], - queryFn: () => Promise.resolve(keyRef.value), + queryFn, }), queryClient, ) - expect(prefetchQuerySpy).toHaveBeenCalledTimes(1) - expect(prefetchQuerySpy).toHaveBeenLastCalledWith({ + expect(prefetchQuerySpy).toHaveBeenNthCalledWith(1, { queryKey: [...key, 'first'], - queryFn: expect.any(Function), + queryFn, }) keyRef.value = 'second' await nextTick() expect(prefetchQuerySpy).toHaveBeenCalledTimes(2) - expect(prefetchQuerySpy).toHaveBeenLastCalledWith({ + expect(prefetchQuerySpy).toHaveBeenNthCalledWith(2, { queryKey: [...key, 'second'], - queryFn: expect.any(Function), + queryFn, }) }) diff --git a/packages/vue-query/src/__tests__/useQueryClient.test.ts b/packages/vue-query/src/__tests__/useQueryClient.test.ts index 8811089184d..a3d2c1a9d23 100644 --- a/packages/vue-query/src/__tests__/useQueryClient.test.ts +++ b/packages/vue-query/src/__tests__/useQueryClient.test.ts @@ -34,7 +34,9 @@ describe('useQueryClient', () => { it('should throw an error when queryClient does not exist in the context', () => { injectSpy.mockReturnValueOnce(undefined) - expect(useQueryClient).toThrow() + expect(useQueryClient).toThrow( + "No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.", + ) expect(injectSpy).toHaveBeenCalledTimes(1) expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT) }) @@ -42,7 +44,9 @@ describe('useQueryClient', () => { it('should throw an error when used outside of setup function', () => { hasInjectionContextSpy.mockReturnValueOnce(false) - expect(useQueryClient).toThrow() + expect(useQueryClient).toThrow( + 'vue-query hooks can only be used inside setup() function or functions that support injection context.', + ) expect(hasInjectionContextSpy).toHaveBeenCalledTimes(1) })