Skip to content
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

fix(query-core): Remove error thrown inside replaceData #8004

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 27 additions & 5 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ describe('query', () => {
expect(spy).toHaveBeenCalledWith('1 - 2')
})

it('should have an error status when queryFn data is not serializable', async () => {
it('should have an error log when queryFn data is not serializable', async () => {
const consoleMock = vi.spyOn(console, 'error')

consoleMock.mockImplementation(() => undefined)
Expand Down Expand Up @@ -1000,8 +1000,6 @@ describe('query', () => {

await queryClient.prefetchQuery({ queryKey: key, queryFn })

const query = queryCache.find({ queryKey: key })!

expect(queryFn).toHaveBeenCalledTimes(1)

expect(consoleMock).toHaveBeenCalledWith(
Expand All @@ -1010,8 +1008,32 @@ describe('query', () => {
),
)

expect(query.state.status).toBe('error')

consoleMock.mockRestore()
})

it('should have an error status when setData has any error inside', async () => {
const key = queryKey()

const queryFn = vi.fn()

queryFn.mockImplementation(async () => {
await sleep(10)

return 'data'
})

await queryClient.prefetchQuery({
queryKey: key,
queryFn,
structuralSharing: () => {
throw Error('Any error')
},
})

const query = queryCache.find({ queryKey: key })!

expect(queryFn).toHaveBeenCalledTimes(1)

expect(query.state.status).toBe('error')
})
})
2 changes: 0 additions & 2 deletions packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ export function replaceData<
console.error(
`StructuralSharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`,
)

throw new Error('Data is not serializable')
}
}

Expand Down
Loading