Skip to content

Commit 7dafa93

Browse files
sukvvonmanudeli
andauthored
test(query-core/query): add 'advanceTimersByTimeAsync' and remove 'vi.waitFor' (#9319)
Co-authored-by: Jonghyeon Ko <manudeli.ko@gmail.com>
1 parent 002550c commit 7dafa93

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,14 @@ describe('query', () => {
501501
})
502502
const unsubscribe1 = observer.subscribe(() => undefined)
503503
unsubscribe1()
504-
await vi.waitFor(() =>
505-
expect(queryCache.find({ queryKey: key })).toBeUndefined(),
506-
)
504+
505+
await vi.advanceTimersByTimeAsync(0)
506+
expect(queryCache.find({ queryKey: key })).toBeUndefined()
507507
const unsubscribe2 = observer.subscribe(() => undefined)
508508
unsubscribe2()
509509

510-
await vi.waitFor(() =>
511-
expect(queryCache.find({ queryKey: key })).toBeUndefined(),
512-
)
510+
await vi.advanceTimersByTimeAsync(0)
511+
expect(queryCache.find({ queryKey: key })).toBeUndefined()
513512
expect(count).toBe(1)
514513
})
515514

@@ -524,9 +523,9 @@ describe('query', () => {
524523
const unsubscribe = observer.subscribe(() => undefined)
525524
expect(queryCache.find({ queryKey: key })).toBeDefined()
526525
unsubscribe()
527-
await vi.waitFor(() =>
528-
expect(queryCache.find({ queryKey: key })).toBeUndefined(),
529-
)
526+
527+
await vi.advanceTimersByTimeAsync(0)
528+
expect(queryCache.find({ queryKey: key })).toBeUndefined()
530529
})
531530

532531
test('should be garbage collected later when unsubscribed and query is fetching', async () => {
@@ -544,9 +543,8 @@ describe('query', () => {
544543
// unsubscribe should not remove even though gcTime has elapsed b/c query is still fetching
545544
expect(queryCache.find({ queryKey: key })).toBeDefined()
546545
// should be removed after an additional staleTime wait
547-
await vi.waitFor(() =>
548-
expect(queryCache.find({ queryKey: key })).toBeUndefined(),
549-
)
546+
await vi.advanceTimersByTimeAsync(30)
547+
expect(queryCache.find({ queryKey: key })).toBeUndefined()
550548
})
551549

552550
test('should not be garbage collected unless there are no subscribers', async () => {
@@ -887,12 +885,11 @@ describe('query', () => {
887885

888886
queryClient.setQueryData(key, 'data')
889887

890-
await vi.waitFor(() =>
891-
expect(fn).toHaveBeenCalledWith(
892-
expect.objectContaining({
893-
type: 'removed',
894-
}),
895-
),
888+
await vi.advanceTimersByTimeAsync(10)
889+
expect(fn).toHaveBeenCalledWith(
890+
expect.objectContaining({
891+
type: 'removed',
892+
}),
896893
)
897894

898895
expect(queryClient.getQueryCache().findAll()).toHaveLength(0)

0 commit comments

Comments
 (0)