@tanstack/query-core 5.0.0-alpha.3 → 5.0.0-alpha.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
  2. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
  3. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
  4. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  5. package/build/lib/focusManager.d.ts +1 -0
  6. package/build/lib/focusManager.d.ts.map +1 -0
  7. package/build/lib/focusManager.esm.js +31 -15
  8. package/build/lib/focusManager.esm.js.map +1 -1
  9. package/build/lib/focusManager.js +31 -15
  10. package/build/lib/focusManager.js.map +1 -1
  11. package/build/lib/hydration.d.ts +3 -6
  12. package/build/lib/hydration.d.ts.map +1 -0
  13. package/build/lib/hydration.esm.js +9 -20
  14. package/build/lib/hydration.esm.js.map +1 -1
  15. package/build/lib/hydration.js +9 -20
  16. package/build/lib/hydration.js.map +1 -1
  17. package/build/lib/hydration.mjs +4 -18
  18. package/build/lib/hydration.mjs.map +1 -1
  19. package/build/lib/index.d.ts +4 -3
  20. package/build/lib/index.d.ts.map +1 -0
  21. package/build/lib/infiniteQueryBehavior.d.ts +3 -2
  22. package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
  23. package/build/lib/infiniteQueryBehavior.esm.js +52 -48
  24. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  25. package/build/lib/infiniteQueryBehavior.js +52 -48
  26. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  27. package/build/lib/infiniteQueryBehavior.mjs +48 -45
  28. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  29. package/build/lib/infiniteQueryObserver.d.ts +7 -6
  30. package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
  31. package/build/lib/infiniteQueryObserver.esm.js +7 -8
  32. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  33. package/build/lib/infiniteQueryObserver.js +7 -8
  34. package/build/lib/infiniteQueryObserver.js.map +1 -1
  35. package/build/lib/infiniteQueryObserver.mjs +4 -6
  36. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  37. package/build/lib/mutation.d.ts +1 -0
  38. package/build/lib/mutation.d.ts.map +1 -0
  39. package/build/lib/mutation.esm.js +127 -102
  40. package/build/lib/mutation.esm.js.map +1 -1
  41. package/build/lib/mutation.js +127 -102
  42. package/build/lib/mutation.js.map +1 -1
  43. package/build/lib/mutationCache.d.ts +1 -0
  44. package/build/lib/mutationCache.d.ts.map +1 -0
  45. package/build/lib/mutationCache.esm.js +34 -20
  46. package/build/lib/mutationCache.esm.js.map +1 -1
  47. package/build/lib/mutationCache.js +34 -20
  48. package/build/lib/mutationCache.js.map +1 -1
  49. package/build/lib/mutationCache.mjs +5 -4
  50. package/build/lib/mutationCache.mjs.map +1 -1
  51. package/build/lib/mutationObserver.d.ts +1 -0
  52. package/build/lib/mutationObserver.d.ts.map +1 -0
  53. package/build/lib/mutationObserver.esm.js +82 -52
  54. package/build/lib/mutationObserver.esm.js.map +1 -1
  55. package/build/lib/mutationObserver.js +82 -52
  56. package/build/lib/mutationObserver.js.map +1 -1
  57. package/build/lib/mutationObserver.mjs +1 -1
  58. package/build/lib/mutationObserver.mjs.map +1 -1
  59. package/build/lib/notifyManager.d.ts +1 -0
  60. package/build/lib/notifyManager.d.ts.map +1 -0
  61. package/build/lib/onlineManager.d.ts +1 -0
  62. package/build/lib/onlineManager.d.ts.map +1 -0
  63. package/build/lib/onlineManager.esm.js +29 -14
  64. package/build/lib/onlineManager.esm.js.map +1 -1
  65. package/build/lib/onlineManager.js +29 -14
  66. package/build/lib/onlineManager.js.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +1 -0
  68. package/build/lib/queriesObserver.d.ts.map +1 -0
  69. package/build/lib/queriesObserver.esm.js +98 -68
  70. package/build/lib/queriesObserver.esm.js.map +1 -1
  71. package/build/lib/queriesObserver.js +98 -68
  72. package/build/lib/queriesObserver.js.map +1 -1
  73. package/build/lib/queriesObserver.mjs +7 -6
  74. package/build/lib/queriesObserver.mjs.map +1 -1
  75. package/build/lib/query.d.ts +5 -3
  76. package/build/lib/query.d.ts.map +1 -0
  77. package/build/lib/query.esm.js +211 -162
  78. package/build/lib/query.esm.js.map +1 -1
  79. package/build/lib/query.js +211 -162
  80. package/build/lib/query.js.map +1 -1
  81. package/build/lib/query.mjs +2 -2
  82. package/build/lib/query.mjs.map +1 -1
  83. package/build/lib/queryCache.d.ts +1 -1
  84. package/build/lib/queryCache.d.ts.map +1 -0
  85. package/build/lib/queryCache.esm.js +20 -13
  86. package/build/lib/queryCache.esm.js.map +1 -1
  87. package/build/lib/queryCache.js +20 -13
  88. package/build/lib/queryCache.js.map +1 -1
  89. package/build/lib/queryCache.mjs +6 -5
  90. package/build/lib/queryCache.mjs.map +1 -1
  91. package/build/lib/queryClient.d.ts +1 -0
  92. package/build/lib/queryClient.d.ts.map +1 -0
  93. package/build/lib/queryClient.esm.js +113 -71
  94. package/build/lib/queryClient.esm.js.map +1 -1
  95. package/build/lib/queryClient.js +113 -71
  96. package/build/lib/queryClient.js.map +1 -1
  97. package/build/lib/queryClient.mjs +17 -13
  98. package/build/lib/queryClient.mjs.map +1 -1
  99. package/build/lib/queryObserver.d.ts +3 -4
  100. package/build/lib/queryObserver.d.ts.map +1 -0
  101. package/build/lib/queryObserver.esm.js +303 -214
  102. package/build/lib/queryObserver.esm.js.map +1 -1
  103. package/build/lib/queryObserver.js +301 -212
  104. package/build/lib/queryObserver.js.map +1 -1
  105. package/build/lib/queryObserver.mjs +16 -28
  106. package/build/lib/queryObserver.mjs.map +1 -1
  107. package/build/lib/removable.d.ts +1 -0
  108. package/build/lib/removable.d.ts.map +1 -0
  109. package/build/lib/removable.esm.js +13 -6
  110. package/build/lib/removable.esm.js.map +1 -1
  111. package/build/lib/removable.js +13 -6
  112. package/build/lib/removable.js.map +1 -1
  113. package/build/lib/retryer.d.ts +1 -0
  114. package/build/lib/retryer.d.ts.map +1 -0
  115. package/build/lib/retryer.esm.js +15 -14
  116. package/build/lib/retryer.esm.js.map +1 -1
  117. package/build/lib/retryer.js +15 -14
  118. package/build/lib/retryer.js.map +1 -1
  119. package/build/lib/subscribable.d.ts +2 -1
  120. package/build/lib/subscribable.d.ts.map +1 -0
  121. package/build/lib/subscribable.esm.js +4 -4
  122. package/build/lib/subscribable.esm.js.map +1 -1
  123. package/build/lib/subscribable.js +4 -4
  124. package/build/lib/subscribable.js.map +1 -1
  125. package/build/lib/subscribable.mjs +4 -4
  126. package/build/lib/subscribable.mjs.map +1 -1
  127. package/build/lib/tests/focusManager.test.d.ts +1 -0
  128. package/build/lib/tests/focusManager.test.d.ts.map +1 -0
  129. package/build/lib/tests/hydration.test.d.ts +1 -0
  130. package/build/lib/tests/hydration.test.d.ts.map +1 -0
  131. package/build/lib/tests/infiniteQueryBehavior.test.d.ts +1 -0
  132. package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
  133. package/build/lib/tests/infiniteQueryObserver.test.d.ts +1 -0
  134. package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
  135. package/build/lib/tests/mutationCache.test.d.ts +1 -0
  136. package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
  137. package/build/lib/tests/mutationObserver.test.d.ts +1 -0
  138. package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
  139. package/build/lib/tests/mutations.test.d.ts +1 -0
  140. package/build/lib/tests/mutations.test.d.ts.map +1 -0
  141. package/build/lib/tests/notifyManager.test.d.ts +1 -0
  142. package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
  143. package/build/lib/tests/onlineManager.test.d.ts +1 -0
  144. package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
  145. package/build/lib/tests/queriesObserver.test.d.ts +1 -0
  146. package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
  147. package/build/lib/tests/query.test.d.ts +1 -0
  148. package/build/lib/tests/query.test.d.ts.map +1 -0
  149. package/build/lib/tests/queryCache.test.d.ts +1 -0
  150. package/build/lib/tests/queryCache.test.d.ts.map +1 -0
  151. package/build/lib/tests/queryClient.test.d.ts +1 -0
  152. package/build/lib/tests/queryClient.test.d.ts.map +1 -0
  153. package/build/lib/tests/queryObserver.test.d.ts +1 -0
  154. package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
  155. package/build/lib/tests/utils.d.ts +5 -5
  156. package/build/lib/tests/utils.d.ts.map +1 -0
  157. package/build/lib/tests/utils.test.d.ts +1 -0
  158. package/build/lib/tests/utils.test.d.ts.map +1 -0
  159. package/build/lib/types.d.ts +12 -21
  160. package/build/lib/types.d.ts.map +1 -0
  161. package/build/lib/utils.d.ts +1 -0
  162. package/build/lib/utils.d.ts.map +1 -0
  163. package/build/lib/utils.esm.js +1 -1
  164. package/build/lib/utils.esm.js.map +1 -1
  165. package/build/lib/utils.js +1 -1
  166. package/build/lib/utils.js.map +1 -1
  167. package/build/umd/index.development.js +113 -131
  168. package/build/umd/index.development.js.map +1 -1
  169. package/build/umd/index.production.js +1 -1
  170. package/build/umd/index.production.js.map +1 -1
  171. package/package.json +2 -2
  172. package/src/hydration.ts +18 -37
  173. package/src/index.ts +2 -4
  174. package/src/infiniteQueryBehavior.ts +52 -60
  175. package/src/infiniteQueryObserver.ts +15 -10
  176. package/src/mutationCache.ts +4 -4
  177. package/src/mutationObserver.ts +1 -1
  178. package/src/queriesObserver.ts +11 -11
  179. package/src/query.ts +8 -6
  180. package/src/queryCache.ts +5 -8
  181. package/src/queryClient.ts +18 -19
  182. package/src/queryObserver.ts +32 -41
  183. package/src/subscribable.ts +5 -5
  184. package/src/tests/focusManager.test.tsx +12 -14
  185. package/src/tests/hydration.test.tsx +22 -17
  186. package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
  187. package/src/tests/infiniteQueryObserver.test.tsx +62 -1
  188. package/src/tests/mutationCache.test.tsx +11 -10
  189. package/src/tests/mutationObserver.test.tsx +3 -2
  190. package/src/tests/mutations.test.tsx +11 -10
  191. package/src/tests/notifyManager.test.tsx +7 -6
  192. package/src/tests/onlineManager.test.tsx +12 -17
  193. package/src/tests/queriesObserver.test.tsx +18 -17
  194. package/src/tests/query.test.tsx +19 -18
  195. package/src/tests/queryCache.test.tsx +12 -28
  196. package/src/tests/queryClient.test.tsx +82 -49
  197. package/src/tests/queryObserver.test.tsx +131 -9
  198. package/src/tests/utils.test.tsx +2 -1
  199. package/src/tests/utils.ts +5 -4
  200. package/src/types.ts +22 -20
