Skip to content

Commit 6a52926

Browse files
test(react-query): initiate queryClient in beforeEach (#9142)
1 parent 81aa02b commit 6a52926

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

packages/react-query/src/__tests__/ssr.test.tsx

+5-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ import { setIsServer } from './utils'
1414
describe('Server Side Rendering', () => {
1515
setIsServer(true)
1616

17+
let queryCache: QueryCache
18+
let queryClient: QueryClient
19+
1720
beforeEach(() => {
1821
vi.useFakeTimers()
22+
queryCache = new QueryCache()
23+
queryClient = new QueryClient({ queryCache })
1924
})
2025

2126
afterEach(() => {
2227
vi.useRealTimers()
2328
})
2429

2530
it('should not trigger fetch', () => {
26-
const queryCache = new QueryCache()
27-
const queryClient = new QueryClient({ queryCache })
2831
const key = queryKey()
2932
const queryFn = vi.fn().mockReturnValue('data')
3033

@@ -52,8 +55,6 @@ describe('Server Side Rendering', () => {
5255
})
5356

5457
it('should add prefetched data to cache', async () => {
55-
const queryCache = new QueryCache()
56-
const queryClient = new QueryClient({ queryCache })
5758
const key = queryKey()
5859
const fetchFn = () => Promise.resolve('data')
5960
const data = await queryClient.fetchQuery({
@@ -66,8 +67,6 @@ describe('Server Side Rendering', () => {
6667
})
6768

6869
it('should return existing data from the cache', async () => {
69-
const queryCache = new QueryCache()
70-
const queryClient = new QueryClient({ queryCache })
7170
const key = queryKey()
7271
const queryFn = vi.fn(async () => {
7372
await vi.advanceTimersByTimeAsync(10)
@@ -102,9 +101,6 @@ describe('Server Side Rendering', () => {
102101
it('should add initialData to the cache', () => {
103102
const key = queryKey()
104103

105-
const queryCache = new QueryCache()
106-
const queryClient = new QueryClient({ queryCache })
107-
108104
function Page() {
109105
const [page, setPage] = React.useState(1)
110106
const { data } = useQuery({
@@ -134,8 +130,6 @@ describe('Server Side Rendering', () => {
134130
})
135131

136132
it('useInfiniteQuery should return the correct state', async () => {
137-
const queryCache = new QueryCache()
138-
const queryClient = new QueryClient({ queryCache })
139133
const key = queryKey()
140134
const queryFn = vi.fn(async () => {
141135
await vi.advanceTimersByTimeAsync(5)

packages/react-query/src/__tests__/useMutation.test.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ import {
1414
import type { UseMutationResult } from '../types'
1515

1616
describe('useMutation', () => {
17-
let queryCache = new QueryCache()
18-
let mutationCache = new MutationCache()
19-
let queryClient = new QueryClient({
20-
queryCache,
21-
mutationCache,
22-
})
17+
let queryCache: QueryCache
18+
let mutationCache: MutationCache
19+
let queryClient: QueryClient
2320

2421
beforeEach(() => {
2522
queryCache = new QueryCache()
2623
mutationCache = new MutationCache()
2724
queryClient = new QueryClient({
2825
queryCache,
26+
mutationCache,
2927
})
3028
vi.useFakeTimers()
3129
})

packages/react-query/src/__tests__/useQuery.test.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ import type { DefinedUseQueryResult, QueryFunction, UseQueryResult } from '..'
2626
import type { Mock } from 'vitest'
2727

2828
describe('useQuery', () => {
29-
let queryCache = new QueryCache()
30-
let queryClient = new QueryClient({
31-
queryCache,
32-
})
29+
let queryCache: QueryCache
30+
let queryClient: QueryClient
3331

3432
beforeEach(() => {
3533
queryCache = new QueryCache()

0 commit comments

Comments
 (0)