From 221c01bf65a8b449998bcace89a5fedccf5a7f9a Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Sat, 4 Jul 2026 15:13:57 +0900 Subject: [PATCH] test(solid-query-devtools): extract shared test setup into 'beforeEach' --- .../src/__tests__/devtools.test.tsx | 36 ++++--------------- .../src/__tests__/devtoolsPanel.test.tsx | 30 ++++------------ 2 files changed, 14 insertions(+), 52 deletions(-) diff --git a/packages/solid-query-devtools/src/__tests__/devtools.test.tsx b/packages/solid-query-devtools/src/__tests__/devtools.test.tsx index f171551b7c8..26da91a25ae 100644 --- a/packages/solid-query-devtools/src/__tests__/devtools.test.tsx +++ b/packages/solid-query-devtools/src/__tests__/devtools.test.tsx @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createSignal } from 'solid-js' import { render } from '@solidjs/testing-library' import { QueryClient, QueryClientProvider } from '@tanstack/solid-query' @@ -12,6 +12,12 @@ import type { } from '@tanstack/query-devtools' describe('SolidQueryDevtools', () => { + let queryClient: QueryClient + + beforeEach(() => { + queryClient = new QueryClient() + }) + afterEach(() => { vi.restoreAllMocks() }) @@ -23,8 +29,6 @@ describe('SolidQueryDevtools', () => { }) it('should not throw an error if query client is provided via context', () => { - const queryClient = new QueryClient() - expect(() => render(() => ( @@ -35,8 +39,6 @@ describe('SolidQueryDevtools', () => { }) it('should not throw an error if query client is provided via props', () => { - const queryClient = new QueryClient() - expect(() => render(() => ), ).not.toThrow() @@ -47,8 +49,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setButtonPosition', ) - const queryClient = new QueryClient() - render(() => ( )) @@ -58,8 +58,6 @@ describe('SolidQueryDevtools', () => { it('should forward "position" to the devtools instance', () => { const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition') - const queryClient = new QueryClient() - render(() => ) expect(setPosition).toHaveBeenCalledWith('left') @@ -70,8 +68,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setInitialIsOpen', ) - const queryClient = new QueryClient() - render(() => ( )) @@ -84,8 +80,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setInitialIsOpen', ) - const queryClient = new QueryClient() - render(() => ) expect(setInitialIsOpen).toHaveBeenCalledWith(false) @@ -96,7 +90,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setErrorTypes', ) - const queryClient = new QueryClient() const errorTypes = [ { name: 'Network', initializer: () => new Error('Network') }, ] @@ -113,8 +106,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setErrorTypes', ) - const queryClient = new QueryClient() - render(() => ) expect(setErrorTypes).toHaveBeenCalledWith([]) @@ -122,8 +113,6 @@ describe('SolidQueryDevtools', () => { it('should forward "theme" to the devtools instance', () => { const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme') - const queryClient = new QueryClient() - render(() => ) expect(setTheme).toHaveBeenCalledWith('dark') @@ -131,8 +120,6 @@ describe('SolidQueryDevtools', () => { it('should default "theme" to "system" when the prop is omitted', () => { const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme') - const queryClient = new QueryClient() - render(() => ) expect(setTheme).toHaveBeenCalledWith('system') @@ -140,8 +127,6 @@ describe('SolidQueryDevtools', () => { it('should forward the resolved "QueryClient" via "setClient"', () => { const setClient = vi.spyOn(TanstackQueryDevtools.prototype, 'setClient') - const queryClient = new QueryClient() - render(() => ) expect(setClient).toHaveBeenCalledWith(queryClient) @@ -152,7 +137,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setButtonPosition', ) - const queryClient = new QueryClient() const [buttonPosition, setButtonPositionSignal] = createSignal('bottom-right') @@ -171,7 +155,6 @@ describe('SolidQueryDevtools', () => { it('should forward a "position" change to the devtools instance after mount', () => { const setPosition = vi.spyOn(TanstackQueryDevtools.prototype, 'setPosition') - const queryClient = new QueryClient() const [position, setPositionSignal] = createSignal('bottom') @@ -190,7 +173,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setInitialIsOpen', ) - const queryClient = new QueryClient() const [initialIsOpen, setInitialIsOpenSignal] = createSignal(false) render(() => ( @@ -211,7 +193,6 @@ describe('SolidQueryDevtools', () => { TanstackQueryDevtools.prototype, 'setErrorTypes', ) - const queryClient = new QueryClient() const [errorTypes, setErrorTypesSignal] = createSignal< Array >([]) @@ -231,7 +212,6 @@ describe('SolidQueryDevtools', () => { it('should forward a "theme" change to the devtools instance after mount', () => { const setTheme = vi.spyOn(TanstackQueryDevtools.prototype, 'setTheme') - const queryClient = new QueryClient() const [theme, setThemeSignal] = createSignal('light') render(() => ) @@ -244,8 +224,6 @@ describe('SolidQueryDevtools', () => { it('should call "unmount" on the devtools instance when the component unmounts', () => { const unmount = vi.spyOn(TanstackQueryDevtools.prototype, 'unmount') - const queryClient = new QueryClient() - const { unmount: unmountComponent } = render(() => ( )) diff --git a/packages/solid-query-devtools/src/__tests__/devtoolsPanel.test.tsx b/packages/solid-query-devtools/src/__tests__/devtoolsPanel.test.tsx index 76e5e30eeae..d69bac088cc 100644 --- a/packages/solid-query-devtools/src/__tests__/devtoolsPanel.test.tsx +++ b/packages/solid-query-devtools/src/__tests__/devtoolsPanel.test.tsx @@ -1,10 +1,16 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { render } from '@solidjs/testing-library' import { QueryClient, QueryClientProvider } from '@tanstack/solid-query' import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools' import SolidQueryDevtoolsPanel from '../devtoolsPanel' describe('SolidQueryDevtoolsPanel', () => { + let queryClient: QueryClient + + beforeEach(() => { + queryClient = new QueryClient() + }) + afterEach(() => { vi.restoreAllMocks() }) @@ -16,8 +22,6 @@ describe('SolidQueryDevtoolsPanel', () => { }) it('should not throw an error if query client is provided via context', () => { - const queryClient = new QueryClient() - expect(() => render(() => ( @@ -28,8 +32,6 @@ describe('SolidQueryDevtoolsPanel', () => { }) it('should not throw an error if query client is provided via props', () => { - const queryClient = new QueryClient() - expect(() => render(() => ), ).not.toThrow() @@ -40,7 +42,6 @@ describe('SolidQueryDevtoolsPanel', () => { TanstackQueryDevtoolsPanel.prototype, 'setOnClose', ) - const queryClient = new QueryClient() const onClose = vi.fn() render(() => ( @@ -55,8 +56,6 @@ describe('SolidQueryDevtoolsPanel', () => { TanstackQueryDevtoolsPanel.prototype, 'setOnClose', ) - const queryClient = new QueryClient() - render(() => ) expect(setOnClose).toHaveBeenCalledWith(expect.any(Function)) @@ -67,7 +66,6 @@ describe('SolidQueryDevtoolsPanel', () => { TanstackQueryDevtoolsPanel.prototype, 'setErrorTypes', ) - const queryClient = new QueryClient() const errorTypes = [ { name: 'Network', initializer: () => new Error('Network') }, ] @@ -84,8 +82,6 @@ describe('SolidQueryDevtoolsPanel', () => { TanstackQueryDevtoolsPanel.prototype, 'setErrorTypes', ) - const queryClient = new QueryClient() - render(() => ) expect(setErrorTypes).toHaveBeenCalledWith([]) @@ -93,8 +89,6 @@ describe('SolidQueryDevtoolsPanel', () => { it('should forward "theme" to the devtools instance', () => { const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme') - const queryClient = new QueryClient() - render(() => ) expect(setTheme).toHaveBeenCalledWith('dark') @@ -102,8 +96,6 @@ describe('SolidQueryDevtoolsPanel', () => { it('should default "theme" to "system" when the prop is omitted', () => { const setTheme = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'setTheme') - const queryClient = new QueryClient() - render(() => ) expect(setTheme).toHaveBeenCalledWith('system') @@ -114,16 +106,12 @@ describe('SolidQueryDevtoolsPanel', () => { TanstackQueryDevtoolsPanel.prototype, 'setClient', ) - const queryClient = new QueryClient() - render(() => ) expect(setClient).toHaveBeenCalledWith(queryClient) }) it('should preserve the default container height when "style" omits "height"', () => { - const queryClient = new QueryClient() - const { container } = render(() => ( { }) it('should let "style" override the default container height on the rendered element', () => { - const queryClient = new QueryClient() - const { container } = render(() => ( { it('should call "unmount" on the devtools instance when the component unmounts', () => { const unmount = vi.spyOn(TanstackQueryDevtoolsPanel.prototype, 'unmount') - const queryClient = new QueryClient() - const { unmount: unmountComponent } = render(() => ( ))