@@ -2,6 +2,7 @@ import { waitFor } from '@testing-library/react'
2
2
  import { createQueryClient, sleep } from './utils'
3
3
  import type { QueryClient } from '..'
4
4
  import { MutationObserver } from '..'
5
+ import { vi } from 'vitest'
5
6
 
6
7
  describe('mutationObserver', () => {
7
8
  let queryClient: QueryClient
@@ -23,8 +24,8 @@ describe('mutationObserver', () => {
23
24
  },
24
25
  })
25
26
 
26
- const subscription1Handler = jest.fn()
27
- const subscription2Handler = jest.fn()
27
+ const subscription1Handler = vi.fn()
28
+ const subscription2Handler = vi.fn()
28
29
 
29
30
  const unsubscribe1 = mutation.subscribe(subscription1Handler)
30
31
  const unsubscribe2 = mutation.subscribe(subscription2Handler)
@@ -2,6 +2,7 @@ import type { QueryClient } from '..'
2
2
  import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
3
3
  import type { MutationState } from '../mutation'
4
4
  import { MutationObserver } from '../mutationObserver'
5
+ import { vi } from 'vitest'
5
6
  import { waitFor } from '@testing-library/react'
6
7
 
7
8
  describe('mutations', () => {
@@ -33,7 +34,7 @@ describe('mutations', () => {
33
34
 
34
35
  test('setMutationDefaults should be able to set defaults', async () => {
35
36
  const key = queryKey()
36
- const fn = jest.fn()
37
+ const fn = vi.fn()
37
38
 
38
39
  queryClient.setMutationDefaults(key, {
39
40
  mutationFn: fn,
@@ -250,9 +251,9 @@ describe('mutations', () => {
250
251
  test('should be able to restore a mutation', async () => {
251
252
  const key = queryKey()
252
253
 
253
- const onMutate = jest.fn()
254
- const onSuccess = jest.fn()
255
- const onSettled = jest.fn()
254
+ const onMutate = vi.fn()
255
+ const onSuccess = vi.fn()
256
+ const onSettled = vi.fn()
256
257
 
257
258
  queryClient.setMutationDefaults(key, {
258
259
  mutationFn: async (text: string) => text,
@@ -317,7 +318,7 @@ describe('mutations', () => {
317
318
  const observer = new MutationObserver(queryClient, {})
318
319
  const currentMutation = mutationCache.build(queryClient, {})
319
320
 
320
- const fn = jest.fn()
321
+ const fn = vi.fn()
321
322
 
322
323
  const unsubscribe = mutationCache.subscribe((event) => {
323
324
  fn(event.type)
@@ -348,8 +349,8 @@ describe('mutations', () => {
348
349
  })
349
350
 
350
351
  test('mutate update the mutation state even without an active subscription', async () => {
351
- const onSuccess = jest.fn()
352
- const onSettled = jest.fn()
352
+ const onSuccess = vi.fn()
353
+ const onSettled = vi.fn()
353
354
 
354
355
  const mutation = new MutationObserver(queryClient, {
355
356
  mutationFn: async () => {
@@ -364,8 +365,8 @@ describe('mutations', () => {
364
365
  })
365
366
 
366
367
  test('mutate update the mutation state even without an active subscription', async () => {
367
- const onSuccess = jest.fn()
368
- const onSettled = jest.fn()
368
+ const onSuccess = vi.fn()
369
+ const onSettled = vi.fn()
369
370
 
370
371
  const mutation = new MutationObserver(queryClient, {
371
372
  mutationFn: async () => {
@@ -380,7 +381,7 @@ describe('mutations', () => {
380
381
  })
381
382
 
382
383
  test('mutation callbacks should see updated options', async () => {
383
- const onSuccess = jest.fn()
384
+ const onSuccess = vi.fn()
384
385
 
385
386
  const mutation = new MutationObserver(queryClient, {
386
387
  mutationFn: async () => {
@@ -1,10 +1,11 @@
1
1
  import { createNotifyManager } from '../notifyManager'
2
2
  import { sleep } from './utils'
3
+ import { vi } from 'vitest'
3
4
 
4
5
  describe('notifyManager', () => {
5
6
  it('should use default notifyFn', async () => {
6
7
  const notifyManagerTest = createNotifyManager()
7
- const callbackSpy = jest.fn()
8
+ const callbackSpy = vi.fn()
8
9
  notifyManagerTest.schedule(callbackSpy)
9
10
  await sleep(1)
10
11
  expect(callbackSpy).toHaveBeenCalled()
@@ -12,13 +13,13 @@ describe('notifyManager', () => {
12
13
 
13
14
  it('should use default batchNotifyFn', async () => {
14
15
  const notifyManagerTest = createNotifyManager()
15
- const callbackScheduleSpy = jest
16
+ const callbackScheduleSpy = vi
16
17
  .fn()
17
18
  .mockImplementation(async () => await sleep(20))
18
- const callbackBatchLevel2Spy = jest.fn().mockImplementation(async () => {
19
+ const callbackBatchLevel2Spy = vi.fn().mockImplementation(async () => {
19
20
  notifyManagerTest.schedule(callbackScheduleSpy)
20
21
  })
21
- const callbackBatchLevel1Spy = jest.fn().mockImplementation(async () => {
22
+ const callbackBatchLevel1Spy = vi.fn().mockImplementation(async () => {
22
23
  notifyManagerTest.batch(callbackBatchLevel2Spy)
23
24
  })
24
25
 
@@ -32,13 +33,13 @@ describe('notifyManager', () => {
32
33
 
33
34
  it('should notify if error is thrown', async () => {
34
35
  const notifyManagerTest = createNotifyManager()
35
- const notifySpy = jest.fn()
36
+ const notifySpy = vi.fn()
36
37
 
37
38
  notifyManagerTest.setNotifyFunction(notifySpy)
38
39
 
39
40
  try {
40
41
  notifyManagerTest.batch(() => {
41
- notifyManagerTest.schedule(jest.fn)
42
+ notifyManagerTest.schedule(vi.fn)
42
43
  throw new Error('Foo')
43
44
  })
44
45
  } catch {}
@@ -1,5 +1,6 @@
1
1
  import { OnlineManager } from '../onlineManager'
2
2
  import { setIsServer, sleep } from './utils'
3
+ import { vi } from 'vitest'
3
4
 
4
5
  describe('onlineManager', () => {
5
6
  let onlineManager: OnlineManager
@@ -8,7 +9,7 @@ describe('onlineManager', () => {
8
9
  })
9
10
 
10
11
  test('isOnline should return true if navigator is undefined', () => {
11
- const navigatorSpy = jest.spyOn(globalThis, 'navigator', 'get')
12
+ const navigatorSpy = vi.spyOn(globalThis, 'navigator', 'get')
12
13
 
13
14
  // Force navigator to be undefined
14
15
  //@ts-expect-error
@@ -19,7 +20,7 @@ describe('onlineManager', () => {
19
20
  })
20
21
 
21
22
  test('isOnline should return true if navigator.onLine is true', () => {
22
- const navigatorSpy = jest.spyOn(navigator, 'onLine', 'get')
23
+ const navigatorSpy = vi.spyOn(navigator, 'onLine', 'get')
23
24
  navigatorSpy.mockImplementation(() => true)
24
25
 
25
26
  expect(onlineManager.isOnline()).toBeTruthy()
@@ -46,8 +47,8 @@ describe('onlineManager', () => {
46
47
  })
47
48
 
48
49
  test('setEventListener should call previous remove handler when replacing an event listener', () => {
49
- const remove1Spy = jest.fn()
50
- const remove2Spy = jest.fn()
50
+ const remove1Spy = vi.fn()
51
+ const remove2Spy = vi.fn()
51
52
 
52
53
  onlineManager.setEventListener(() => remove1Spy)
53
54
  onlineManager.setEventListener(() => remove2Spy)
@@ -59,7 +60,7 @@ describe('onlineManager', () => {
59
60
  test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
60
61
  const restoreIsServer = setIsServer(true)
61
62
 
62
- const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
63
+ const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
63
64
 
64
65
  const unsubscribe = onlineManager.subscribe(() => undefined)
65
66
 
@@ -76,7 +77,7 @@ describe('onlineManager', () => {
76
77
  // @ts-expect-error
77
78
  globalThis.window.addEventListener = undefined
78
79
 
79
- const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
80
+ const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
80
81
 
81
82
  const unsubscribe = onlineManager.subscribe(() => undefined)
82
83
 
@@ -88,12 +89,9 @@ describe('onlineManager', () => {
88
89
  })
89
90
 
90
91
  test('it should replace default window listener when a new event listener is set', async () => {
91
- const addEventListenerSpy = jest.spyOn(
92
- globalThis.window,
93
- 'addEventListener',
94
- )
92
+ const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
95
93
 
96
- const removeEventListenerSpy = jest.spyOn(
94
+ const removeEventListenerSpy = vi.spyOn(
97
95
  globalThis.window,
98
96
  'removeEventListener',
99
97
  )
@@ -116,12 +114,9 @@ describe('onlineManager', () => {
116
114
  })
117
115
 
118
116
  test('should call removeEventListener when last listener unsubscribes', () => {
119
- const addEventListenerSpy = jest.spyOn(
120
- globalThis.window,
121
- 'addEventListener',
122
- )
117
+ const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
123
118
 
124
- const removeEventListenerSpy = jest.spyOn(
119
+ const removeEventListenerSpy = vi.spyOn(
125
120
  globalThis.window,
126
121
  'removeEventListener',
127
122
  )
@@ -137,7 +132,7 @@ describe('onlineManager', () => {
137
132
  })
138
133
 
139
134
  test('should keep setup function even if last listener unsubscribes', () => {
140
- const setupSpy = jest.fn().mockImplementation(() => () => undefined)
135
+ const setupSpy = vi.fn().mockImplementation(() => () => undefined)
141
136
 
142
137
  onlineManager.setEventListener(setupSpy)
143
138
 
@@ -2,6 +2,7 @@ import { waitFor } from '@testing-library/react'
2
2
  import { sleep, queryKey, createQueryClient } from './utils'
3
3
  import type { QueryClient, QueryObserverResult } from '..'
4
4
  import { QueriesObserver } from '..'
5
+ import { vi } from 'vitest'
5
6
 
6
7
  describe('queriesObserver', () => {
7
8
  let queryClient: QueryClient
@@ -18,8 +19,8 @@ describe('queriesObserver', () => {
18
19
  test('should return an array with all query results', async () => {
19
20
  const key1 = queryKey()
20
21
  const key2 = queryKey()
21
- const queryFn1 = jest.fn().mockReturnValue(1)
22
- const queryFn2 = jest.fn().mockReturnValue(2)
22
+ const queryFn1 = vi.fn().mockReturnValue(1)
23
+ const queryFn2 = vi.fn().mockReturnValue(2)
23
24
  const observer = new QueriesObserver(queryClient, [
24
25
  { queryKey: key1, queryFn: queryFn1 },
25
26
  { queryKey: key2, queryFn: queryFn2 },
@@ -34,11 +35,11 @@ describe('queriesObserver', () => {
34
35
  })
35
36
 
36
37
  test('should still return value for undefined query key', async () => {
37
- const consoleMock = jest.spyOn(console, 'error')
38
+ const consoleMock = vi.spyOn(console, 'error')
38
39
  consoleMock.mockImplementation(() => undefined)
39
40
  const key1 = queryKey()
40
- const queryFn1 = jest.fn().mockReturnValue(1)
41
- const queryFn2 = jest.fn().mockReturnValue(2)
41
+ const queryFn1 = vi.fn().mockReturnValue(1)
42
+ const queryFn2 = vi.fn().mockReturnValue(2)
42
43
  const observer = new QueriesObserver(queryClient, [
43
44
  { queryKey: key1, queryFn: queryFn1 },
44
45
  { queryKey: undefined, queryFn: queryFn2 },
@@ -61,8 +62,8 @@ describe('queriesObserver', () => {
61
62
  test('should update when a query updates', async () => {
62
63
  const key1 = queryKey()
63
64
  const key2 = queryKey()
64
- const queryFn1 = jest.fn().mockReturnValue(1)
65
- const queryFn2 = jest.fn().mockReturnValue(2)
65
+ const queryFn1 = vi.fn().mockReturnValue(1)
66
+ const queryFn2 = vi.fn().mockReturnValue(2)
66
67
  const observer = new QueriesObserver(queryClient, [
67
68
  { queryKey: key1, queryFn: queryFn1 },
68
69
  { queryKey: key2, queryFn: queryFn2 },
@@ -106,8 +107,8 @@ describe('queriesObserver', () => {
106
107
  test('should update when a query is removed', async () => {
107
108
  const key1 = queryKey()
108
109
  const key2 = queryKey()
109
- const queryFn1 = jest.fn().mockReturnValue(1)
110
- const queryFn2 = jest.fn().mockReturnValue(2)
110
+ const queryFn1 = vi.fn().mockReturnValue(1)
111
+ const queryFn2 = vi.fn().mockReturnValue(2)
111
112
  const observer = new QueriesObserver(queryClient, [
112
113
  { queryKey: key1, queryFn: queryFn1 },
113
114
  { queryKey: key2, queryFn: queryFn2 },
@@ -153,8 +154,8 @@ describe('queriesObserver', () => {
153
154
  test('should update when a query changed position', async () => {
154
155
  const key1 = queryKey()
155
156
  const key2 = queryKey()
156
- const queryFn1 = jest.fn().mockReturnValue(1)
157
- const queryFn2 = jest.fn().mockReturnValue(2)
157
+ const queryFn1 = vi.fn().mockReturnValue(1)
158
+ const queryFn2 = vi.fn().mockReturnValue(2)
158
159
  const observer = new QueriesObserver(queryClient, [
159
160
  { queryKey: key1, queryFn: queryFn1 },
160
161
  { queryKey: key2, queryFn: queryFn2 },
@@ -201,8 +202,8 @@ describe('queriesObserver', () => {
201
202
  test('should not update when nothing has changed', async () => {
202
203
  const key1 = queryKey()
203
204
  const key2 = queryKey()
204
- const queryFn1 = jest.fn().mockReturnValue(1)
205
- const queryFn2 = jest.fn().mockReturnValue(2)
205
+ const queryFn1 = vi.fn().mockReturnValue(1)
206
+ const queryFn2 = vi.fn().mockReturnValue(2)
206
207
  const observer = new QueriesObserver(queryClient, [
207
208
  { queryKey: key1, queryFn: queryFn1 },
208
209
  { queryKey: key2, queryFn: queryFn2 },
@@ -245,8 +246,8 @@ describe('queriesObserver', () => {
245
246
  test('should trigger all fetches when subscribed', async () => {
246
247
  const key1 = queryKey()
247
248
  const key2 = queryKey()
248
- const queryFn1 = jest.fn().mockReturnValue(1)
249
- const queryFn2 = jest.fn().mockReturnValue(2)
249
+ const queryFn1 = vi.fn().mockReturnValue(1)
250
+ const queryFn2 = vi.fn().mockReturnValue(2)
250
251
  const observer = new QueriesObserver(queryClient, [
251
252
  { queryKey: key1, queryFn: queryFn1 },
252
253
  { queryKey: key2, queryFn: queryFn2 },
@@ -270,8 +271,8 @@ describe('queriesObserver', () => {
270
271
  },
271
272
  ])
272
273
 
273
- const subscription1Handler = jest.fn()
274
- const subscription2Handler = jest.fn()
274
+ const subscription1Handler = vi.fn()
275
+ const subscription2Handler = vi.fn()
275
276
 
276
277
  const unsubscribe1 = observer.subscribe(subscription1Handler)
277
278
  const unsubscribe2 = observer.subscribe(subscription2Handler)
@@ -12,6 +12,7 @@ import type {
12
12
  } from '..'
13
13
  import { QueryObserver, isCancelledError, onlineManager } from '..'
14
14
  import { waitFor } from '@testing-library/react'
15
+ import { vi } from 'vitest'
15
16
 
16
17
  describe('query', () => {
17
18
  let queryClient: QueryClient
@@ -186,10 +187,10 @@ describe('query', () => {
186
187
  test('should provide context to queryFn', async () => {
187
188
  const key = queryKey()
188
189
 
189
- const queryFn = jest
190
+ const queryFn = vi
190
191
  .fn<
191
- Promise<'data'>,
192
- [QueryFunctionContext<ReturnType<typeof queryKey>>]
192
+ [QueryFunctionContext<ReturnType<typeof queryKey>>],
193
+ Promise<'data'>
193
194
  >()
194
195
  .mockResolvedValue('data')
195
196
 
@@ -273,12 +274,12 @@ describe('query', () => {
273
274
  test('should provide an AbortSignal to the queryFn that provides info about the cancellation state', async () => {
274
275
  const key = queryKey()
275
276
 
276
- const queryFn = jest.fn<
277
- Promise<unknown>,
278
- [QueryFunctionContext<ReturnType<typeof queryKey>>]
277
+ const queryFn = vi.fn<
278
+ [QueryFunctionContext<ReturnType<typeof queryKey>>],
279
+ Promise<unknown>
279
280
  >()
280
- const onAbort = jest.fn()
281
- const abortListener = jest.fn()
281
+ const onAbort = vi.fn()
282
+ const abortListener = vi.fn()
282
283
  let error
283
284
 
284
285
  queryFn.mockImplementation(async ({ signal }) => {
@@ -323,7 +324,7 @@ describe('query', () => {
323
324
  test('should not continue if explicitly cancelled', async () => {
324
325
  const key = queryKey()
325
326
 
326
- const queryFn = jest.fn<unknown, unknown[]>()
327
+ const queryFn = vi.fn<unknown[], unknown>()
327
328
 
328
329
  queryFn.mockImplementation(async () => {
329
330
  await sleep(10)
@@ -355,7 +356,7 @@ describe('query', () => {
355
356
  test('should not error if reset while pending', async () => {
356
357
  const key = queryKey()
357
358
 
358
- const queryFn = jest.fn<unknown, unknown[]>()
359
+ const queryFn = vi.fn<unknown[], unknown>()
359
360
 
360
361
  queryFn.mockImplementation(async () => {
361
362
  await sleep(10)
@@ -382,7 +383,7 @@ describe('query', () => {
382
383
  test('should be able to refetch a cancelled query', async () => {
383
384
  const key = queryKey()
384
385
 
385
- const queryFn = jest.fn<unknown, unknown[]>()
386
+ const queryFn = vi.fn<unknown[], unknown>()
386
387
 
387
388
  queryFn.mockImplementation(async () => {
388
389
  await sleep(50)
@@ -631,7 +632,7 @@ describe('query', () => {
631
632
  it: 'works',
632
633
  }
633
634
 
634
- const queryFn = jest.fn(() => 'data')
635
+ const queryFn = vi.fn(() => 'data')
635
636
 
636
637
  const key = queryKey()
637
638
 
@@ -652,7 +653,7 @@ describe('query', () => {
652
653
  queryFn: () => 'data',
653
654
  })
654
655
 
655
- const refetchSpy = jest.spyOn(observer, 'refetch')
656
+ const refetchSpy = vi.spyOn(observer, 'refetch')
656
657
  const unsubscribe = observer.subscribe(() => undefined)
657
658
  queryCache.onOnline()
658
659
 
@@ -692,7 +693,7 @@ describe('query', () => {
692
693
  })
693
694
  expect(query.getObserversCount()).toEqual(0)
694
695
 
695
- const notifySpy = jest.spyOn(queryCache, 'notify')
696
+ const notifySpy = vi.spyOn(queryCache, 'notify')
696
697
  expect(() => query.removeObserver(observer)).not.toThrow()
697
698
  expect(notifySpy).not.toHaveBeenCalled()
698
699
 
@@ -769,7 +770,7 @@ describe('query', () => {
769
770
  })
770
771
 
771
772
  test('fetch should dispatch an error if the queryFn returns undefined', async () => {
772
- const consoleMock = jest.spyOn(console, 'error')
773
+ const consoleMock = vi.spyOn(console, 'error')
773
774
  consoleMock.mockImplementation(() => undefined)
774
775
  const key = queryKey()
775
776
 
@@ -787,7 +788,7 @@ describe('query', () => {
787
788
 
788
789
  await sleep(10)
789
790
 
790
- const error = new Error('undefined')
791
+ const error = new Error(`${JSON.stringify(key)} data is undefined`)
791
792
 
792
793
  expect(observerResult).toMatchObject({
793
794
  isError: true,
@@ -804,7 +805,7 @@ describe('query', () => {
804
805
  test('constructor should call initialDataUpdatedAt if defined as a function', async () => {
805
806
  const key = queryKey()
806
807
 
807
- const initialDataUpdatedAtSpy = jest.fn()
808
+ const initialDataUpdatedAtSpy = vi.fn()
808
809
 
809
810
  await queryClient.prefetchQuery({
810
811
  queryKey: key,
@@ -821,7 +822,7 @@ describe('query', () => {
821
822
 
822
823
  queryClient.setQueryDefaults(key, { gcTime: 10 })
823
824
 
824
- const fn = jest.fn()
825
+ const fn = vi.fn()
825
826
 
826
827
  const unsubscribe = queryClient.getQueryCache().subscribe(fn)
827
828
 
@@ -2,6 +2,7 @@ import { sleep, queryKey, createQueryClient } from './utils'
2
2
  import { QueryClient } from '..'
3
3
  import { QueryCache, QueryObserver } from '..'
4
4
  import { waitFor } from '@testing-library/react'
5
+ import { vi } from 'vitest'
5
6
 
6
7
  describe('queryCache', () => {
7
8
  let queryClient: QueryClient
@@ -19,7 +20,7 @@ describe('queryCache', () => {
19
20
  describe('subscribe', () => {
20
21
  test('should pass the correct query', async () => {
21
22
  const key = queryKey()
22
- const subscriber = jest.fn()
23
+ const subscriber = vi.fn()
23
24
  const unsubscribe = queryCache.subscribe(subscriber)
24
25
  queryClient.setQueryData(key, 'foo')
25
26
  const query = queryCache.find({ queryKey: key })
@@ -30,7 +31,7 @@ describe('queryCache', () => {
30
31
 
31
32
  test('should notify listeners when new query is added', async () => {
32
33
  const key = queryKey()
33
- const callback = jest.fn()
34
+ const callback = vi.fn()
34
35
  queryCache.subscribe(callback)
35
36
  queryClient.prefetchQuery({ queryKey: key, queryFn: () => 'data' })
36
37
  await sleep(100)
@@ -50,7 +51,7 @@ describe('queryCache', () => {
50
51
  staleTime: 10,
51
52
  })
52
53
 
53
- const unsubScribeObserver = observer.subscribe(jest.fn)
54
+ const unsubScribeObserver = observer.subscribe(vi.fn())
54
55
 
55
56
  await waitFor(() => {
56
57
  expect(events.length).toBe(8)
@@ -73,7 +74,7 @@ describe('queryCache', () => {
73
74
 
74
75
  test('should include the queryCache and query when notifying listeners', async () => {
75
76
  const key = queryKey()
76
- const callback = jest.fn()
77
+ const callback = vi.fn()
77
78
  queryCache.subscribe(callback)
78
79
  queryClient.prefetchQuery({ queryKey: key, queryFn: () => 'data' })
79
80
  const query = queryCache.find({ queryKey: key })
@@ -83,7 +84,7 @@ describe('queryCache', () => {
83
84
 
84
85
  test('should notify subscribers when new query with initialData is added', async () => {
85
86
  const key = queryKey()
86
- const callback = jest.fn()
87
+ const callback = vi.fn()
87
88
  queryCache.subscribe(callback)
88
89
  queryClient.prefetchQuery({
89
90
  queryKey: key,
@@ -289,9 +290,9 @@ describe('queryCache', () => {
289
290
  describe('QueryCacheConfig error callbacks', () => {
290
291
  test('should call onError and onSettled when a query errors', async () => {
291
292
  const key = queryKey()
292
- const onSuccess = jest.fn()
293
- const onSettled = jest.fn()
294
- const onError = jest.fn()
293
+ const onSuccess = vi.fn()
294
+ const onSettled = vi.fn()
295
+ const onError = vi.fn()
295
296
  const testCache = new QueryCache({ onSuccess, onError, onSettled })
296
297
  const testClient = createQueryClient({ queryCache: testCache })
297
298
  await testClient.prefetchQuery({
@@ -310,9 +311,9 @@ describe('queryCache', () => {
310
311
  describe('QueryCacheConfig success callbacks', () => {
311
312
  test('should call onSuccess and onSettled when a query is successful', async () => {
312
313
  const key = queryKey()
313
- const onSuccess = jest.fn()
314
- const onSettled = jest.fn()
315
- const onError = jest.fn()
314
+ const onSuccess = vi.fn()
315
+ const onSettled = vi.fn()
316
+ const onError = vi.fn()
316
317
  const testCache = new QueryCache({ onSuccess, onError, onSettled })
317
318
  const testClient = createQueryClient({ queryCache: testCache })
318
319
  await testClient.prefetchQuery({
@@ -328,23 +329,6 @@ describe('queryCache', () => {
328
329
  })
329
330
  })
330
331
 
331
- describe('QueryCacheConfig.createStore', () => {
332
- test('should call createStore', async () => {
333
- const createStore = jest.fn().mockImplementation(() => new Map())
334
- new QueryCache({ createStore })
335
- expect(createStore).toHaveBeenCalledWith()
336
- })
337
-
338
- test('should use created store', async () => {
339
- const store = new Map()
340
- const spy = jest.spyOn(store, 'get')
341
-
342
- new QueryCache({ createStore: () => store }).get('key')
343
-
344
- expect(spy).toHaveBeenCalledTimes(1)
345
- })
346
- })
347
-
348
332
  describe('QueryCache.add', () => {
349
333
  test('should not try to add a query already added to the cache', async () => {
350
334
  const key = queryKey()