From a3fab11b42b368eebdc9d44badc06d4a3477c035 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:12:14 +0200 Subject: [PATCH 01/13] chore(eslint): turn on no-unnecessary-condition rule --- .eslintrc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.eslintrc b/.eslintrc index fcf0d28e86..a39b84e27b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -12,6 +12,7 @@ "es6": true }, "parserOptions": { + "project": "./tsconfig.json", "sourceType": "module" }, "rules": { @@ -21,6 +22,7 @@ "@typescript-eslint/no-empty-interface": "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/no-unnecessary-condition": "error", "@typescript-eslint/no-inferrable-types": [ "error", { From 127c285d0a2a514080e6bad037b8b10a4515e6b1 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:40:51 +0200 Subject: [PATCH 02/13] chore(eslint): turn on no-unnecessary-condition rule remove unnecessary optional chainings in tests --- src/core/tests/hydration.test.tsx | 4 ++-- src/react/tests/ssr.test.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/tests/hydration.test.tsx b/src/core/tests/hydration.test.tsx index 4bc3bc2087..d7249b15e2 100644 --- a/src/core/tests/hydration.test.tsx +++ b/src/core/tests/hydration.test.tsx @@ -178,8 +178,8 @@ describe('dehydration and rehydration', () => { // This is testing implementation details that can change and are not // part of the public API, but is important for keeping the payload small - const dehydratedQuery = dehydrated?.queries.find( - query => query?.queryKey === 'string' + const dehydratedQuery = dehydrated.queries.find( + query => query.queryKey === 'string' ) expect(dehydratedQuery).toBeUndefined() diff --git a/src/react/tests/ssr.test.tsx b/src/react/tests/ssr.test.tsx index 2363a160c0..43bb71c780 100644 --- a/src/react/tests/ssr.test.tsx +++ b/src/react/tests/ssr.test.tsx @@ -132,7 +132,7 @@ describe('Server Side Rendering', () => { const query = useInfiniteQuery(key, queryFn) return ( From 872d6ddf976e137cfc73553e0dcbd6aae513e07a Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:53:40 +0200 Subject: [PATCH 03/13] chore(eslint): keep the matchMedia check even though it can never be undefined, except in tests, and I couldn't figure out how to mock that properly --- src/devtools/useMediaQuery.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/devtools/useMediaQuery.ts b/src/devtools/useMediaQuery.ts index d089d04620..8a1ec33113 100644 --- a/src/devtools/useMediaQuery.ts +++ b/src/devtools/useMediaQuery.ts @@ -4,6 +4,7 @@ export default function useMediaQuery(query: string): boolean | undefined { // Keep track of the preference in state, start with the current match const [isMatch, setIsMatch] = React.useState(() => { if (typeof window !== 'undefined') { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition return window.matchMedia && window.matchMedia(query).matches } }) @@ -11,6 +12,7 @@ export default function useMediaQuery(query: string): boolean | undefined { // Watch for changes React.useEffect(() => { if (typeof window !== 'undefined') { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!window.matchMedia) { return } From 5ea63950eb3debe448ac60124e3ed93e81beda98 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:55:27 +0200 Subject: [PATCH 04/13] chore(eslint): remove unnecessary checks in devtools --- src/devtools/devtools.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/devtools/devtools.tsx b/src/devtools/devtools.tsx index af098e5978..bbb5c47e70 100644 --- a/src/devtools/devtools.tsx +++ b/src/devtools/devtools.tsx @@ -122,7 +122,7 @@ export function ReactQueryDevtools({ const run = (moveEvent: MouseEvent) => { const delta = dragInfo.pageY - moveEvent.pageY - const newHeight = dragInfo?.originalHeight + delta + const newHeight = dragInfo.originalHeight + delta setDevtoolsHeight(newHeight) @@ -153,13 +153,13 @@ export function ReactQueryDevtools({ const ref = panelRef.current if (ref) { const handlePanelTransitionStart = () => { - if (ref && isResolvedOpen) { + if (isResolvedOpen) { ref.style.visibility = 'visible' } } const handlePanelTransitionEnd = () => { - if (ref && !isResolvedOpen) { + if (!isResolvedOpen) { ref.style.visibility = 'hidden' } } @@ -837,7 +837,7 @@ export const ReactQueryDevtoolsPanel = React.forwardRef< > From bb89a9e06d72bd543bf090a461259498130a3885 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:56:04 +0200 Subject: [PATCH 05/13] chore(eslint): addEventListener should exist on window if window is defined, which is checked by isServer --- src/core/focusManager.ts | 2 +- src/core/onlineManager.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/focusManager.ts b/src/core/focusManager.ts index 18bfdcbc46..1894bee9df 100644 --- a/src/core/focusManager.ts +++ b/src/core/focusManager.ts @@ -56,7 +56,7 @@ class FocusManager extends Subscribable { } private setDefaultEventListener() { - if (!isServer && window?.addEventListener) { + if (!isServer) { this.setEventListener(onFocus => { const listener = () => onFocus() // Listen to visibillitychange and focus diff --git a/src/core/onlineManager.ts b/src/core/onlineManager.ts index f02a03c9bf..2fe5dd56c3 100644 --- a/src/core/onlineManager.ts +++ b/src/core/onlineManager.ts @@ -56,7 +56,7 @@ class OnlineManager extends Subscribable { } private setDefaultEventListener() { - if (!isServer && window?.addEventListener) { + if (!isServer) { this.setEventListener(onOnline => { const listener = () => onOnline() // Listen to online From 7ef1f606c01a76164d5f373ac9b5e38c8dfab86d Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:56:37 +0200 Subject: [PATCH 06/13] chore(eslint): assign default object to options instead of re-assigning it. In any case, the optional chaining is unnecessary --- src/core/hydration.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/hydration.ts b/src/core/hydration.ts index d10e1c83f0..c45dd86ab2 100644 --- a/src/core/hydration.ts +++ b/src/core/hydration.ts @@ -75,14 +75,12 @@ function defaultShouldDehydrateQuery(query: Query) { export function dehydrate( client: QueryClient, - options?: DehydrateOptions + options: DehydrateOptions = {} ): DehydratedState { - options = options || {} - const mutations: DehydratedMutation[] = [] const queries: DehydratedQuery[] = [] - if (options?.dehydrateMutations !== false) { + if (options.dehydrateMutations !== false) { const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation @@ -96,7 +94,7 @@ export function dehydrate( }) } - if (options?.dehydrateQueries !== false) { + if (options.dehydrateQueries !== false) { const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery @@ -125,7 +123,9 @@ export function hydrate( const mutationCache = client.getMutationCache() const queryCache = client.getQueryCache() + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const mutations = (dehydratedState as DehydratedState).mutations || [] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const queries = (dehydratedState as DehydratedState).queries || [] mutations.forEach(dehydratedMutation => { From 2b55ec31827b4bd4688c6ac2271e5604ad95e646 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:56:56 +0200 Subject: [PATCH 07/13] chore(eslint): action.type should always be defined --- src/broadcastQueryClient-experimental/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/broadcastQueryClient-experimental/index.ts b/src/broadcastQueryClient-experimental/index.ts index 9f6181553a..4c195ef757 100644 --- a/src/broadcastQueryClient-experimental/index.ts +++ b/src/broadcastQueryClient-experimental/index.ts @@ -34,7 +34,7 @@ export function broadcastQueryClient({ if ( queryEvent.type === 'queryUpdated' && - queryEvent.action?.type === 'success' + queryEvent.action.type === 'success' ) { channel.postMessage({ type: 'queryUpdated', From 02a13430c4644e8e43f3e6777cbd0e403daeb7b8 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:57:09 +0200 Subject: [PATCH 08/13] chore(eslint): keep the fallback for console --- src/core/logger.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/logger.ts b/src/core/logger.ts index 4d459e70ea..cfbfd00904 100644 --- a/src/core/logger.ts +++ b/src/core/logger.ts @@ -12,6 +12,7 @@ type LogFunction = (...args: any[]) => void // FUNCTIONS +// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition let logger: Logger = console || { error: noop, warn: noop, From 03c432c17b293721ab0e60be827863a01ea9ec25 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:57:33 +0200 Subject: [PATCH 09/13] chore(eslint): one rule always complains so up-casting false to boolean --- src/core/queriesObserver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/queriesObserver.ts b/src/core/queriesObserver.ts index 99ddf7e773..88ad0f3fc1 100644 --- a/src/core/queriesObserver.ts +++ b/src/core/queriesObserver.ts @@ -86,7 +86,7 @@ export class QueriesObserver extends Subscribable { private updateObservers(notifyOptions?: NotifyOptions): void { notifyManager.batch(() => { - let hasIndexChange = false + let hasIndexChange = false as boolean const prevObservers = this.observers const prevObserversMap = this.observersMap From b81640e599bb294b9629d0f7857577067192d228 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:58:01 +0200 Subject: [PATCH 10/13] chore(eslint): if we have a behaviour, we also have na onFetch --- src/core/query.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/query.ts b/src/core/query.ts index 4fde0c12c5..7a3f44921f 100644 --- a/src/core/query.ts +++ b/src/core/query.ts @@ -414,9 +414,7 @@ export class Query< meta: this.meta, } - if (this.options.behavior?.onFetch) { - this.options.behavior?.onFetch(context) - } + this.options.behavior?.onFetch(context) // Store state in case the current fetch needs to be reverted this.revertState = this.state From 8751b8b635b1616c88260fc016177ea3a790c274 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:58:38 +0200 Subject: [PATCH 11/13] chore(eslint): parseFilters always returns an object as it falls back to an empty object internally, so the falsy check didn't do anything --- src/core/queryCache.ts | 4 +--- src/core/queryClient.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/queryCache.ts b/src/core/queryCache.ts index 1a72d194be..d20badb12f 100644 --- a/src/core/queryCache.ts +++ b/src/core/queryCache.ts @@ -174,9 +174,7 @@ export class QueryCache extends Subscribable { findAll(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): Query[] findAll(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): Query[] { const [filters] = parseFilterArgs(arg1, arg2) - return filters - ? this.queries.filter(query => matchQuery(filters, query)) - : this.queries + return this.queries.filter(query => matchQuery(filters, query)) } notify(event: QueryCacheNotifyEvent) { diff --git a/src/core/queryClient.ts b/src/core/queryClient.ts index 79723b4176..055993eba3 100644 --- a/src/core/queryClient.ts +++ b/src/core/queryClient.ts @@ -290,7 +290,7 @@ export class QueryClient { this.queryCache.findAll(filters).map(query => query.fetch(undefined, { ...options, - meta: { refetchPage: filters?.refetchPage }, + meta: { refetchPage: filters.refetchPage }, }) ) ) From 44c3a430a9590d8cb431324f0de48952f1aa41c0 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 26 Oct 2021 22:59:31 +0200 Subject: [PATCH 12/13] chore(eslint): upcast previous result to be potentially undefined to make the optinal chains necessary --- src/core/queryObserver.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/queryObserver.ts b/src/core/queryObserver.ts index 254a194469..243282da60 100644 --- a/src/core/queryObserver.ts +++ b/src/core/queryObserver.ts @@ -430,7 +430,9 @@ export class QueryObserver< ): QueryObserverResult { const prevQuery = this.currentQuery const prevOptions = this.options - const prevResult = this.currentResult + const prevResult = this.currentResult as + | QueryObserverResult + | undefined const prevResultState = this.currentResultState const prevResultOptions = this.currentResultOptions const queryChange = query !== prevQuery @@ -662,7 +664,9 @@ export class QueryObserver< return } - const prevQuery = this.currentQuery + const prevQuery = this.currentQuery as + | Query + | undefined this.currentQuery = query this.currentQueryInitialState = query.state this.previousQueryResult = this.currentResult From 3facf9b00dc18cc4b9ddf03d0a39b09edbfa1d6e Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Sat, 26 Feb 2022 18:17:28 +0100 Subject: [PATCH 13/13] fix issues after updating to alpha --- src/broadcastQueryClient-experimental/index.ts | 7 ++----- src/core/focusManager.ts | 2 +- src/core/onlineManager.ts | 2 +- src/core/query.ts | 2 +- src/core/queryClient.ts | 6 +++--- src/core/tests/focusManager.test.tsx | 11 +++++------ src/core/tests/mutationCache.test.tsx | 8 ++++---- src/core/tests/mutations.test.tsx | 2 +- src/core/tests/onlineManager.test.tsx | 10 ++++------ src/core/tests/queryObserver.test.tsx | 4 ++-- src/reactjs/tests/ssr-hydration.test.tsx | 10 +--------- src/reactjs/tests/suspense.test.tsx | 8 +------- src/reactjs/tests/utils.tsx | 14 ++++++++++++++ 13 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/broadcastQueryClient-experimental/index.ts b/src/broadcastQueryClient-experimental/index.ts index 32868118b0..89823ba9d7 100644 --- a/src/broadcastQueryClient-experimental/index.ts +++ b/src/broadcastQueryClient-experimental/index.ts @@ -24,7 +24,7 @@ export function broadcastQueryClient({ const queryCache = queryClient.getQueryCache() queryClient.getQueryCache().subscribe(queryEvent => { - if (transaction || !queryEvent?.query) { + if (transaction) { return } @@ -32,10 +32,7 @@ export function broadcastQueryClient({ query: { queryHash, queryKey, state }, } = queryEvent - if ( - queryEvent.type === 'updated' && - queryEvent.action.type === 'success' - ) { + if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') { channel.postMessage({ type: 'updated', queryHash, diff --git a/src/core/focusManager.ts b/src/core/focusManager.ts index db08d0fb9a..2ffb62eaaf 100644 --- a/src/core/focusManager.ts +++ b/src/core/focusManager.ts @@ -14,7 +14,7 @@ export class FocusManager extends Subscribable { constructor() { super() this.setup = onFocus => { - if (!isServer && window?.addEventListener) { + if (!isServer) { const listener = () => onFocus() // Listen to visibillitychange and focus window.addEventListener('visibilitychange', listener, false) diff --git a/src/core/onlineManager.ts b/src/core/onlineManager.ts index 6efcf75aa6..7c3d832b63 100644 --- a/src/core/onlineManager.ts +++ b/src/core/onlineManager.ts @@ -14,7 +14,7 @@ export class OnlineManager extends Subscribable { constructor() { super() this.setup = onOnline => { - if (!isServer && window?.addEventListener) { + if (!isServer) { const listener = () => onOnline() // Listen to online window.addEventListener('online', listener, false) diff --git a/src/core/query.ts b/src/core/query.ts index 37500110f5..ac3738b97e 100644 --- a/src/core/query.ts +++ b/src/core/query.ts @@ -461,7 +461,7 @@ export class Query< // Try to fetch the data this.retryer = createRetryer({ fn: context.fetchFn as () => TData, - abort: abortController?.abort?.bind(abortController), + abort: abortController?.abort.bind(abortController), onSuccess: data => { if (typeof data === 'undefined') { onError(new Error('Query data cannot be undefined') as any) diff --git a/src/core/queryClient.ts b/src/core/queryClient.ts index cd96553ef6..2a730473d9 100644 --- a/src/core/queryClient.ts +++ b/src/core/queryClient.ts @@ -267,12 +267,12 @@ export class QueryClient { query.invalidate() }) - if (filters?.refetchType === 'none') { + if (filters.refetchType === 'none') { return Promise.resolve() } const refetchFilters: RefetchQueryFilters = { ...filters, - type: filters?.refetchType ?? filters?.type ?? 'active', + type: filters.refetchType ?? filters.type ?? 'active', } return this.refetchQueries(refetchFilters, options) }) @@ -302,7 +302,7 @@ export class QueryClient { query.fetch(undefined, { ...options, cancelRefetch: options?.cancelRefetch ?? true, - meta: { refetchPage: filters?.refetchPage }, + meta: { refetchPage: filters.refetchPage }, }) ) ) diff --git a/src/core/tests/focusManager.test.tsx b/src/core/tests/focusManager.test.tsx index f747835638..b3086179ce 100644 --- a/src/core/tests/focusManager.test.tsx +++ b/src/core/tests/focusManager.test.tsx @@ -1,9 +1,11 @@ import { sleep } from '../utils' import { FocusManager } from '../focusManager' +import { setIsServer } from '../../reactjs/tests/utils' describe('focusManager', () => { let focusManager: FocusManager beforeEach(() => { + jest.resetModules() focusManager = new FocusManager() }) @@ -61,17 +63,14 @@ describe('focusManager', () => { globalThis.document = document }) - test('cleanup should still be undefined if window.addEventListener is not defined', async () => { - const { addEventListener } = globalThis.window - - // @ts-expect-error - globalThis.window.addEventListener = undefined + test('cleanup should still be undefined if window is not defined', async () => { + const restoreIsServer = setIsServer(true) const unsubscribe = focusManager.subscribe(() => undefined) expect(focusManager['cleanup']).toBeUndefined() unsubscribe() - globalThis.window.addEventListener = addEventListener + restoreIsServer() }) it('should replace default window listener when a new event listener is set', async () => { diff --git a/src/core/tests/mutationCache.test.tsx b/src/core/tests/mutationCache.test.tsx index 8c1b8098ac..a094786e29 100644 --- a/src/core/tests/mutationCache.test.tsx +++ b/src/core/tests/mutationCache.test.tsx @@ -193,14 +193,14 @@ describe('mutationCache', () => { // we currently have no way to add multiple observers to the same mutation const currentMutation = observer1['currentMutation']! - currentMutation?.addObserver(observer1) - currentMutation?.addObserver(observer2) + currentMutation.addObserver(observer1) + currentMutation.addObserver(observer2) expect(currentMutation['observers'].length).toEqual(2) expect(queryClient.getMutationCache().getAll()).toHaveLength(1) - currentMutation?.removeObserver(observer1) - currentMutation?.removeObserver(observer2) + currentMutation.removeObserver(observer1) + currentMutation.removeObserver(observer2) expect(currentMutation['observers'].length).toEqual(0) expect(queryClient.getMutationCache().getAll()).toHaveLength(1) // wait for cacheTime to gc diff --git a/src/core/tests/mutations.test.tsx b/src/core/tests/mutations.test.tsx index acf9fb79b5..ab6494e6d5 100644 --- a/src/core/tests/mutations.test.tsx +++ b/src/core/tests/mutations.test.tsx @@ -341,7 +341,7 @@ describe('mutations', () => { // because no use case has been found const currentMutation = mutation['currentMutation']! expect(currentMutation['observers'].length).toEqual(1) - currentMutation?.addObserver(mutation) + currentMutation.addObserver(mutation) expect(currentMutation['observers'].length).toEqual(1) }) diff --git a/src/core/tests/onlineManager.test.tsx b/src/core/tests/onlineManager.test.tsx index e9d15cf1fd..78def64c60 100644 --- a/src/core/tests/onlineManager.test.tsx +++ b/src/core/tests/onlineManager.test.tsx @@ -1,5 +1,6 @@ import { OnlineManager } from '../onlineManager' import { sleep } from '../utils' +import { setIsServer } from '../../reactjs/tests/utils' describe('onlineManager', () => { let onlineManager: OnlineManager @@ -56,17 +57,14 @@ describe('onlineManager', () => { expect(remove2Spy).not.toHaveBeenCalled() }) - test('cleanup should still be undefined if window.addEventListener is not defined', async () => { - const { addEventListener } = globalThis.window - - // @ts-expect-error - globalThis.window.addEventListener = undefined + test('cleanup should still be undefined if window is not defined', async () => { + const restoreIsServer = setIsServer(true) const unsubscribe = onlineManager.subscribe(() => undefined) expect(onlineManager['cleanup']).toBeUndefined() unsubscribe() - globalThis.window.addEventListener = addEventListener + restoreIsServer() }) test('it should replace default window listener when a new event listener is set', async () => { diff --git a/src/core/tests/queryObserver.test.tsx b/src/core/tests/queryObserver.test.tsx index 2bee1ca21e..82af00c784 100644 --- a/src/core/tests/queryObserver.test.tsx +++ b/src/core/tests/queryObserver.test.tsx @@ -619,7 +619,7 @@ describe('queryObserver', () => { queryKey: key, queryFn: () => data, select: () => { - if (true) return selectedData1 + return selectedData1 }, placeholderData: placeholderData1, }) @@ -655,7 +655,7 @@ describe('queryObserver', () => { queryKey: key, queryFn: () => data, select: () => { - if (true) return selectedData + return selectedData }, placeholderData: placeholderData1, }) diff --git a/src/reactjs/tests/ssr-hydration.test.tsx b/src/reactjs/tests/ssr-hydration.test.tsx index 3ea1111a22..0dbfacf248 100644 --- a/src/reactjs/tests/ssr-hydration.test.tsx +++ b/src/reactjs/tests/ssr-hydration.test.tsx @@ -9,15 +9,7 @@ import { dehydrate, hydrate, } from '../..' -import * as utils from '../../core/utils' -import { createQueryClient, mockLogger, sleep } from './utils' - -// This monkey-patches the isServer-value from utils, -// so that we can pretend to be in a server environment -function setIsServer(isServer: boolean) { - // @ts-ignore - utils.isServer = isServer -} +import { createQueryClient, mockLogger, setIsServer, sleep } from './utils' async function fetchData(value: TData, ms?: number): Promise { await sleep(ms || 1) diff --git a/src/reactjs/tests/suspense.test.tsx b/src/reactjs/tests/suspense.test.tsx index c913bef0a9..d6d4263317 100644 --- a/src/reactjs/tests/suspense.test.tsx +++ b/src/reactjs/tests/suspense.test.tsx @@ -877,8 +877,6 @@ describe("useQuery's in Suspense mode", () => { }) it('should error catched in error boundary without infinite loop when enabled changed', async () => { - const succeed = false - function Page() { const queryKeys = '1' const [enabled, setEnabled] = React.useState(false) @@ -887,11 +885,7 @@ describe("useQuery's in Suspense mode", () => { [queryKeys], async () => { await sleep(10) - if (!succeed) { - throw new Error('Suspense Error Bingo') - } else { - return 'data' - } + throw new Error('Suspense Error Bingo') }, { retry: false, diff --git a/src/reactjs/tests/utils.tsx b/src/reactjs/tests/utils.tsx index 73851a98b7..e6e0bd77fd 100644 --- a/src/reactjs/tests/utils.tsx +++ b/src/reactjs/tests/utils.tsx @@ -7,6 +7,7 @@ import { QueryClientConfig, QueryClientProvider, } from '../..' +import * as utils from '../../core/utils' export function createQueryClient(config?: QueryClientConfig): QueryClient { jest.spyOn(console, 'error').mockImplementation(() => undefined) @@ -94,3 +95,16 @@ export const executeMutation = ( ): Promise => { return queryClient.getMutationCache().build(queryClient, options).execute() } + +// This monkey-patches the isServer-value from utils, +// so that we can pretend to be in a server environment +export function setIsServer(isServer: boolean) { + const original = utils.isServer + // @ts-ignore + utils.isServer = isServer + + return () => { + // @ts-ignore + utils.isServer = original + } +}