From 7ec9b868afca01292a738c5ab69c5139162a13fc Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Tue, 16 Jun 2026 16:55:48 +0900 Subject: [PATCH 1/2] test(solid-query/useQuery): move type-only test to 'useQuery.test-d.tsx' --- .../src/__tests__/useQuery.test-d.tsx | 154 ++++++++++++++++ .../src/__tests__/useQuery.test.tsx | 166 +----------------- 2 files changed, 155 insertions(+), 165 deletions(-) diff --git a/packages/solid-query/src/__tests__/useQuery.test-d.tsx b/packages/solid-query/src/__tests__/useQuery.test-d.tsx index 64eaa5eb694..1fb6223a8cf 100644 --- a/packages/solid-query/src/__tests__/useQuery.test-d.tsx +++ b/packages/solid-query/src/__tests__/useQuery.test-d.tsx @@ -1,8 +1,162 @@ import { describe, expectTypeOf, it } from 'vitest' import { queryKey } from '@tanstack/query-test-utils' import { queryOptions, useQuery } from '../index' +import type { OmitKeyof, QueryFunction, UseQueryOptions } from '..' describe('useQuery', () => { + const key = queryKey() + + // unspecified query function should default to unknown + const noQueryFn = useQuery(() => ({ queryKey: key })) + 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).toEqualTypeOf() + expectTypeOf(fromQueryFn.error).toEqualTypeOf() + + // it should be possible to specify the result type + const withResult = useQuery(() => ({ + queryKey: key, + queryFn: () => 'test', + })) + 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).toEqualTypeOf() + expectTypeOf(withError.error).toEqualTypeOf() + + // it should provide the result type in the configuration + useQuery(() => ({ + queryKey: [key], + queryFn: () => true, + })) + + // it should be possible to specify a union type as result type + const unionTypeSync = useQuery(() => ({ + queryKey: key, + queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)), + })) + expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>() + const unionTypeAsync = useQuery<'a' | 'b'>(() => ({ + queryKey: key, + queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'), + })) + expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>() + + // should error when the query function result does not match with the specified type + // @ts-expect-error + useQuery(() => ({ queryKey: key, queryFn: () => 'test' })) + + // it should infer the result type from a generic query function + function queryFn(): Promise { + return Promise.resolve({} as T) + } + + const fromGenericQueryFn = useQuery(() => ({ + queryKey: key, + queryFn: () => queryFn(), + })) + expectTypeOf(fromGenericQueryFn.data).toEqualTypeOf() + expectTypeOf(fromGenericQueryFn.error).toEqualTypeOf() + + const fromGenericOptionsQueryFn = useQuery(() => ({ + queryKey: key, + queryFn: () => queryFn(), + })) + expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf< + string | undefined + >() + expectTypeOf( + fromGenericOptionsQueryFn.error, + ).toEqualTypeOf() + + type MyData = number + type MyQueryKey = readonly ['my-data', number] + + const getMyDataArrayKey: QueryFunction = ({ + queryKey: [, n], + }) => { + return n + 42 + } + + useQuery(() => ({ + queryKey: ['my-data', 100] as const, + queryFn: getMyDataArrayKey, + })) + + const getMyDataStringKey: QueryFunction = (context) => { + expectTypeOf(context.queryKey).toEqualTypeOf<['1']>() + return Number(context.queryKey[0]) + 42 + } + + useQuery(() => ({ + queryKey: ['1'] as ['1'], + queryFn: getMyDataStringKey, + })) + + // it should handle query-functions that return Promise + useQuery(() => ({ + queryKey: key, + queryFn: () => fetch('return Promise').then((resp) => resp.json()), + })) + + // handles wrapped queries with custom fetcher passed as inline queryFn + const useWrappedQuery = < + TQueryKey extends [string, Record?], + TQueryFnData, + TError, + TData = TQueryFnData, + >( + qk: TQueryKey, + fetcher: ( + obj: TQueryKey[1], + token: string, + // return type must be wrapped with TQueryFnReturn + ) => Promise, + options?: OmitKeyof< + UseQueryOptions, + 'queryKey' | 'queryFn' | 'initialData', + 'safely' + >, + ) => + useQuery(() => ({ + queryKey: qk, + queryFn: () => fetcher(qk[1], 'token'), + ...options, + })) + const test = useWrappedQuery([''], () => Promise.resolve('1')) + expectTypeOf(test.data).toEqualTypeOf() + + // handles wrapped queries with custom fetcher passed directly to useQuery + const useWrappedFuncStyleQuery = < + TQueryKey extends [string, Record?], + TQueryFnData, + TError, + TData = TQueryFnData, + >( + qk: TQueryKey, + fetcher: () => Promise, + options?: OmitKeyof< + UseQueryOptions, + 'queryKey' | 'queryFn' | 'initialData', + 'safely' + >, + ) => useQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options })) + const testFuncStyle = useWrappedFuncStyleQuery([''], () => + Promise.resolve(true), + ) + expectTypeOf(testFuncStyle.data).toEqualTypeOf() + describe('initialData', () => { describe('Config object overload', () => { it('TData should always be defined when initialData is provided as an object', () => { diff --git a/packages/solid-query/src/__tests__/useQuery.test.tsx b/packages/solid-query/src/__tests__/useQuery.test.tsx index 6eb755a89cb..6c999d10b6d 100644 --- a/packages/solid-query/src/__tests__/useQuery.test.tsx +++ b/packages/solid-query/src/__tests__/useQuery.test.tsx @@ -37,13 +37,7 @@ import { renderWithClient, setActTimeout, } from './utils' -import type { - DefinedUseQueryResult, - OmitKeyof, - QueryFunction, - UseQueryOptions, - UseQueryResult, -} from '..' +import type { DefinedUseQueryResult, QueryFunction, UseQueryResult } from '..' import type { Mock } from 'vitest' import type { JSX } from 'solid-js' @@ -62,164 +56,6 @@ describe('useQuery', () => { vi.useRealTimers() }) - it('should return the correct types', () => { - const key = queryKey() - - // @ts-expect-error - function Page() { - // unspecified query function should default to unknown - const noQueryFn = useQuery(() => ({ queryKey: key })) - 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).toEqualTypeOf() - expectTypeOf(fromQueryFn.error).toEqualTypeOf() - - // it should be possible to specify the result type - const withResult = useQuery(() => ({ - queryKey: key, - queryFn: () => 'test', - })) - 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).toEqualTypeOf() - expectTypeOf(withError.error).toEqualTypeOf() - - // it should provide the result type in the configuration - useQuery(() => ({ - queryKey: [key], - queryFn: () => true, - })) - - // it should be possible to specify a union type as result type - const unionTypeSync = useQuery(() => ({ - queryKey: key, - queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)), - })) - expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>() - const unionTypeAsync = useQuery<'a' | 'b'>(() => ({ - queryKey: key, - queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'), - })) - expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>() - - // should error when the query function result does not match with the specified type - // @ts-expect-error - useQuery(() => ({ queryKey: key, queryFn: () => 'test' })) - - // it should infer the result type from a generic query function - function queryFn(): Promise { - return Promise.resolve({} as T) - } - - const fromGenericQueryFn = useQuery(() => ({ - queryKey: key, - queryFn: () => queryFn(), - })) - expectTypeOf(fromGenericQueryFn.data).toEqualTypeOf() - expectTypeOf(fromGenericQueryFn.error).toEqualTypeOf() - - const fromGenericOptionsQueryFn = useQuery(() => ({ - queryKey: key, - queryFn: () => queryFn(), - })) - expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf< - string | undefined - >() - expectTypeOf( - fromGenericOptionsQueryFn.error, - ).toEqualTypeOf() - - type MyData = number - type MyQueryKey = readonly ['my-data', number] - - const getMyDataArrayKey: QueryFunction = ({ - queryKey: [, n], - }) => { - return n + 42 - } - - useQuery(() => ({ - queryKey: ['my-data', 100] as const, - queryFn: getMyDataArrayKey, - })) - - const getMyDataStringKey: QueryFunction = (context) => { - expectTypeOf(context.queryKey).toEqualTypeOf<['1']>() - return Number(context.queryKey[0]) + 42 - } - - useQuery(() => ({ - queryKey: ['1'] as ['1'], - queryFn: getMyDataStringKey, - })) - - // it should handle query-functions that return Promise - useQuery(() => ({ - queryKey: key, - queryFn: () => fetch('return Promise').then((resp) => resp.json()), - })) - - // handles wrapped queries with custom fetcher passed as inline queryFn - const useWrappedQuery = < - TQueryKey extends [string, Record?], - TQueryFnData, - TError, - TData = TQueryFnData, - >( - qk: TQueryKey, - fetcher: ( - obj: TQueryKey[1], - token: string, - // return type must be wrapped with TQueryFnReturn - ) => Promise, - options?: OmitKeyof< - UseQueryOptions, - 'queryKey' | 'queryFn' | 'initialData', - 'safely' - >, - ) => - useQuery(() => ({ - queryKey: qk, - queryFn: () => fetcher(qk[1], 'token'), - ...options, - })) - const test = useWrappedQuery([''], () => Promise.resolve('1')) - expectTypeOf(test.data).toEqualTypeOf() - - // handles wrapped queries with custom fetcher passed directly to useQuery - const useWrappedFuncStyleQuery = < - TQueryKey extends [string, Record?], - TQueryFnData, - TError, - TData = TQueryFnData, - >( - qk: TQueryKey, - fetcher: () => Promise, - options?: OmitKeyof< - UseQueryOptions, - 'queryKey' | 'queryFn' | 'initialData', - 'safely' - >, - ) => useQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options })) - const testFuncStyle = useWrappedFuncStyleQuery([''], () => - Promise.resolve(true), - ) - expectTypeOf(testFuncStyle.data).toEqualTypeOf() - } - }) - // See https://github.com/tannerlinsley/react-query/issues/105 it('should allow to set default data value', async () => { const key = queryKey() From 9872ea5268c2beac8c074f460a6fa257820f8427 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 16 Jun 2026 08:22:40 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- packages/solid-query/src/__tests__/useQuery.test-d.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/solid-query/src/__tests__/useQuery.test-d.tsx b/packages/solid-query/src/__tests__/useQuery.test-d.tsx index 1fb6223a8cf..08fb3f17bc2 100644 --- a/packages/solid-query/src/__tests__/useQuery.test-d.tsx +++ b/packages/solid-query/src/__tests__/useQuery.test-d.tsx @@ -76,9 +76,7 @@ describe('useQuery', () => { expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf< string | undefined >() - expectTypeOf( - fromGenericOptionsQueryFn.error, - ).toEqualTypeOf() + expectTypeOf(fromGenericOptionsQueryFn.error).toEqualTypeOf() type MyData = number type MyQueryKey = readonly ['my-data', number]