Skip to content

Commit f168555

Browse files
test({react,preact,solid}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation (#10356)
* test({react,preact}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation * test(solid-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation * test({react,preact}-query): add 'queryClient.clear()' to 'afterEach' in 'useQuery' and 'useMutation' * test({react,preact}-query): add 'queryClient.clear()' to 'afterEach' in 'ssr' and 'suspense' * test({react,preact}-query/ssr): remove redundant 'queryCache.clear()' calls in individual tests * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7e5fe21 commit f168555

30 files changed

Lines changed: 159 additions & 109 deletions

packages/preact-query/src/__tests__/QueryResetErrorBoundary.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ import { ErrorBoundary } from './ErrorBoundary'
1717
import { renderWithClient } from './utils'
1818

1919
describe('QueryErrorResetBoundary', () => {
20+
let queryCache: QueryCache
21+
let queryClient: QueryClient
22+
2023
beforeEach(() => {
2124
vi.useFakeTimers()
25+
queryCache = new QueryCache()
26+
queryClient = new QueryClient({ queryCache })
2227
})
2328

2429
afterEach(() => {
2530
vi.useRealTimers()
31+
queryClient.clear()
2632
})
2733

28-
const queryCache = new QueryCache()
29-
const queryClient = new QueryClient({ queryCache })
30-
3134
describe('useQuery', () => {
3235
it('should retry fetch if the reset error boundary has been reset', async () => {
3336
const consoleMock = vi

packages/preact-query/src/__tests__/fine-grained-persister.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ import { QueryCache, QueryClient, hashKey, useQuery } from '..'
1010
import { renderWithClient } from './utils'
1111

1212
describe('fine grained persister', () => {
13+
let queryCache: QueryCache
14+
let queryClient: QueryClient
15+
1316
beforeEach(() => {
1417
vi.useFakeTimers()
18+
queryCache = new QueryCache()
19+
queryClient = new QueryClient({ queryCache })
1520
})
1621

1722
afterEach(() => {
1823
vi.useRealTimers()
24+
queryClient.clear()
1925
})
2026

21-
const queryCache = new QueryCache()
22-
const queryClient = new QueryClient({ queryCache })
23-
2427
it('should restore query state from persister and not refetch', async () => {
2528
const key = queryKey()
2629
const hash = hashKey(key)

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('Server Side Rendering', () => {
2727

2828
afterEach(() => {
2929
vi.useRealTimers()
30+
queryClient.clear()
3031
})
3132

3233
it('should not trigger fetch', () => {
@@ -53,8 +54,6 @@ describe('Server Side Rendering', () => {
5354

5455
expect(markup).toContain('status pending')
5556
expect(queryFn).toHaveBeenCalledTimes(0)
56-
57-
queryCache.clear()
5857
})
5958

6059
it('should add prefetched data to cache', async () => {
@@ -70,8 +69,6 @@ describe('Server Side Rendering', () => {
7069

7170
expect(data).toBe('data')
7271
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
73-
74-
queryCache.clear()
7572
})
7673

7774
it('should return existing data from the cache', async () => {
@@ -101,8 +98,6 @@ describe('Server Side Rendering', () => {
10198

10299
expect(markup).toContain('status success')
103100
expect(queryFn).toHaveBeenCalledTimes(1)
104-
105-
queryCache.clear()
106101
})
107102

108103
it('should add initialData to the cache', () => {
@@ -133,8 +128,6 @@ describe('Server Side Rendering', () => {
133128
const keys = queryCache.getAll().map((query) => query.queryKey)
134129

135130
expect(keys).toEqual([[key, 1]])
136-
137-
queryCache.clear()
138131
})
139132

140133
it('useMutationState should return empty array', () => {
@@ -151,8 +144,6 @@ describe('Server Side Rendering', () => {
151144
)
152145

153146
expect(markup).toContain('mutationState: 0')
154-
155-
queryCache.clear()
156147
})
157148

158149
it('useInfiniteQuery should return the correct state', async () => {
@@ -190,7 +181,5 @@ describe('Server Side Rendering', () => {
190181

191182
expect(markup).toContain('page 1')
192183
expect(queryFn).toHaveBeenCalledTimes(1)
193-
194-
queryCache.clear()
195184
})
196185
})

packages/preact-query/src/__tests__/suspense.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('Suspense Timer Tests', () => {
5353

5454
afterEach(() => {
5555
vi.useRealTimers()
56+
queryClient.clear()
5657
})
5758

5859
it('should enforce minimum staleTime of 1000ms when using suspense with number', async () => {

packages/preact-query/src/__tests__/useInfiniteQuery.test.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,25 @@ const fetchItems = async (
4545
}
4646

4747
describe('useInfiniteQuery', () => {
48+
let queryCache: QueryCache
49+
let queryClient: QueryClient
50+
4851
beforeEach(() => {
4952
vi.useFakeTimers()
53+
queryCache = new QueryCache()
54+
queryClient = new QueryClient({
55+
queryCache,
56+
defaultOptions: {
57+
queries: {
58+
experimental_prefetchInRender: true,
59+
},
60+
},
61+
})
5062
})
5163

5264
afterEach(() => {
5365
vi.useRealTimers()
54-
})
55-
56-
const queryCache = new QueryCache()
57-
const queryClient = new QueryClient({
58-
queryCache,
59-
defaultOptions: {
60-
queries: {
61-
experimental_prefetchInRender: true,
62-
},
63-
},
66+
queryClient.clear()
6467
})
6568

6669
it('should return the correct states for a successful query', async () => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('useMutation', () => {
2929

3030
afterEach(() => {
3131
vi.useRealTimers()
32+
queryClient.clear()
3233
})
3334

3435
it('should be able to reset `data`', async () => {

packages/preact-query/src/__tests__/usePrefetchInfiniteQuery.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ const generateInfiniteQueryOptions = (
4242
}
4343

4444
describe('usePrefetchInfiniteQuery', () => {
45+
let queryCache: QueryCache
46+
let queryClient: QueryClient
47+
4548
beforeEach(() => {
4649
vi.useFakeTimers()
50+
queryCache = new QueryCache()
51+
queryClient = new QueryClient({ queryCache })
4752
})
4853

4954
afterEach(() => {
55+
vi.useRealTimers()
5056
queryClient.clear()
5157
Fallback.mockClear()
52-
vi.useRealTimers()
5358
})
5459

55-
const queryCache = new QueryCache()
56-
const queryClient = new QueryClient({ queryCache })
57-
5860
const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)
5961

6062
function Suspended<T = unknown>(props: {

packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ const generateQueryFn = (data: string) =>
2525
})
2626

2727
describe('usePrefetchQuery', () => {
28-
const queryCache = new QueryCache()
29-
const queryClient = new QueryClient({ queryCache })
28+
let queryCache: QueryCache
29+
let queryClient: QueryClient
3030

3131
beforeEach(() => {
3232
vi.useFakeTimers()
33+
queryCache = new QueryCache()
34+
queryClient = new QueryClient({ queryCache })
3335
})
3436

3537
afterEach(() => {
3638
vi.useRealTimers()
39+
queryClient.clear()
3740
})
3841

3942
function Suspended<TData = unknown>(props: {

packages/preact-query/src/__tests__/useQueries.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ import { ErrorBoundary } from './ErrorBoundary'
3131
import { renderWithClient } from './utils'
3232

3333
describe('useQueries', () => {
34+
let queryCache: QueryCache
35+
let queryClient: QueryClient
36+
3437
beforeEach(() => {
3538
vi.useFakeTimers()
39+
queryCache = new QueryCache()
40+
queryClient = new QueryClient({ queryCache })
3641
})
3742

3843
afterEach(() => {
3944
vi.useRealTimers()
45+
queryClient.clear()
4046
})
4147

42-
const queryCache = new QueryCache()
43-
const queryClient = new QueryClient({ queryCache })
44-
4548
it('should return the correct states', async () => {
4649
const key1 = queryKey()
4750
const key2 = queryKey()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('useQuery', () => {
4848

4949
afterEach(() => {
5050
vi.useRealTimers()
51+
queryClient.clear()
5152
})
5253

5354
// See https://github.com/tannerlinsley/react-query/issues/105

0 commit comments

Comments
 (0)