From f345936723052ae1bd9d4e95e60a66cfd5cb8cf9 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Thu, 29 Feb 2024 01:38:16 +0900 Subject: [PATCH 1/5] test(react-query): update vitest type test code correctly (expectTypeOf) --- .../src/tests/infiniteQueryObserver.test.tsx | 57 ++--- .../src/tests/queryObserver.test.tsx | 64 ++--- .../__tests__/infiniteQueryOptions.test-d.tsx | 14 +- .../src/__tests__/useQueries.test.tsx | 219 ++++++++++-------- .../src/__tests__/useQuery.test.tsx | 52 +++-- packages/react-query/src/__tests__/utils.tsx | 7 - 6 files changed, 218 insertions(+), 195 deletions(-) diff --git a/packages/query-core/src/tests/infiniteQueryObserver.test.tsx b/packages/query-core/src/tests/infiniteQueryObserver.test.tsx index 4bd38c8239..b28acb57c2 100644 --- a/packages/query-core/src/tests/infiniteQueryObserver.test.tsx +++ b/packages/query-core/src/tests/infiniteQueryObserver.test.tsx @@ -178,31 +178,36 @@ describe('InfiniteQueryObserver', () => { const result = observer.getCurrentResult() - result.isPending && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf(result.isLoading) && - expectTypeOf<'pending'>(result.status) - - result.isLoading && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf(result.isPending) && - expectTypeOf<'pending'>(result.status) - - result.isLoadingError && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'error'>(result.status) - - result.isRefetchError && - expectTypeOf>(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'error'>(result.status) - - result.isSuccess && - expectTypeOf>(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'success'>(result.status) + if (result.isPending) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.isLoading).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'pending'>() + } + + if (result.isLoading) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.isPending).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'pending'>() + } + + if (result.isLoadingError) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'error'>() + } + + if (result.isRefetchError) { + expectTypeOf(result.data).toEqualTypeOf>() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'error'>() + } + + if (result.isSuccess) { + expectTypeOf(result.data).toEqualTypeOf>() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'success'>() + } }) }) diff --git a/packages/query-core/src/tests/queryObserver.test.tsx b/packages/query-core/src/tests/queryObserver.test.tsx index 550e5cbf91..e64e709dc1 100644 --- a/packages/query-core/src/tests/queryObserver.test.tsx +++ b/packages/query-core/src/tests/queryObserver.test.tsx @@ -120,7 +120,9 @@ describe('queryObserver', () => { }) let observerResult const unsubscribe = observer.subscribe((result) => { - expectTypeOf>(result) + expectTypeOf(result).toEqualTypeOf< + QueryObserverResult<{ myCount: number }> + >() observerResult = result }) await sleep(1) @@ -136,7 +138,9 @@ describe('queryObserver', () => { select: (data) => ({ myCount: data.count }), }) const observerResult = await observer.refetch() - expectTypeOf<{ myCount: number } | undefined>(observerResult.data) + expectTypeOf(observerResult.data).toEqualTypeOf< + { myCount: number } | undefined + >() expect(observerResult.data).toMatchObject({ myCount: 1 }) }) @@ -905,31 +909,35 @@ describe('queryObserver', () => { const result = observer.getCurrentResult() - result.isPending && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf(result.isLoading) && - expectTypeOf<'pending'>(result.status) - - result.isLoading && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf(result.isPending) && - expectTypeOf<'pending'>(result.status) - - result.isLoadingError && - expectTypeOf(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'error'>(result.status) - - result.isRefetchError && - expectTypeOf<{ value: string }>(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'error'>(result.status) - - result.isSuccess && - expectTypeOf<{ value: string }>(result.data) && - expectTypeOf(result.error) && - expectTypeOf<'success'>(result.status) + if (result.isPending) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.isLoading).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'pending'>() + } + if (result.isLoading) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.isPending).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'pending'>() + } + + if (result.isLoadingError) { + expectTypeOf(result.data).toEqualTypeOf() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'error'>() + } + + if (result.isRefetchError) { + expectTypeOf(result.data).toEqualTypeOf<{ value: string }>() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'error'>() + } + + if (result.isSuccess) { + expectTypeOf(result.data).toEqualTypeOf<{ value: string }>() + expectTypeOf(result.error).toEqualTypeOf() + expectTypeOf(result.status).toEqualTypeOf<'success'>() + } }) }) diff --git a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx index 434f5fb48c..cdd2dac0f2 100644 --- a/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx @@ -1,9 +1,9 @@ import { describe, expectTypeOf, it } from 'vitest' import { QueryClient } from '@tanstack/query-core' +import { type InfiniteData, dataTagSymbol } from '@tanstack/query-core' import { infiniteQueryOptions } from '../infiniteQueryOptions' import { useInfiniteQuery } from '../useInfiniteQuery' import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery' -import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core' describe('queryOptions', () => { it('should not allow excess properties', () => { @@ -75,9 +75,7 @@ describe('queryOptions', () => { initialPageParam: 1, }) - expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< - InfiniteData - >() + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf>() }) it('should tag the queryKey even if no promise is returned', () => { const { queryKey } = infiniteQueryOptions({ @@ -87,9 +85,7 @@ describe('queryOptions', () => { initialPageParam: 1, }) - expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< - InfiniteData - >() + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf>() }) it('should tag the queryKey with the result type of the QueryFn if select is used', () => { const { queryKey } = infiniteQueryOptions({ @@ -100,9 +96,7 @@ describe('queryOptions', () => { initialPageParam: 1, }) - expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< - InfiniteData - >() + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf>() }) it('should return the proper type when passed to getQueryData', () => { const { queryKey } = infiniteQueryOptions({ diff --git a/packages/react-query/src/__tests__/useQueries.test.tsx b/packages/react-query/src/__tests__/useQueries.test.tsx index 5d57636c8e..fac83b3658 100644 --- a/packages/react-query/src/__tests__/useQueries.test.tsx +++ b/packages/react-query/src/__tests__/useQueries.test.tsx @@ -3,13 +3,7 @@ import { fireEvent, render, waitFor } from '@testing-library/react' import * as React from 'react' import { ErrorBoundary } from 'react-error-boundary' import { QueryCache, queryOptions, useQueries } from '..' -import { - createQueryClient, - expectTypeNotAny, - queryKey, - renderWithClient, - sleep, -} from './utils' +import { createQueryClient, queryKey, renderWithClient, sleep } from './utils' import type { QueryFunction, QueryKey, @@ -140,13 +134,19 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result1[0]) - expectTypeOf>(result1[1]) - expectTypeOf, boolean>>(result1[2]) - expectTypeOf(result1[0].data) - expectTypeOf(result1[1].data) - expectTypeOf | undefined>(result1[2].data) - expectTypeOf(result1[2].error) + expectTypeOf(result1[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result1[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result1[2]).toEqualTypeOf< + QueryObserverResult, boolean> + >() + expectTypeOf(result1[0].data).toEqualTypeOf() + expectTypeOf(result1[1].data).toEqualTypeOf() + expectTypeOf(result1[2].data).toEqualTypeOf | undefined>() + expectTypeOf(result1[2].error).toEqualTypeOf() // TData (3rd element) takes precedence over TQueryFnData (1st element) const result2 = useQueries< @@ -157,8 +157,7 @@ describe('useQueries', () => { queryKey: key1, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a.toLowerCase() }, }, @@ -166,17 +165,20 @@ describe('useQueries', () => { queryKey: key2, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return parseInt(a) }, }, ], }) - expectTypeOf>(result2[0]) - expectTypeOf>(result2[1]) - expectTypeOf(result2[0].data) - expectTypeOf(result2[1].data) + expectTypeOf(result2[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result2[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result2[0].data).toEqualTypeOf() + expectTypeOf(result2[1].data).toEqualTypeOf() // types should be enforced useQueries<[[string, unknown, string], [string, boolean, number]]>({ @@ -185,8 +187,7 @@ describe('useQueries', () => { queryKey: key1, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a.toLowerCase() }, placeholderData: 'string', @@ -197,8 +198,7 @@ describe('useQueries', () => { queryKey: key2, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return parseInt(a) }, placeholderData: 'string', @@ -251,13 +251,19 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result1[0]) - expectTypeOf>(result1[1]) - expectTypeOf, boolean>>(result1[2]) - expectTypeOf(result1[0].data) - expectTypeOf(result1[1].data) - expectTypeOf | undefined>(result1[2].data) - expectTypeOf(result1[2].error) + expectTypeOf(result1[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result1[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result1[2]).toEqualTypeOf< + QueryObserverResult, boolean> + >() + expectTypeOf(result1[0].data).toEqualTypeOf() + expectTypeOf(result1[1].data).toEqualTypeOf() + expectTypeOf(result1[2].data).toEqualTypeOf | undefined>() + expectTypeOf(result1[2].error).toEqualTypeOf() // TData (data prop) takes precedence over TQueryFnData (queryFnData prop) const result2 = useQueries< @@ -271,8 +277,7 @@ describe('useQueries', () => { queryKey: key1, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a.toLowerCase() }, }, @@ -280,17 +285,20 @@ describe('useQueries', () => { queryKey: key2, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return parseInt(a) }, }, ], }) - expectTypeOf>(result2[0]) - expectTypeOf>(result2[1]) - expectTypeOf(result2[0].data) - expectTypeOf(result2[1].data) + expectTypeOf(result2[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result2[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result2[0].data).toEqualTypeOf() + expectTypeOf(result2[1].data).toEqualTypeOf() // can pass only TData (data prop) although TQueryFnData will be left unknown const result3 = useQueries<[{ data: string }, { data: number }]>({ @@ -299,8 +307,7 @@ describe('useQueries', () => { queryKey: key1, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a as string }, }, @@ -308,17 +315,20 @@ describe('useQueries', () => { queryKey: key2, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a as number }, }, ], }) - expectTypeOf>(result3[0]) - expectTypeOf>(result3[1]) - expectTypeOf(result3[0].data) - expectTypeOf(result3[1].data) + expectTypeOf(result3[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result3[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result3[0].data).toEqualTypeOf() + expectTypeOf(result3[1].data).toEqualTypeOf() // types should be enforced useQueries< @@ -332,8 +342,7 @@ describe('useQueries', () => { queryKey: key1, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a.toLowerCase() }, placeholderData: 'string', @@ -344,8 +353,7 @@ describe('useQueries', () => { queryKey: key2, queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return parseInt(a) }, placeholderData: 'string', @@ -379,8 +387,7 @@ describe('useQueries', () => { queryKey: ['key1'], queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return a.toLowerCase() }, }), @@ -388,17 +395,20 @@ describe('useQueries', () => { queryKey: ['key2'], queryFn: () => 'string', select: (a) => { - expectTypeOf(a) - expectTypeNotAny(a) + expectTypeOf(a).toEqualTypeOf() return parseInt(a) }, }), ], }) - expectTypeOf>(result4[0]) - expectTypeOf>(result4[1]) - expectTypeOf(result4[0].data) - expectTypeOf(result4[1].data) + expectTypeOf(result4[0]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result4[1]).toEqualTypeOf< + QueryObserverResult + >() + expectTypeOf(result4[0].data).toEqualTypeOf() + expectTypeOf(result4[1].data).toEqualTypeOf() } }) @@ -421,8 +431,10 @@ describe('useQueries', () => { queryFn: () => i + 10, })), }) - expectTypeOf>>(result1) - expectTypeOf(result1[0]?.data) + expectTypeOf(result1).toEqualTypeOf< + Array> + >() + expectTypeOf(result1[0]?.data).toEqualTypeOf() // Array.map preserves TError const result1_err = useQueries({ @@ -432,9 +444,13 @@ describe('useQueries', () => { throwOnError, })), }) - expectTypeOf>>(result1_err) - expectTypeOf(result1_err[0]?.data) - expectTypeOf(result1_err[0]?.error) + expectTypeOf(result1_err).toEqualTypeOf< + Array> + >() + expectTypeOf(result1_err[0]?.data).toEqualTypeOf() + expectTypeOf(result1_err[0]?.error).toEqualTypeOf< + BizError | null | undefined + >() // Array.map preserves TData const result2 = useQueries({ @@ -444,7 +460,9 @@ describe('useQueries', () => { select: (data: number) => data.toString(), })), }) - expectTypeOf>>(result2) + expectTypeOf(result2).toEqualTypeOf< + Array> + >() const result2_err = useQueries({ queries: Array(50).map((_, i) => ({ @@ -454,7 +472,9 @@ describe('useQueries', () => { throwOnError, })), }) - expectTypeOf>>(result2_err) + expectTypeOf(result2_err).toEqualTypeOf< + Array> + >() const result3 = useQueries({ queries: [ @@ -478,16 +498,16 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result3[0]) - expectTypeOf>(result3[1]) - expectTypeOf>(result3[2]) - expectTypeOf(result3[0].data) - expectTypeOf(result3[1].data) - expectTypeOf(result3[3].data) + expectTypeOf(result3[0]).toEqualTypeOf>() + expectTypeOf(result3[1]).toEqualTypeOf>() + expectTypeOf(result3[2]).toEqualTypeOf>() + expectTypeOf(result3[0].data).toEqualTypeOf() + expectTypeOf(result3[1].data).toEqualTypeOf() + expectTypeOf(result3[3].data).toEqualTypeOf() // select takes precedence over queryFn - expectTypeOf(result3[2].data) + expectTypeOf(result3[2].data).toEqualTypeOf() // infer TError from throwOnError - expectTypeOf(result3[3].error) + expectTypeOf(result3[3].error).toEqualTypeOf() // initialData/placeholderData are enforced useQueries({ @@ -580,10 +600,10 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result4[0]) - expectTypeOf>(result4[1]) - expectTypeOf>(result4[2]) - expectTypeOf>(result4[3]) + expectTypeOf(result4[0]).toEqualTypeOf>() + expectTypeOf(result4[1]).toEqualTypeOf>() + expectTypeOf(result4[2]).toEqualTypeOf>() + expectTypeOf(result4[3]).toEqualTypeOf>() // handles when queryFn returns a Promise const result5 = useQueries({ @@ -594,7 +614,7 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result5[0]) + expectTypeOf(result5[0]).toEqualTypeOf>() // Array as const does not throw error const result6 = useQueries({ @@ -614,9 +634,9 @@ describe('useQueries', () => { }, ], } as const) - expectTypeOf>(result6[0]) - expectTypeOf>(result6[1]) - expectTypeOf>(result6[2]) + expectTypeOf(result6[0]).toEqualTypeOf>() + expectTypeOf(result6[1]).toEqualTypeOf>() + expectTypeOf(result6[2]).toEqualTypeOf>() // field names should be enforced - array literal useQueries({ @@ -704,14 +724,13 @@ describe('useQueries', () => { // no need to type the mapped query (query) => { const { queryFn: fn, queryKey: key } = query - expectTypeOf | undefined>(fn) + expectTypeOf(fn).toEqualTypeOf< + QueryFunction | undefined + >() return { queryKey: key, queryFn: fn - ? (ctx: QueryFunctionContext) => { - expectTypeOf(ctx.queryKey) - return fn.call({}, ctx) - } + ? (ctx: QueryFunctionContext) => fn.call({}, ctx) : undefined, } }, @@ -733,8 +752,8 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>(result[0]) - expectTypeOf>(result[1]) + expectTypeOf(result[0]).toEqualTypeOf>() + expectTypeOf(result[1]).toEqualTypeOf>() const withSelector = useQueries({ queries: [ @@ -750,12 +769,12 @@ describe('useQueries', () => { }, ], }) - expectTypeOf>( - withSelector[0], - ) - expectTypeOf>( - withSelector[1], - ) + expectTypeOf(withSelector[0]).toEqualTypeOf< + UseQueryResult<[number, string], Error> + >() + expectTypeOf(withSelector[1]).toEqualTypeOf< + UseQueryResult<[string, number], Error> + >() const withWrappedQueries = useWrappedQueries( Array(10).map(() => ({ @@ -765,9 +784,9 @@ describe('useQueries', () => { })), ) - expectTypeOf>>( - withWrappedQueries, - ) + expectTypeOf(withWrappedQueries).toEqualTypeOf< + Array> + >() } }) diff --git a/packages/react-query/src/__tests__/useQuery.test.tsx b/packages/react-query/src/__tests__/useQuery.test.tsx index 18888856a9..0bb56b6c79 100644 --- a/packages/react-query/src/__tests__/useQuery.test.tsx +++ b/packages/react-query/src/__tests__/useQuery.test.tsx @@ -32,29 +32,29 @@ describe('useQuery', () => { function Page() { // unspecified query function should default to unknown const noQueryFn = useQuery({ queryKey: key }) - expectTypeOf(noQueryFn.data) - expectTypeOf(noQueryFn.error) + expectTypeOf(noQueryFn.data).toEqualTypeOf() + expectTypeOf(noQueryFn.error).toEqualTypeOf() // it should infer the result type from the query function const fromQueryFn = useQuery({ queryKey: key, queryFn: () => 'test' }) - expectTypeOf(fromQueryFn.data) - expectTypeOf(fromQueryFn.error) + expectTypeOf(fromQueryFn.data).toEqualTypeOf() + expectTypeOf(fromQueryFn.error).toEqualTypeOf() // it should be possible to specify the result type const withResult = useQuery({ queryKey: key, queryFn: () => 'test', }) - expectTypeOf(withResult.data) - expectTypeOf(withResult.error) + expectTypeOf(withResult.data).toEqualTypeOf() + expectTypeOf(withResult.error).toEqualTypeOf() // it should be possible to specify the error type const withError = useQuery({ queryKey: key, queryFn: () => 'test', }) - expectTypeOf(withError.data) - expectTypeOf(withError.error) + expectTypeOf(withError.data).toEqualTypeOf() + expectTypeOf(withError.error).toEqualTypeOf() // it should provide the result type in the configuration useQuery({ @@ -65,14 +65,14 @@ describe('useQuery', () => { // it should be possible to specify a union type as result type const unionTypeSync = useQuery({ queryKey: key, - queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'), + queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)), }) - expectTypeOf<'a' | 'b' | undefined>(unionTypeSync.data) + expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>() const unionTypeAsync = useQuery<'a' | 'b'>({ queryKey: key, queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'), }) - expectTypeOf<'a' | 'b' | undefined>(unionTypeAsync.data) + expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>() // should error when the query function result does not match with the specified type // @ts-expect-error @@ -87,15 +87,19 @@ describe('useQuery', () => { queryKey: key, queryFn: () => queryFn(), }) - expectTypeOf(fromGenericQueryFn.data) - expectTypeOf(fromGenericQueryFn.error) + expectTypeOf(fromGenericQueryFn.data).toEqualTypeOf() + expectTypeOf(fromGenericQueryFn.error).toEqualTypeOf() const fromGenericOptionsQueryFn = useQuery({ queryKey: key, queryFn: () => queryFn(), }) - expectTypeOf(fromGenericOptionsQueryFn.data) - expectTypeOf(fromGenericOptionsQueryFn.error) + expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf< + string | undefined + >() + expectTypeOf( + fromGenericOptionsQueryFn.error, + ).toEqualTypeOf() type MyData = number type MyQueryKey = readonly ['my-data', number] @@ -114,7 +118,7 @@ describe('useQuery', () => { const getMyDataStringKey: QueryFunction = async ( context, ) => { - expectTypeOf<['1']>(context.queryKey) + expectTypeOf(context.queryKey).toEqualTypeOf<['1']>() return Number(context.queryKey[0]) + 42 } @@ -153,7 +157,7 @@ describe('useQuery', () => { ...options, }) const testQuery = useWrappedQuery([''], async () => '1') - expectTypeOf(testQuery.data) + expectTypeOf(testQuery.data).toEqualTypeOf() // handles wrapped queries with custom fetcher passed directly to useQuery const useWrappedFuncStyleQuery = < @@ -170,7 +174,7 @@ describe('useQuery', () => { >, ) => useQuery({ queryKey: qk, queryFn: fetcher, ...options }) const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true) - expectTypeOf(testFuncStyle.data) + expectTypeOf(testFuncStyle.data).toEqualTypeOf() } }) @@ -217,19 +221,19 @@ describe('useQuery', () => { states.push(state) if (state.isPending) { - expectTypeOf(state.data) - expectTypeOf(state.error) + expectTypeOf(state.data).toEqualTypeOf() + expectTypeOf(state.error).toEqualTypeOf() return pending } if (state.isLoadingError) { - expectTypeOf(state.data) - expectTypeOf(state.error) + expectTypeOf(state.data).toEqualTypeOf() + expectTypeOf(state.error).toEqualTypeOf() return {state.error.message} } - expectTypeOf(state.data) - expectTypeOf(state.error) + expectTypeOf(state.data).toEqualTypeOf() + expectTypeOf(state.error).toEqualTypeOf() return {state.data} } diff --git a/packages/react-query/src/__tests__/utils.tsx b/packages/react-query/src/__tests__/utils.tsx index 6c0e79e2e8..a32f5c2592 100644 --- a/packages/react-query/src/__tests__/utils.tsx +++ b/packages/react-query/src/__tests__/utils.tsx @@ -78,13 +78,6 @@ export function setActTimeout(fn: () => void, ms?: number) { }, ms) } -/** - * Assert the parameter is not typed as `any` - */ -export function expectTypeNotAny(_: 0 extends 1 & T ? never : T): void { - return undefined -} - // This monkey-patches the isServer-value from utils, // so that we can pretend to be in a server environment export function setIsServer(isServer: boolean) { From 5c1968a16adc2a2e0f7cdbf754edd39c8d126540 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Thu, 29 Feb 2024 01:42:51 +0900 Subject: [PATCH 2/5] chore: update --- .../src/__tests__/useQueries.test.tsx | 55 +++++-------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/packages/react-query/src/__tests__/useQueries.test.tsx b/packages/react-query/src/__tests__/useQueries.test.tsx index fac83b3658..68139af73e 100644 --- a/packages/react-query/src/__tests__/useQueries.test.tsx +++ b/packages/react-query/src/__tests__/useQueries.test.tsx @@ -7,7 +7,6 @@ import { createQueryClient, queryKey, renderWithClient, sleep } from './utils' import type { QueryFunction, QueryKey, - QueryObserverResult, UseQueryOptions, UseQueryResult, } from '..' @@ -134,14 +133,10 @@ describe('useQueries', () => { }, ], }) - expectTypeOf(result1[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result1[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result1[0]).toEqualTypeOf>() + expectTypeOf(result1[1]).toEqualTypeOf>() expectTypeOf(result1[2]).toEqualTypeOf< - QueryObserverResult, boolean> + UseQueryResult, boolean> >() expectTypeOf(result1[0].data).toEqualTypeOf() expectTypeOf(result1[1].data).toEqualTypeOf() @@ -171,12 +166,8 @@ describe('useQueries', () => { }, ], }) - expectTypeOf(result2[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result2[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result2[0]).toEqualTypeOf>() + expectTypeOf(result2[1]).toEqualTypeOf>() expectTypeOf(result2[0].data).toEqualTypeOf() expectTypeOf(result2[1].data).toEqualTypeOf() @@ -251,14 +242,10 @@ describe('useQueries', () => { }, ], }) - expectTypeOf(result1[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result1[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result1[0]).toEqualTypeOf>() + expectTypeOf(result1[1]).toEqualTypeOf>() expectTypeOf(result1[2]).toEqualTypeOf< - QueryObserverResult, boolean> + UseQueryResult, boolean> >() expectTypeOf(result1[0].data).toEqualTypeOf() expectTypeOf(result1[1].data).toEqualTypeOf() @@ -291,12 +278,8 @@ describe('useQueries', () => { }, ], }) - expectTypeOf(result2[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result2[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result2[0]).toEqualTypeOf>() + expectTypeOf(result2[1]).toEqualTypeOf>() expectTypeOf(result2[0].data).toEqualTypeOf() expectTypeOf(result2[1].data).toEqualTypeOf() @@ -321,12 +304,8 @@ describe('useQueries', () => { }, ], }) - expectTypeOf(result3[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result3[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result3[0]).toEqualTypeOf>() + expectTypeOf(result3[1]).toEqualTypeOf>() expectTypeOf(result3[0].data).toEqualTypeOf() expectTypeOf(result3[1].data).toEqualTypeOf() @@ -401,12 +380,8 @@ describe('useQueries', () => { }), ], }) - expectTypeOf(result4[0]).toEqualTypeOf< - QueryObserverResult - >() - expectTypeOf(result4[1]).toEqualTypeOf< - QueryObserverResult - >() + expectTypeOf(result4[0]).toEqualTypeOf>() + expectTypeOf(result4[1]).toEqualTypeOf>() expectTypeOf(result4[0].data).toEqualTypeOf() expectTypeOf(result4[1].data).toEqualTypeOf() } @@ -445,7 +420,7 @@ describe('useQueries', () => { })), }) expectTypeOf(result1_err).toEqualTypeOf< - Array> + Array> >() expectTypeOf(result1_err[0]?.data).toEqualTypeOf() expectTypeOf(result1_err[0]?.error).toEqualTypeOf< From 9f53ad493a0fe5241787fd69b0b5236c80745fb8 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Thu, 29 Feb 2024 01:50:00 +0900 Subject: [PATCH 3/5] chore(react-query): remove optional chaining --- .../react-query/src/__tests__/useQueries.test.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/react-query/src/__tests__/useQueries.test.tsx b/packages/react-query/src/__tests__/useQueries.test.tsx index 68139af73e..1b49f08e3f 100644 --- a/packages/react-query/src/__tests__/useQueries.test.tsx +++ b/packages/react-query/src/__tests__/useQueries.test.tsx @@ -409,7 +409,9 @@ describe('useQueries', () => { expectTypeOf(result1).toEqualTypeOf< Array> >() - expectTypeOf(result1[0]?.data).toEqualTypeOf() + if (result1[0]) { + expectTypeOf(result1[0].data).toEqualTypeOf() + } // Array.map preserves TError const result1_err = useQueries({ @@ -422,10 +424,10 @@ describe('useQueries', () => { expectTypeOf(result1_err).toEqualTypeOf< Array> >() - expectTypeOf(result1_err[0]?.data).toEqualTypeOf() - expectTypeOf(result1_err[0]?.error).toEqualTypeOf< - BizError | null | undefined - >() + if (result1_err[0]) { + expectTypeOf(result1_err[0].data).toEqualTypeOf() + expectTypeOf(result1_err[0].error).toEqualTypeOf() + } // Array.map preserves TData const result2 = useQueries({ From 1939c48a3c40b3ddb9cbc0db60775c0547518139 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Wed, 6 Mar 2024 03:35:57 +0900 Subject: [PATCH 4/5] chore: update --- packages/react-query/src/__tests__/useQueries.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-query/src/__tests__/useQueries.test.tsx b/packages/react-query/src/__tests__/useQueries.test.tsx index 13f2d1b524..98957ea8b6 100644 --- a/packages/react-query/src/__tests__/useQueries.test.tsx +++ b/packages/react-query/src/__tests__/useQueries.test.tsx @@ -10,7 +10,7 @@ import type { UseQueryOptions, UseQueryResult, } from '..' -import type { QueryFunctionContext, SkipToken } from '@tanstack/query-core' +import type { QueryFunctionContext } from '@tanstack/query-core' describe('useQueries', () => { const queryCache = new QueryCache() From a1717de255c9cd6c9372703e7172b859ad4e1da4 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Thu, 7 Mar 2024 05:50:35 +0900 Subject: [PATCH 5/5] chore: update --- .../src/tests/infiniteQueryObserver.test.tsx | 57 ++++++++--------- .../src/tests/queryObserver.test.tsx | 64 ++++++++----------- 2 files changed, 54 insertions(+), 67 deletions(-) diff --git a/packages/query-core/src/tests/infiniteQueryObserver.test.tsx b/packages/query-core/src/tests/infiniteQueryObserver.test.tsx index b28acb57c2..4bd38c8239 100644 --- a/packages/query-core/src/tests/infiniteQueryObserver.test.tsx +++ b/packages/query-core/src/tests/infiniteQueryObserver.test.tsx @@ -178,36 +178,31 @@ describe('InfiniteQueryObserver', () => { const result = observer.getCurrentResult() - if (result.isPending) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.isLoading).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'pending'>() - } - - if (result.isLoading) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.isPending).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'pending'>() - } - - if (result.isLoadingError) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'error'>() - } - - if (result.isRefetchError) { - expectTypeOf(result.data).toEqualTypeOf>() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'error'>() - } - - if (result.isSuccess) { - expectTypeOf(result.data).toEqualTypeOf>() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'success'>() - } + result.isPending && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf(result.isLoading) && + expectTypeOf<'pending'>(result.status) + + result.isLoading && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf(result.isPending) && + expectTypeOf<'pending'>(result.status) + + result.isLoadingError && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'error'>(result.status) + + result.isRefetchError && + expectTypeOf>(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'error'>(result.status) + + result.isSuccess && + expectTypeOf>(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'success'>(result.status) }) }) diff --git a/packages/query-core/src/tests/queryObserver.test.tsx b/packages/query-core/src/tests/queryObserver.test.tsx index e64e709dc1..550e5cbf91 100644 --- a/packages/query-core/src/tests/queryObserver.test.tsx +++ b/packages/query-core/src/tests/queryObserver.test.tsx @@ -120,9 +120,7 @@ describe('queryObserver', () => { }) let observerResult const unsubscribe = observer.subscribe((result) => { - expectTypeOf(result).toEqualTypeOf< - QueryObserverResult<{ myCount: number }> - >() + expectTypeOf>(result) observerResult = result }) await sleep(1) @@ -138,9 +136,7 @@ describe('queryObserver', () => { select: (data) => ({ myCount: data.count }), }) const observerResult = await observer.refetch() - expectTypeOf(observerResult.data).toEqualTypeOf< - { myCount: number } | undefined - >() + expectTypeOf<{ myCount: number } | undefined>(observerResult.data) expect(observerResult.data).toMatchObject({ myCount: 1 }) }) @@ -909,35 +905,31 @@ describe('queryObserver', () => { const result = observer.getCurrentResult() - if (result.isPending) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.isLoading).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'pending'>() - } - if (result.isLoading) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.isPending).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'pending'>() - } - - if (result.isLoadingError) { - expectTypeOf(result.data).toEqualTypeOf() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'error'>() - } - - if (result.isRefetchError) { - expectTypeOf(result.data).toEqualTypeOf<{ value: string }>() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'error'>() - } - - if (result.isSuccess) { - expectTypeOf(result.data).toEqualTypeOf<{ value: string }>() - expectTypeOf(result.error).toEqualTypeOf() - expectTypeOf(result.status).toEqualTypeOf<'success'>() - } + result.isPending && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf(result.isLoading) && + expectTypeOf<'pending'>(result.status) + + result.isLoading && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf(result.isPending) && + expectTypeOf<'pending'>(result.status) + + result.isLoadingError && + expectTypeOf(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'error'>(result.status) + + result.isRefetchError && + expectTypeOf<{ value: string }>(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'error'>(result.status) + + result.isSuccess && + expectTypeOf<{ value: string }>(result.data) && + expectTypeOf(result.error) && + expectTypeOf<'success'>(result.status) }) })