Skip to content

test(react-query): use fakeTimers for ssr.test.tsx #8981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test(react-query): use fakeTimers in ssr.test.tsx
  • Loading branch information
minseong0324 committed Apr 9, 2025
commit e5abe41293e31f3408c46307e20f797b5764edb9
19 changes: 13 additions & 6 deletions packages/react-query/src/__tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as React from 'react'
import { renderToString } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryCache, QueryClientProvider, useInfiniteQuery, useQuery } from '..'
import { createQueryClient, queryKey, setIsServer, sleep } from './utils'
import { createQueryClient, queryKey, setIsServer } from './utils'

describe('Server Side Rendering', () => {
setIsServer(true)
beforeEach(() => {
vi.useFakeTimers()
setIsServer(true)
})

afterEach(() => {
vi.useRealTimers()
})

it('should not trigger fetch', () => {
const queryCache = new QueryCache()
Expand Down Expand Up @@ -54,8 +61,8 @@ describe('Server Side Rendering', () => {
const queryCache = new QueryCache()
const queryClient = createQueryClient({ queryCache })
const key = queryKey()
const queryFn = vi.fn(() => {
sleep(10)
const queryFn = vi.fn(async () => {
await vi.advanceTimersByTimeAsync(10)
return 'data'
})

Expand Down Expand Up @@ -123,7 +130,7 @@ describe('Server Side Rendering', () => {
const queryClient = createQueryClient({ queryCache })
const key = queryKey()
const queryFn = vi.fn(async () => {
await sleep(5)
await vi.advanceTimersByTimeAsync(5)
return 'page 1'
})

Expand Down
Loading