Open History to see the last commit
1import { RuleTester } from '@typescript-eslint/rule-tester'
2import { rule } from '../rules/stable-query-client/stable-query-client.rule'
3import { normalizeIndent } from './test-utils'
4β
5const ruleTester = new RuleTester()
6β
7ruleTester.run('stable-query-client', rule, {
8 valid: [
9 ...[
10 'const Component = () => { const [client] = React.useState(() => new QueryClient()); return client; }',
11 'const useClient = () => React.useState(() => new QueryClient());',
12 'const Component = function() { const [client] = React.useState(() => new QueryClient()); return client; }',
13 'function Component() { const [client] = React.useState(() => { const client = new QueryClient(); return client; }); return client; }',
14 'const Component = () => { const [client] = React.useState(() => { const client = new QueryClient(); return client; }); return client; }',
15 'function Component() { function createClient() { const client = new QueryClient(); return client; } return createClient; }',
16 'const createClient = () => { const client = new QueryClient(); return client; }',
17 'const ServerComponent = async () => { const client = new QueryClient(); return client; }',
18 'const client = new QueryClient();',
19 ].map((code) => ({
20 code: `import { QueryClient } from "@tanstack/react-query";\n${code}`,
21 })),
22 {
23 name: 'QueryClient is stable when wrapped in React.useState',
24 code: normalizeIndent`
25 import { QueryClient } from "@tanstack/react-query";
26β
27 function Component() {
28 const [queryClient] = React.useState(() => new QueryClient());
29 return;
30 }
31 `,
32 },
33 {
34 name: 'QueryClient is stable when wrapped in useState',
35 code: normalizeIndent`
36 import { QueryClient } from "@tanstack/react-query";
37β
38 function Component() {
39 const [queryClient] = useState(() => new QueryClient());
40 return;
41 }
42 `,
43 },
44 {
45 name: 'QueryClient is stable when wrapped in React.useMemo',
46 code: normalizeIndent`
47 import { QueryClient } from "@tanstack/react-query";
48β
49 function Component() {
50 const [queryClient] = React.useMemo(() => new QueryClient(), []);
51 return;
52 }
53 `,
54 },
55 {
56 name: 'QueryClient is stable when wrapped in useAnything',
57 code: normalizeIndent`
58 import { QueryClient } from "@tanstack/react-query";
59β
60 function Component() {
61 const [queryClient] = useAnything(() => new QueryClient());
62 return;
63 }
64 `,
65 },
66 {
67 name: 'QueryClient is imported from a non-tanstack package',
68 code: normalizeIndent`
69 import { QueryClient } from "other-library";
70β
71 function Component() {
72 const queryClient = new QueryClient();
73 return;
74 }
75 `,
76 },
77 {
78 name: 'QueryClient is not imported from @tanstack/react-query',
79 code: normalizeIndent`
80 import { QueryClient } from "@tanstack/solid-query";
81β
82 function Component() {
83 const queryClient = new QueryClient();
84 return;
85 }
86 `,
87 },
88 {
89 name: 'QueryClient is invoked outside of a function',
90 code: normalizeIndent`
91 import { QueryClient } from "@tanstack/solid-query";
92β
93 const queryClient = new QueryClient();
94β
95 function Component() {
96 return;
97 }
98 `,
99 },
100 {
101 name: 'QueryClient is invoked in a non-component function',
102 code: normalizeIndent`
103 import { QueryClient } from "@tanstack/solid-query";
104β
105 function someFn() {
106 const queryClient = new QueryClient();
107 return;
108 }
109 `,
110 },
111 {
112 name: 'QueryClient is not flagged when imported from a non-react-query package in an async component',
113 code: normalizeIndent`
114 import { QueryClient } from "@tanstack/solid-query";
115β
116 async function AsyncComponent() {
117 const queryClient = new QueryClient();
118 return;
119 }
120 `,
121 },
122 {
123 name: 'QueryClient is not flagged in an async react-query server component',
124 code: normalizeIndent`
125 import { QueryClient } from "@tanstack/react-query";
126β
127 async function ServerComponent() {
128 const queryClient = new QueryClient();
129 return;
130 }
131 `,
132 },
133 ],
134 invalid: [
135 ...['Component', 'useClient'].flatMap((name) =>
136 ['() =>', 'function()'].map((fn) => ({
137 code: `import { QueryClient } from "@tanstack/react-query";
138const ${name} = ${fn} { const client = new QueryClient(); return client; }`,
139 output: `import { QueryClient } from "@tanstack/react-query";
140const ${name} = ${fn} { const [client] = React.useState(() => new QueryClient()); return client; }`,
141 errors: [{ messageId: 'unstable' as const }],
142 })),
143 ),
144 ...[
145 'function Component() { return <QueryClientProvider client={new QueryClient()} />; }',
146 'const Component = () => { return <QueryClientProvider client={new QueryClient()} />; }',
147 'const Component = () => <QueryClientProvider client={new QueryClient()} />;',
148 'const useClient = () => new QueryClient();',
149 ].map((code) => ({
150 filename: 'test.tsx',
151 languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },
152 code: `import { QueryClient, QueryClientProvider } from "@tanstack/react-query";\n${code}`,
153 output: null,
154 errors: [{ messageId: 'unstable' as const }],
155 })),
156 {
157 name: 'QueryClient is not stable when it is not wrapped in React.useState in component',
158 code: normalizeIndent`
159 import { QueryClient } from "@tanstack/react-query";
160β
161 function Component() {
162 const queryClient = new QueryClient();
163 return;
164 }
165 `,
166 output: normalizeIndent`
167 import { QueryClient } from "@tanstack/react-query";
168β
169 function Component() {
170 const [queryClient] = React.useState(() => new QueryClient());
171 return;
172 }
173 `,
174 errors: [{ messageId: 'unstable' }],
175 },
176 {
177 name: 'QueryClient is not stable when it is not wrapped in React.useState in custom hook',
178 code: normalizeIndent`
179 import { QueryClient } from "@tanstack/react-query";
180β
181 function useHook() {
182 const queryClient = new QueryClient();
183 return;
184 }
185 `,
186 output: normalizeIndent`
187 import { QueryClient } from "@tanstack/react-query";
188β
189 function useHook() {
190 const [queryClient] = React.useState(() => new QueryClient());
191 return;
192 }
193 `,
194 errors: [{ messageId: 'unstable' }],
195 },
196 {
197 name: 'preserve QueryClient options',
198 code: normalizeIndent`
199 import { QueryClient } from "@tanstack/react-query";
200β
201 function Component() {
202 const queryClient = new QueryClient({ defaultOptions: { /* */ } });
203 return;
204 }
205 `,
206 output: normalizeIndent`
207 import { QueryClient } from "@tanstack/react-query";
208β
209 function Component() {
210 const [queryClient] = React.useState(() => new QueryClient({ defaultOptions: { /* */ } }));
211 return;
212 }
213 `,
214 errors: [{ messageId: 'unstable' }],
215 },
216 {
217 name: 'preserve QueryClient variable declarator name',
218 code: normalizeIndent`
219 import { QueryClient } from "@tanstack/react-query";
220β
221 function Component() {
222 const customName = new QueryClient();
223 return;
224 }
225 `,
226 output: normalizeIndent`
227 import { QueryClient } from "@tanstack/react-query";
228β
229 function Component() {
230 const [customName] = React.useState(() => new QueryClient());
231 return;
232 }
233 `,
234 errors: [{ messageId: 'unstable' }],
235 },
236 {
237 name: 'QueryClient with destructuring pattern reports error without autofix',
238 code: normalizeIndent`
239 import { QueryClient } from "@tanstack/react-query";
240β
241 function Component() {
242 const { defaultOptions } = new QueryClient();
243 return;
244 }
245 `,
246 output: null,
247 errors: [{ messageId: 'unstable' }],
248 },
249 ],
250})