Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.ts

34 lines811 Bytes

1import { act } from '@testing-library/react'
2​
3import { QueryClient, QueryClientConfig } from '@tanstack/query-core'
4​
5export function createQueryClient(config?: QueryClientConfig): QueryClient {
6 jest.spyOn(console, 'error').mockImplementation(() => undefined)
7 return new QueryClient({ logger: mockLogger, ...config })
8}
9​
10export const mockLogger = {
11 log: jest.fn(),
12 warn: jest.fn(),
13 error: jest.fn(),
15​
16let queryKeyCount = 0
17export function queryKey(): Array<string> {
18 queryKeyCount++
19 return [`query_${queryKeyCount}`]
21​
22export function sleep(timeout: number): Promise<void> {
23 return new Promise((resolve, _reject) => {
24 setTimeout(resolve, timeout)
25 })
27​
28export function setActTimeout(fn: () => void, ms?: number) {
29 return setTimeout(() => {
30 act(() => {
31 fn()
32 })
33 }, ms)