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

utils.ts

35 lines854 Bytes

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