From a7780444bb0bdd0e7a768c6ebc57e04aea5f7c51 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Sat, 26 Jun 2021 21:42:28 +0200 Subject: [PATCH] fix(useQuery): don't inform observers about CancelledErrors (#2409) --- src/core/queryObserver.ts | 3 ++- src/react/tests/useQuery.test.tsx | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/queryObserver.ts b/src/core/queryObserver.ts index cc1cd3e85e..e1295cbd37 100644 --- a/src/core/queryObserver.ts +++ b/src/core/queryObserver.ts @@ -22,6 +22,7 @@ import type { QueryClient } from './queryClient' import { focusManager } from './focusManager' import { Subscribable } from './subscribable' import { getLogger } from './logger' +import { isCancelledError } from './retryer' type QueryObserverListener = ( result: QueryObserverResult @@ -660,7 +661,7 @@ export class QueryObserver< if (action.type === 'success') { notifyOptions.onSuccess = true - } else if (action.type === 'error') { + } else if (action.type === 'error' && !isCancelledError(action.error)) { notifyOptions.onError = true } diff --git a/src/react/tests/useQuery.test.tsx b/src/react/tests/useQuery.test.tsx index 7e94f02098..ae35708ca6 100644 --- a/src/react/tests/useQuery.test.tsx +++ b/src/react/tests/useQuery.test.tsx @@ -473,6 +473,33 @@ describe('useQuery', () => { consoleMock.mockRestore() }) + it('should not call onError when receiving a CancelledError', async () => { + const key = queryKey() + const onError = jest.fn() + const consoleMock = mockConsoleError() + + function Page() { + useQuery( + key, + async () => { + await sleep(10) + return 23 + }, + { + onError, + } + ) + return null + } + + renderWithClient(queryClient, ) + + await sleep(5) + await queryClient.cancelQueries(key) + expect(onError).not.toHaveBeenCalled() + consoleMock.mockRestore() + }) + it('should call onSettled after a query has been fetched', async () => { const key = queryKey() const states: UseQueryResult[] = []