Skip to content

Commit

Permalink
fix(query-core): remove error thrown inside replaceData (#8004)
Browse files Browse the repository at this point in the history
  • Loading branch information
Efimenko committed Sep 2, 2024
1 parent cd91357 commit fb9d4f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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

0 comments on commit fb9d4f6

Please sign in to comment.