Skip to content

Commit fb9d4f6

Browse files
authored
fix(query-core): remove error thrown inside replaceData (#8004)
1 parent cd91357 commit fb9d4f6

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ describe('query', () => {
966966
expect(spy).toHaveBeenCalledWith('1 - 2')
967967
})
968968

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

972972
consoleMock.mockImplementation(() => undefined)
@@ -1000,8 +1000,6 @@ describe('query', () => {
10001000

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

1003-
const query = queryCache.find({ queryKey: key })!
1004-
10051003
expect(queryFn).toHaveBeenCalledTimes(1)
10061004

10071005
expect(consoleMock).toHaveBeenCalledWith(
@@ -1010,8 +1008,32 @@ describe('query', () => {
10101008
),
10111009
)
10121010

1013-
expect(query.state.status).toBe('error')
1014-
10151011
consoleMock.mockRestore()
10161012
})
1013+
1014+
it('should have an error status when setData has any error inside', async () => {
1015+
const key = queryKey()
1016+
1017+
const queryFn = vi.fn()
1018+
1019+
queryFn.mockImplementation(async () => {
1020+
await sleep(10)
1021+
1022+
return 'data'
1023+
})
1024+
1025+
await queryClient.prefetchQuery({
1026+
queryKey: key,
1027+
queryFn,
1028+
structuralSharing: () => {
1029+
throw Error('Any error')
1030+
},
1031+
})
1032+
1033+
const query = queryCache.find({ queryKey: key })!
1034+
1035+
expect(queryFn).toHaveBeenCalledTimes(1)
1036+
1037+
expect(query.state.status).toBe('error')
1038+
})
10171039
})

packages/query-core/src/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ export function replaceData<
362362
console.error(
363363
`StructuralSharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`,
364364
)
365-
366-
throw new Error('Data is not serializable')
367365
}
368366
}
369367

0 commit comments

Comments
 (0)