From 9c8055b9aceebff6b3cbe072e697f264d5852af4 Mon Sep 17 00:00:00 2001 From: Karl MAZIER Date: Tue, 21 Jul 2026 14:11:36 +0200 Subject: [PATCH] test(query-core): assert exact values instead of loose matchers in remaining tests --- .../src/__tests__/hydration.test.tsx | 10 +++++---- .../src/__tests__/queriesObserver.test.tsx | 4 +++- .../query-core/src/__tests__/query.test.tsx | 21 +++++++++---------- .../src/__tests__/queryCache.test.tsx | 7 ++++--- .../src/__tests__/queryClient.test.tsx | 12 +++++------ 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/query-core/src/__tests__/hydration.test.tsx b/packages/query-core/src/__tests__/hydration.test.tsx index 3c61a7ba77..389226bd5b 100644 --- a/packages/query-core/src/__tests__/hydration.test.tsx +++ b/packages/query-core/src/__tests__/hydration.test.tsx @@ -155,7 +155,7 @@ describe('dehydration and rehydration', () => { hydrate(hydrationClient, parsed) expect(hydrationCache.find({ queryKey: key })?.state.data).toBe('string') await vi.advanceTimersByTimeAsync(100) - expect(hydrationCache.find({ queryKey: key })).toBeTruthy() + expect(hydrationCache.find({ queryKey: key })?.state.data).toBe('string') queryClient.clear() hydrationClient.clear() @@ -330,9 +330,11 @@ describe('dehydration and rehydration', () => { const hydrationClient = new QueryClient({ queryCache: hydrationCache }) hydrate(hydrationClient, parsed) - expect(hydrationCache.find({ queryKey: successKey })).toBeTruthy() - expect(hydrationCache.find({ queryKey: loadingKey })).toBeFalsy() - expect(hydrationCache.find({ queryKey: errorKey })).toBeFalsy() + expect(hydrationCache.find({ queryKey: successKey })?.state.data).toBe( + 'success', + ) + expect(hydrationCache.find({ queryKey: loadingKey })).toBeUndefined() + expect(hydrationCache.find({ queryKey: errorKey })).toBeUndefined() queryClient.clear() hydrationClient.clear() diff --git a/packages/query-core/src/__tests__/queriesObserver.test.tsx b/packages/query-core/src/__tests__/queriesObserver.test.tsx index 171d1493c7..4e940fca40 100644 --- a/packages/query-core/src/__tests__/queriesObserver.test.tsx +++ b/packages/query-core/src/__tests__/queriesObserver.test.tsx @@ -149,7 +149,9 @@ describe('queriesObserver', () => { const queryCache = queryClient.getQueryCache() expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined() - expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined() + expect( + queryCache.find({ queryKey: key2, type: 'active' })?.queryKey, + ).toEqual(key2) unsubscribe() expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined() expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined() diff --git a/packages/query-core/src/__tests__/query.test.tsx b/packages/query-core/src/__tests__/query.test.tsx index ac4c10ea5b..fdac03e398 100644 --- a/packages/query-core/src/__tests__/query.test.tsx +++ b/packages/query-core/src/__tests__/query.test.tsx @@ -279,7 +279,6 @@ describe('query', () => { expect(queryFn).toHaveBeenCalledTimes(1) const args = queryFn.mock.calls[0]![0] - expect(args).toBeDefined() expect(args.pageParam).toBeUndefined() expect(args.queryKey).toEqual(key) expect(args.signal).toBeInstanceOf(AbortSignal) @@ -601,9 +600,9 @@ describe('query', () => { queryFn: () => 'data', gcTime: 0, }) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending') const unsubscribe = observer.subscribe(() => undefined) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending') unsubscribe() await vi.advanceTimersByTimeAsync(0) @@ -619,11 +618,11 @@ describe('query', () => { }) const unsubscribe = observer.subscribe(() => undefined) await vi.advanceTimersByTimeAsync(20) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') observer.refetch() unsubscribe() // unsubscribe should not remove even though gcTime has elapsed b/c query is still fetching - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') // should be removed after an additional staleTime wait await vi.advanceTimersByTimeAsync(30) expect(queryCache.find({ queryKey: key })).toBeUndefined() @@ -636,16 +635,16 @@ describe('query', () => { queryFn: () => 'data', gcTime: 0, }) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending') const unsubscribe = observer.subscribe(() => undefined) await vi.advanceTimersByTimeAsync(100) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') unsubscribe() await vi.advanceTimersByTimeAsync(100) expect(queryCache.find({ queryKey: key })).toBeUndefined() queryClient.setQueryData(key, 'data') await vi.advanceTimersByTimeAsync(100) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') }) it('should return proper count of observers', () => { @@ -1103,9 +1102,9 @@ describe('query', () => { expect(queryFn).toHaveBeenCalledTimes(1) expect(query.state.status).toBe('error') - expect( - query.state.error?.message.includes('Maximum call stack size exceeded'), - ).toBeTruthy() + expect(query.state.error?.message).toContain( + 'Maximum call stack size exceeded', + ) expect(consoleMock).toHaveBeenCalledWith( expect.stringContaining( diff --git a/packages/query-core/src/__tests__/queryCache.test.tsx b/packages/query-core/src/__tests__/queryCache.test.tsx index f381bcbccf..307cb2d987 100644 --- a/packages/query-core/src/__tests__/queryCache.test.tsx +++ b/packages/query-core/src/__tests__/queryCache.test.tsx @@ -72,8 +72,9 @@ describe('queryCache', () => { 'observerResultsUpdated', // 8. Observer result updated -> stale ]) + const cachedQuery = queryCache.find({ queryKey: key }) queries.forEach((query) => { - expect(query).toBeDefined() + expect(query).toBe(cachedQuery) }) unsubscribe() @@ -161,7 +162,7 @@ describe('queryCache', () => { }) await vi.advanceTimersByTimeAsync(100) const query = queryCache.find({ queryKey: key })! - expect(query).toBeDefined() + expect(query.state.data).toBe('data1') }) it('find should filter correctly with exact set to false', async () => { @@ -172,7 +173,7 @@ describe('queryCache', () => { }) await vi.advanceTimersByTimeAsync(100) const query = queryCache.find({ queryKey: key, exact: false })! - expect(query).toBeDefined() + expect(query.state.data).toBe('data1') }) }) diff --git a/packages/query-core/src/__tests__/queryClient.test.tsx b/packages/query-core/src/__tests__/queryClient.test.tsx index 2ed4c4c193..3c2822130a 100644 --- a/packages/query-core/src/__tests__/queryClient.test.tsx +++ b/packages/query-core/src/__tests__/queryClient.test.tsx @@ -965,9 +965,9 @@ describe('queryClient', () => { queryFn: () => 'data', gcTime: 10, }) - expect(queryCache.find({ queryKey: key })).toBeDefined() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') await vi.advanceTimersByTimeAsync(15) - expect(queryCache.find({ queryKey: key })).not.toBeDefined() + expect(queryCache.find({ queryKey: key })).toBeUndefined() }) }) @@ -979,7 +979,7 @@ describe('queryClient', () => { // check the query was added to the cache await queryClient.prefetchQuery({ queryKey: key, queryFn: fetchFn }) - expect(queryCache.find({ queryKey: key })).toBeTruthy() + expect(queryCache.find({ queryKey: key })?.state.data).toBe('data') // check the error doesn't occur expect(() => @@ -987,7 +987,7 @@ describe('queryClient', () => { ).not.toThrow() // check query was successful removed - expect(queryCache.find({ queryKey: key })).toBeFalsy() + expect(queryCache.find({ queryKey: key })).toBeUndefined() }) }) @@ -1638,7 +1638,6 @@ describe('queryClient', () => { state = queryClient.getQueryState(key) - expect(state).toBeTruthy() expect(state?.data).toBeUndefined() expect(state?.status).toEqual('pending') expect(state?.fetchStatus).toEqual('idle') @@ -1660,7 +1659,6 @@ describe('queryClient', () => { state = queryClient.getQueryState(key) - expect(state).toBeTruthy() expect(state?.data).toEqual('initial') }) @@ -1936,7 +1934,7 @@ describe('queryClient', () => { // still paused because we are still offline expect( newQueryClient.getMutationCache().getAll()[0]?.state.isPaused, - ).toBeTruthy() + ).toBe(true) await newQueryClient.resumePausedMutations()