Skip to content

test(query-core): add test case for hydration #8949

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 16 commits into from
Apr 14, 2025
Merged
Changes from all commits
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
100 changes: 100 additions & 0 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1202,4 +1202,104 @@ describe('dehydration and rehydration', () => {
clientQueryClient.clear()
serverQueryClient.clear()
})

test('should not redact errors when shouldRedactErrors returns false', async () => {
const queryCache = new QueryCache()
const queryClient = createQueryClient({
queryCache,
defaultOptions: {
dehydrate: {
shouldDehydrateQuery: () => true,
shouldRedactErrors: () => false,
},
},
})

const testError = new Error('original error')

const promise = queryClient
.prefetchQuery({
queryKey: ['error'],
queryFn: () => Promise.reject(testError),
retry: false,
})
.catch(() => undefined)

const dehydrated = dehydrate(queryClient)

expect(dehydrated.queries[0]?.promise).toBeInstanceOf(Promise)
await expect(dehydrated.queries[0]?.promise).rejects.toBe(testError)
await promise
})

test('should handle errors in promises for pending queries', async () => {
const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)

const queryCache = new QueryCache()
const queryClient = createQueryClient({
queryCache,
defaultOptions: {
dehydrate: {
shouldDehydrateQuery: () => true,
},
},
})

const promise = queryClient
.prefetchQuery({
queryKey: ['error'],
queryFn: () => Promise.reject(new Error('test error')),
retry: false,
})
.catch(() => undefined)

const dehydrated = dehydrate(queryClient)

expect(dehydrated.queries[0]?.promise).toBeInstanceOf(Promise)

await expect(dehydrated.queries[0]?.promise).rejects.toThrow('redacted')
await promise
consoleMock.mockRestore()
})

test('should log error in development environment when redacting errors', async () => {
const originalNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'development'

const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)

const queryCache = new QueryCache()
const queryClient = createQueryClient({
queryCache,
defaultOptions: {
dehydrate: {
shouldDehydrateQuery: () => true,
shouldRedactErrors: () => true,
},
},
})

const testError = new Error('test error')

const promise = queryClient
.prefetchQuery({
queryKey: ['error'],
queryFn: () => Promise.reject(testError),
retry: false,
})
.catch(() => undefined)

const dehydrated = dehydrate(queryClient)

await expect(dehydrated.queries[0]?.promise).rejects.toThrow('redacted')
expect(consoleMock).toHaveBeenCalledWith(
expect.stringContaining('test error'),
)
await promise

process.env.NODE_ENV = originalNodeEnv
consoleMock.mockRestore()
})
})
Loading