From b881482358664e9bbcbe985e51c7ec10512f68d1 Mon Sep 17 00:00:00 2001 From: KimHyeongRae0 Date: Mon, 20 Apr 2026 22:18:13 +0900 Subject: [PATCH 1/2] test(query-core/utils): add tests for 'ensureQueryFn' initialPromise fallback and skipToken handling --- .../query-core/src/__tests__/utils.test.tsx | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/packages/query-core/src/__tests__/utils.test.tsx b/packages/query-core/src/__tests__/utils.test.tsx index f9b0ee4fa07..caccf2b3f16 100644 --- a/packages/query-core/src/__tests__/utils.test.tsx +++ b/packages/query-core/src/__tests__/utils.test.tsx @@ -4,6 +4,7 @@ import { QueryClient } from '..' import { addToEnd, addToStart, + ensureQueryFn, hashKey, hashQueryKeyByOptions, isPlainArray, @@ -14,6 +15,7 @@ import { replaceEqualDeep, shallowEqualObjects, shouldThrowError, + skipToken, } from '../utils' import { Mutation } from '../mutation' @@ -534,6 +536,57 @@ describe('core/utils', () => { }) }) + describe('ensureQueryFn', () => { + it('should return a function that resolves to initialPromise when queryFn is missing and initialPromise is provided', async () => { + const initialPromise = Promise.resolve('initial-data') + + const resolved = ensureQueryFn( + { queryHash: '["key"]' }, + { initialPromise }, + ) + + await expect( + (resolved as unknown as () => Promise)(), + ).resolves.toBe('initial-data') + }) + + it('should return a function that rejects when initialPromise rejects', async () => { + const error = new Error('initial-promise-error') + const initialPromise = Promise.reject(error) + + const resolved = ensureQueryFn( + { queryHash: '["key"]' }, + { initialPromise }, + ) + + await expect( + (resolved as unknown as () => Promise)(), + ).rejects.toBe(error) + }) + + it('should return a function that rejects with missing queryFn error when queryFn is set to skipToken', async () => { + const consoleErrorSpy = vi + .spyOn(console, 'error') + .mockImplementation(() => undefined) + + const resolved = ensureQueryFn({ + queryFn: skipToken, + queryHash: '["skip"]', + }) + + expect(consoleErrorSpy).toHaveBeenCalledWith( + expect.stringContaining( + 'Attempted to invoke queryFn when set to skipToken', + ), + ) + await expect( + (resolved as unknown as () => Promise)(), + ).rejects.toThrow('Missing queryFn: \'["skip"]\'') + + consoleErrorSpy.mockRestore() + }) + }) + describe('shouldThrowError', () => { it('should return the result of executing throwOnError if throwOnError parameter is a function', () => { const throwOnError = (error: Error) => error.message === 'test error' From 902e5b96d5256318552ffed4bd51e863ed3c6f6a Mon Sep 17 00:00:00 2001 From: KimHyeongRae0 Date: Fri, 24 Apr 2026 01:40:41 +0900 Subject: [PATCH 2/2] test(query-core/utils): use typed QueryFunctionContext in ensureQueryFn tests Replace the `(resolved as unknown as () => Promise<...>)()` double-cast with `resolved(context)` using a shared `QueryFunctionContext` fixture. Per review suggestions on #10527. --- .../query-core/src/__tests__/utils.test.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/query-core/src/__tests__/utils.test.tsx b/packages/query-core/src/__tests__/utils.test.tsx index caccf2b3f16..69c72a50aa8 100644 --- a/packages/query-core/src/__tests__/utils.test.tsx +++ b/packages/query-core/src/__tests__/utils.test.tsx @@ -18,6 +18,7 @@ import { skipToken, } from '../utils' import { Mutation } from '../mutation' +import type { QueryFunctionContext } from '..' describe('core/utils', () => { describe('hashQueryKeyByOptions', () => { @@ -537,6 +538,8 @@ describe('core/utils', () => { }) describe('ensureQueryFn', () => { + const context = {} as QueryFunctionContext + it('should return a function that resolves to initialPromise when queryFn is missing and initialPromise is provided', async () => { const initialPromise = Promise.resolve('initial-data') @@ -545,9 +548,7 @@ describe('core/utils', () => { { initialPromise }, ) - await expect( - (resolved as unknown as () => Promise)(), - ).resolves.toBe('initial-data') + await expect(resolved(context)).resolves.toBe('initial-data') }) it('should return a function that rejects when initialPromise rejects', async () => { @@ -559,9 +560,7 @@ describe('core/utils', () => { { initialPromise }, ) - await expect( - (resolved as unknown as () => Promise)(), - ).rejects.toBe(error) + await expect(resolved(context)).rejects.toBe(error) }) it('should return a function that rejects with missing queryFn error when queryFn is set to skipToken', async () => { @@ -579,9 +578,9 @@ describe('core/utils', () => { 'Attempted to invoke queryFn when set to skipToken', ), ) - await expect( - (resolved as unknown as () => Promise)(), - ).rejects.toThrow('Missing queryFn: \'["skip"]\'') + await expect(resolved(context)).rejects.toThrow( + 'Missing queryFn: \'["skip"]\'', + ) consoleErrorSpy.mockRestore() })