Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

exhaustive-deps.test.ts

1,088 lines30.4 KB

1import { RuleTester } from '@typescript-eslint/rule-tester'
2import { rule } from '../rules/exhaustive-deps/exhaustive-deps.rule'
3import { normalizeIndent } from './test-utils'
4​
5const ruleTester = new RuleTester()
6​
7ruleTester.run('exhaustive-deps', rule, {
8 valid: [
9 {
10 name: 'should pass when deps are passed in array (react)',
11 code: 'useQuery({ queryKey: ["todos"], queryFn: fetchTodos });',
12 },
13 {
14 name: 'should pass when deps are passed in array (solid)',
15 code: 'createQuery({ queryKey: ["todos"], queryFn: fetchTodos });',
16 },
17 {
18 name: 'should pass when deps are passed in array',
19 code: 'useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });',
20 },
21 {
22 name: 'should pass when deps are passed in template literal',
23 code: 'useQuery({ queryKey: [`entity/${id}`], queryFn: () => api.getEntity(id) });',
24 },
25 {
26 name: 'should not pass fetch',
27 code: 'useQuery({ queryKey: ["entity", id], queryFn: () => fetch(id) });',
28 },
29 {
30 name: 'should not pass axios.get',
31 code: 'useQuery({ queryKey: ["entity", id], queryFn: () => axios.get(id) });',
32 },
33 {
34 name: 'should not pass api.entity.get',
35 code: 'useQuery({ queryKey: ["entity", id], queryFn: () => api.entity.get(id) });',
36 },
37 {
38 name: 'should not pass api when is being used for calling a function',
39 code: `
40 import useApi from './useApi'
41​
42 const useFoo = () => {
43 const api = useApi();
44 return useQuery({
45 queryKey: ['foo'],
46 queryFn: () => api.fetchFoo(),
47 })
48 }
49 `,
50 },
51 {
52 name: 'should pass props.src',
53 code: `
54 function MyComponent(props) {
55 useQuery({ queryKey: ["entity", props.src], queryFn: () => api.entity.get(props.src) });
56 }
57 `,
58 },
59 {
60 name: 'identify !!props.id (unary expression)',
61 code: `
62 function MyComponent(props) {
63 useQuery({ queryKey: ["entity", !!props.id], queryFn: () => api.entity.get(props.id) });
64 }
65 `,
66 },
67 {
68 name: 'identify props?.id (chain expression)',
69 code: `
70 function MyComponent(props) {
71 useQuery({ queryKey: ["entity", props?.id], queryFn: () => api.entity.get(props?.id) });
72 }
73 `,
74 },
75 {
76 name: 'identify props!.id (ts non null expression)',
77 code: `
78 function MyComponent(props) {
79 useQuery({ queryKey: ["entity", props!.id], queryFn: () => api.entity.get(props!.id) });
80 }
81 `,
82 },
83 {
84 name: 'should ignore keys from callback',
85 code: `
86 function MyComponent(props) {
87 useQuery({
88 queryKey: ["foo", dep1],
89 queryFn: ({ queryKey: [, dep] }) => fetch(dep),
90 });
91 }
92 `,
93 },
94 {
95 name: 'should ignore type identifiers',
96 code: `
97 type Result = {};
98 function MyComponent(props) {
99 useQuery({
100 queryKey: ["foo", dep],
101 queryFn: () => api.get<Result>(dep),
102 });
103 }
104 `,
105 },
106 {
107 name: 'should add "...args" to deps',
108 code: `
109 function foo(...args) {}
110 function useData(arg, ...args) {
111 return useQuery({
112 queryKey: ['foo', arg, ...args],
113 queryFn: async () => foo([arg, ...args])
114 });
115 }
116 `,
117 },
118 {
119 name: 'should not add class to deps',
120 code: `
121 class Foo {}
122 useQuery({ queryKey: ['foo'], queryFn: async () => new Foo() });
123 `,
124 },
125 {
126 name: 'should not add `undefined` to deps',
127 code: `
128 useQuery({
129 queryKey: [],
130 queryFn: async () => {
131 if (undefined) {
132 return null;
133 }
134 return 1
135 },
136 });
137 `,
138 },
139 {
140 name: 'should not fail when queryKey is a queryKeyFactory while having a dep as first arg',
141 code: normalizeIndent`
142 const fooQueryKeyFactory = {
143 foo: () => ['foo'] as const,
144 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
145 }
146​
147 const useFoo = (num: number) =>
148 useQuery({
149 queryKey: fooQueryKeyFactory.foo(num),
150 queryFn: () => Promise.resolve(num),
151 })
152 `,
153 },
154 {
155 name: 'should not fail when queryKey is a queryKeyFactory while having a dep in object',
156 code: normalizeIndent`
157 const fooQueryKeyFactory = {
158 foo: () => ['foo'] as const,
159 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
160 }
161​
162 const useFoo = (num: number) =>
163 useQuery({
164 queryKey: fooQueryKeyFactory.foo({ x: num }),
165 queryFn: () => Promise.resolve(num),
166 })
167 `,
168 },
169 {
170 name: 'should not fail when queryKey is a queryKeyFactory while having a dep in object 2',
171 code: normalizeIndent`
172 const fooQueryKeyFactory = {
173 foo: () => ['foo'] as const,
174 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
175 }
176​
177 const useFoo = (num: number) =>
178 useQuery({
179 queryKey: fooQueryKeyFactory.foo({ num }),
180 queryFn: () => Promise.resolve(num),
181 })
182 `,
183 },
184 {
185 name: 'should not fail when queryKey is a queryKeyFactory while having a dep in array',
186 code: normalizeIndent`
187 const fooQueryKeyFactory = {
188 foo: () => ['foo'] as const,
189 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
190 }
191​
192 const useFoo = (num: number) =>
193 useQuery({
194 queryKey: fooQueryKeyFactory.foo([num]),
195 queryFn: () => Promise.resolve(num),
196 })
197 `,
198 },
199 {
200 name: 'should not fail when queryKey is a queryKeyFactory while having a dep in second arg',
201 code: normalizeIndent`
202 const fooQueryKeyFactory = {
203 foo: () => ['foo'] as const,
204 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
205 }
206​
207 const useFoo = (num: number) =>
208 useQuery({
209 queryKey: fooQueryKeyFactory.foo(1, num),
210 queryFn: () => Promise.resolve(num),
211 })
212 `,
213 },
214 {
215 name: 'should not fail when queryKey is a queryKeyFactory while having a dep is object prop',
216 code: normalizeIndent`
217 const fooQueryKeyFactory = {
218 foo: () => ['foo'] as const,
219 num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
220 }
221​
222 const useFoo = (obj: { num: number }) =>
223 useQuery({
224 queryKey: fooQueryKeyFactory.foo(obj.num),
225 queryFn: () => Promise.resolve(obj.num),
226 })
227 `,
228 },
229 {
230 name: 'should pass with queryKeyFactory result assigned to a variable',
231 code: `
232 function fooQueryKeyFactory(dep: string) {
233 return ["foo", dep];
234 }
235​
236 const useFoo = (dep: string) => {
237 const queryKey = fooQueryKeyFactory(dep);
238 return useQuery({
239 queryKey,
240 queryFn: () => Promise.resolve(dep),
241 })
242 }
243 `,
244 },
245 {
246 name: 'should pass with queryKeyFactory result assigned to a variable 2',
247 code: `
248 function fooQueryKeyFactory(dep: string) {
249 const x = ["foo", dep] as const;
250 return x as const;
251 }
252​
253 const useFoo = (dep: string) => {
254 const queryKey = fooQueryKeyFactory(dep);
255 return useQuery({
256 queryKey,
257 queryFn: () => Promise.resolve(dep),
258 })
259 }
260 `,
261 },
262 {
263 name: 'should not treat new Error as missing dependency',
264 code: normalizeIndent`
265 useQuery({
266 queryKey: ['foo'],
267 queryFn: () => Promise.reject(new Error('1')),
268 })
269 `,
270 },
271 {
272 name: 'should see id when there is a const assertion',
273 code: normalizeIndent`
274 const useX = (id: number) => {
275 return useQuery({
276 queryKey: ['foo', id] as const,
277 queryFn: async () => id,
278 })
279 }
280 `,
281 },
282 {
283 name: 'should see id when there is a const assertion of a variable dereference',
284 code: normalizeIndent`
285 const useX = (id: number) => {
286 const queryKey = ['foo', id]
287 return useQuery({
288 queryKey: queryKey as const,
289 queryFn: async () => id,
290 })
291 }
292 `,
293 },
294 {
295 name: 'should see id when there is a const assertion assigned to a variable',
296 code: normalizeIndent`
297 const useX = (id: number) => {
298 const queryKey = ['foo', id] as const
299 return useQuery({
300 queryKey,
301 queryFn: async () => id,
302 })
303 }
304 `,
305 },
306 {
307 name: 'should not fail if queryKey is having the whole object while queryFn uses some props of it',
308 code: normalizeIndent`
309 const state = { foo: 'foo', bar: 'bar' }
310​
311 useQuery({
312 queryKey: ['state', state],
313 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
314 })
315 `,
316 },
317 {
318 name: 'should not fail if queryKey does not include an internal dependency',
319 code: normalizeIndent`
320 useQuery({
321 queryKey: ["api"],
322 queryFn: async () => {
323 const response = Promise.resolve([]);
324 const data = await response.json();
325 return data[0].name;
326 },
327 });
328 `,
329 },
330 {
331 name: 'should ignore constants defined out of scope (react component, function declaration)',
332 code: `
333 const CONST_VAL = 1
334 function MyComponent() {
335 useQuery({
336 queryKey: ["foo"],
337 queryFn: () => CONST_VAL
338 });
339 }
340 `,
341 },
342 {
343 name: 'should ignore constants defined out of scope (react component, function expression)',
344 code: `
345 const CONST_VAL = 1
346 const MyComponent = () => {
347 useQuery({
348 queryKey: ["foo"],
349 queryFn: () => CONST_VAL
350 });
351 }
352 `,
353 },
354 {
355 name: 'should ignore constants defined out of scope (react component, anonymous function)',
356 code: `
357 const CONST_VAL = 1
358 const MyComponent = function () {
359 useQuery({
360 queryKey: ["foo"],
361 queryFn: () => CONST_VAL
362 });
363 }
364 `,
365 },
366 {
367 name: 'should ignore constants defined out of scope (non react component/hook function)',
368 code: `
369 const CONST_VAL = 1
370 function fn() {
371 return {
372 queryKey: ["foo"],
373 queryFn: () => CONST_VAL
374 }
375 }
376 `,
377 },
378 {
379 name: 'should ignore constants defined out of scope (react hook, function declaration)',
380 code: `
381 const CONST_VAL = 1
382 function useHook() {
383 useQuery({
384 queryKey: ["foo"],
385 queryFn: () => CONST_VAL
386 });
387 }
388 `,
389 },
390 {
391 name: 'should ignore constants defined out of scope (react hook, function expression)',
392 code: `
393 const CONST_VAL = 1
394 const useHook = () => {
395 useQuery({
396 queryKey: ["foo"],
397 queryFn: () => CONST_VAL
398 });
399 }
400 `,
401 },
402 {
403 name: 'should ignore constants defined out of scope (react hook, anonymous function)',
404 code: `
405 const CONST_VAL = 1
406 const useHook = function () {
407 useQuery({
408 queryKey: ["foo"],
409 queryFn: () => CONST_VAL
410 });
411 }
412 `,
413 },
414 {
415 name: 'query key with nullish coalescing operator',
416 code: `
417 const factory = (id: number) => ['foo', id];
418 function Component({ id }) {
419 useQuery({
420 queryKey: factory(id ?? -1),
421 queryFn: () => Promise.resolve({ id })
422 });
423 }
424 `,
425 },
426 {
427 name: 'instanceof value should not be in query key',
428 code: `
429 class SomeClass {}
430​
431 function Component({ value }) {
432 useQuery({
433 queryKey: ['foo'],
434 queryFn: () => {
435 return value instanceof SomeClass;
436 }
437 });
438 }
439 `,
440 },
441 {
442 name: 'queryFn as a ternary expression with dep and a skipToken',
443 code: normalizeIndent`
444 import { useQuery, skipToken } from "@tanstack/react-query";
445 const fetch = true
446​
447 function Component({ id }) {
448 useQuery({
449 queryKey: [id],
450 queryFn: fetch ? () => Promise.resolve(id) : skipToken
451 })
452 }
453 `,
454 },
455 {
456 name: 'should not fail when queryFn uses nullish coalescing operator',
457 code: normalizeIndent`
458 useQuery({
459 queryKey: ["foo", options],
460 queryFn: () => options?.params ?? options
461 });
462 `,
463 },
464 {
465 name: 'should not fail when queryKey uses arrow function to produce a key',
466 code: normalizeIndent`
467 const obj = reactive<{ boo?: string }>({});
468​
469 const query = useQuery({
470 queryKey: ['foo', () => obj.boo],
471 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
472 enable: () => !!obj.boo,
473 });
474 `,
475 },
476 {
477 name: 'should not fail when queryKey uses arrow function to produce a key as the body return',
478 code: normalizeIndent`
479 const obj = reactive<{ boo?: string }>({});
480​
481 const query = useQuery({
482 queryKey: ['foo', () => { return obj.boo }],
483 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
484 enable: () => !!obj.boo,
485 });
486 `,
487 },
488 {
489 name: 'should not fail when queryKey uses function expression to produce a key as the body return',
490 code: normalizeIndent`
491 const obj = reactive<{ boo?: string }>({});
492​
493 const query = useQuery({
494 queryKey: ['foo', function() {
495 return obj.boo
496 }],
497 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
498 enable: () => !!obj.boo,
499 });
500 `,
501 },
502 {
503 name: 'should not fail when queryFn inside queryOptions contains a reference to an external variable',
504 code: normalizeIndent`
505 const EXTERNAL = 1;
506​
507 export const queries = {
508 foo: queryOptions({
509 queryKey: ['foo'],
510 queryFn: () => Promise.resolve(EXTERNAL),
511 }),
512 };
513 `,
514 },
515 {
516 name: 'should pass with optional chaining as key',
517 code: `
518 function useTest(data?: any) {
519 return useQuery({
520 queryKey: ['query-name', data?.address],
521 queryFn: async () => sendQuery(data.address),
522 enabled: !!data?.address,
523 })
524 }
525 `,
526 },
527 {
528 name: 'should pass with optional chaining as key and non-null assertion in queryFn',
529 code: `
530 function useTest(data?: any) {
531 return useQuery({
532 queryKey: ['query-name', data?.address],
533 queryFn: async () => sendQuery(data!.address),
534 enabled: !!data?.address,
535 })
536 }
537 `,
538 },
539 {
540 name: 'should pass with optional chaining as key and non-null assertion at the end of the variable in queryFn',
541 code: `
542 function useTest(data?: any) {
543 return useQuery({
544 queryKey: ['query-name', data?.address],
545 queryFn: async () => sendQuery(data!.address!),
546 enabled: !!data?.address,
547 })
548 }
549 `,
550 },
551 {
552 name: 'should pass in Vue file when deps are correctly included (script setup)',
553 filename: 'Component.vue',
554 code: normalizeIndent`
555 import { useQuery } from '@tanstack/vue-query'
556​
557 const id = 1
558 useQuery({
559 queryKey: ['entity', id],
560 queryFn: () => fetchEntity(id),
561 })
562 `,
563 },
564 {
565 name: 'should not require imports in queryKey for Vue files',
566 filename: 'Component.vue',
567 code: normalizeIndent`
568 import { useQuery } from '@tanstack/vue-query'
569 import { fetchTodos } from './api'
570​
571 useQuery({
572 queryKey: ['todos'],
573 queryFn: () => fetchTodos(),
574 })
575 `,
576 },
577 {
578 name: 'should not require global fetch in queryKey for Vue files',
579 filename: 'Component.vue',
580 code: normalizeIndent`
581 import { useQuery } from '@tanstack/vue-query'
582​
583 const id = 1
584 useQuery({
585 queryKey: ['entity', id],
586 queryFn: () => fetch('/api/entity/' + id),
587 })
588 `,
589 },
590 ],
591 invalid: [
592 {
593 name: 'should fail when deps are missing in query factory',
594 code: normalizeIndent`
595 const todoQueries = {
596 list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
597 detail: (id) => ({ queryKey: ['entity'], queryFn: () => fetchEntity(id) })
598 }
599 `,
600 errors: [
601 {
602 messageId: 'missingDeps',
603 data: { deps: 'id' },
604 suggestions: [
605 {
606 messageId: 'fixTo',
607 data: { result: "['entity', id]" },
608 output: normalizeIndent`
609 const todoQueries = {
610 list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
611 detail: (id) => ({ queryKey: ['entity', id], queryFn: () => fetchEntity(id) })
612 }
613 `,
614 },
615 ],
616 },
617 ],
618 },
619 {
620 name: 'should fail when no deps are passed (react)',
621 code: normalizeIndent`
622 function Component() {
623 const id = 1;
624 useQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
625 }
626 `,
627 errors: [
628 {
629 messageId: 'missingDeps',
630 data: { deps: 'id' },
631 suggestions: [
632 {
633 messageId: 'fixTo',
634 data: { result: '["entity", id]' },
635 output: normalizeIndent`
636 function Component() {
637 const id = 1;
638 useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
639 }
640 `,
641 },
642 ],
643 },
644 ],
645 },
646 {
647 name: 'should fail when no deps are passed (solid)',
648 code: normalizeIndent`
649 function Component() {
650 const id = 1;
651 createQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
652 }
653 `,
654 errors: [
655 {
656 messageId: 'missingDeps',
657 data: { deps: 'id' },
658 suggestions: [
659 {
660 messageId: 'fixTo',
661 data: { result: '["entity", id]' },
662 output: normalizeIndent`
663 function Component() {
664 const id = 1;
665 createQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
666 }
667 `,
668 },
669 ],
670 },
671 ],
672 },
673 {
674 name: 'should fail when deps are passed incorrectly',
675 code: normalizeIndent`
676 function Component() {
677 const id = 1;
678 useQuery({ queryKey: ["entity/\${id}"], queryFn: () => api.getEntity(id) });
679 }
680 `,
681 errors: [
682 {
683 messageId: 'missingDeps',
684 data: { deps: 'id' },
685 suggestions: [
686 {
687 messageId: 'fixTo',
688 data: { result: '["entity/${id}", id]' },
689 output: normalizeIndent`
690 function Component() {
691 const id = 1;
692 useQuery({ queryKey: ["entity/\${id}", id], queryFn: () => api.getEntity(id) });
693 }
694 `,
695 },
696 ],
697 },
698 ],
699 },
700 {
701 name: 'should pass missing dep while key has a template literal',
702 code: normalizeIndent`
703 function Component() {
704 const a = 1;
705 const b = 2;
706 useQuery({ queryKey: [\`entity/\${a}\`], queryFn: () => api.getEntity(a, b) });
707 }
708 `,
709 errors: [
710 {
711 messageId: 'missingDeps',
712 data: { deps: 'b' },
713 suggestions: [
714 {
715 messageId: 'fixTo',
716 data: { result: '[`entity/${a}`, b]' },
717 output: normalizeIndent`
718 function Component() {
719 const a = 1;
720 const b = 2;
721 useQuery({ queryKey: [\`entity/\${a}\`, b], queryFn: () => api.getEntity(a, b) });
722 }
723 `,
724 },
725 ],
726 },
727 ],
728 },
729 {
730 name: 'should fail when dep exists inside setter and missing in queryKey',
731 code: normalizeIndent`
732 function Component() {
733 const [id] = React.useState(1);
734 useQuery({
735 queryKey: ["entity"],
736 queryFn: () => {
737 const { data } = axios.get(\`.../\${id}\`);
738 return data;
739 }
740 });
741 }
742 `,
743 errors: [
744 {
745 messageId: 'missingDeps',
746 data: { deps: 'id' },
747 suggestions: [
748 {
749 messageId: 'fixTo',
750 data: { result: '["entity", id]' },
751 output: normalizeIndent`
752 function Component() {
753 const [id] = React.useState(1);
754 useQuery({
755 queryKey: ["entity", id],
756 queryFn: () => {
757 const { data } = axios.get(\`.../\${id}\`);
758 return data;
759 }
760 });
761 }
762 `,
763 },
764 ],
765 },
766 ],
767 },
768 {
769 name: 'should fail when dep does not exist while having a complex queryKey',
770 code: normalizeIndent`
771 const todoQueries = {
772 key: (a, b, c, d, e) => ({
773 queryKey: ["entity", a, [b], { c }, 1, true],
774 queryFn: () => api.getEntity(a, b, c, d, e)
775 })
776 }
777 `,
778 errors: [
779 {
780 messageId: 'missingDeps',
781 data: { deps: 'd, e' },
782 suggestions: [
783 {
784 messageId: 'fixTo',
785 data: { result: '["entity", a, [b], { c }, 1, true, d, e]' },
786 output: normalizeIndent`
787 const todoQueries = {
788 key: (a, b, c, d, e) => ({
789 queryKey: ["entity", a, [b], { c }, 1, true, d, e],
790 queryFn: () => api.getEntity(a, b, c, d, e)
791 })
792 }
793 `,
794 },
795 ],
796 },
797 ],
798 },
799 {
800 name: 'should fail when dep does not exist while having a complex queryKey #2',
801 code: normalizeIndent`
802 const todoQueries = {
803 key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
804 queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7]],
805 queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
806 }),
807 };
808 `,
809 errors: [
810 {
811 messageId: 'missingDeps',
812 data: { deps: 'dep8' },
813 suggestions: [
814 {
815 messageId: 'fixTo',
816 data: {
817 result:
818 "['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8]",
819 },
820 output: normalizeIndent`
821 const todoQueries = {
822 key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
823 queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8],
824 queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
825 }),
826 };
827 `,
828 },
829 ],
830 },
831 ],
832 },
833 {
834 name: 'should fail when two deps that depend on each other are missing',
835 code: normalizeIndent`
836 function Component({ map, key }) {
837 useQuery({ queryKey: ["key"], queryFn: () => api.get(map[key]) });
838 }
839 `,
840 errors: [
841 {
842 messageId: 'missingDeps',
843 data: { deps: 'map[key]' },
844 suggestions: [
845 {
846 messageId: 'fixTo',
847 data: {
848 result: '["key", map[key]]',
849 },
850 output: normalizeIndent`
851 function Component({ map, key }) {
852 useQuery({ queryKey: ["key", map[key]], queryFn: () => api.get(map[key]) });
853 }
854 `,
855 },
856 ],
857 },
858 ],
859 },
860 {
861 name: 'should fail when a queryKey is a reference of an array expression with a missing dep',
862 code: normalizeIndent`
863 function Component() {
864 const x = 5;
865 const queryKey = ['foo']
866 useQuery({ queryKey, queryFn: () => x })
867 }
868 `,
869 errors: [
870 {
871 messageId: 'missingDeps',
872 data: { deps: 'x' },
873 suggestions: [
874 {
875 messageId: 'fixTo',
876 data: {
877 result: "['foo', x]",
878 },
879 output: normalizeIndent`
880 function Component() {
881 const x = 5;
882 const queryKey = ['foo', x]
883 useQuery({ queryKey, queryFn: () => x })
884 }
885 `,
886 },
887 ],
888 },
889 ],
890 },
891 {
892 name: 'should fail when queryKey is a queryKeyFactory while having missing dep',
893 code: normalizeIndent`
894 const fooQueryKeyFactory = { foo: () => ['foo'] as const }
895​
896 const useFoo = (num: number) =>
897 useQuery({
898 queryKey: fooQueryKeyFactory.foo(),
899 queryFn: () => Promise.resolve(num),
900 })
901 `,
902 errors: [
903 {
904 messageId: 'missingDeps',
905 data: { deps: 'num' },
906 },
907 ],
908 },
909 {
910 name: 'should fail if queryFn is using multiple object props when only one of them is in the queryKey',
911 code: normalizeIndent`
912 function Component() {
913 const state = { foo: 'foo', bar: 'bar' }
914​
915 useQuery({
916 queryKey: ['state', state.foo],
917 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
918 })
919 }
920 `,
921 errors: [
922 {
923 suggestions: [
924 {
925 messageId: 'fixTo',
926 output: normalizeIndent`
927 function Component() {
928 const state = { foo: 'foo', bar: 'bar' }
929​
930 useQuery({
931 queryKey: ['state', state.foo, state.bar],
932 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
933 })
934 }
935 `,
936 },
937 ],
938 messageId: 'missingDeps',
939 data: { deps: 'state.bar' },
940 },
941 ],
942 },
943 {
944 name: 'should fail if queryFn is invalid while using FunctionExpression syntax',
945 code: normalizeIndent`
946 function Component() {
947 const id = 1;
948​
949 useQuery({
950 queryKey: [],
951 queryFn() {
952 Promise.resolve(id)
953 }
954 });
955 }
956 `,
957 errors: [
958 {
959 suggestions: [
960 {
961 messageId: 'fixTo',
962 output: normalizeIndent`
963 function Component() {
964 const id = 1;
965​
966 useQuery({
967 queryKey: [id],
968 queryFn() {
969 Promise.resolve(id)
970 }
971 });
972 }
973 `,
974 },
975 ],
976 messageId: 'missingDeps',
977 data: { deps: 'id' },
978 },
979 ],
980 },
981 {
982 name: 'should fail if queryFn is a ternary expression with missing dep and a skipToken',
983 code: normalizeIndent`
984 import { useQuery, skipToken } from "@tanstack/react-query";
985 const fetch = true
986​
987 function Component({ id }) {
988 useQuery({
989 queryKey: [],
990 queryFn: fetch ? () => Promise.resolve(id) : skipToken
991 })
992 }
993 `,
994 errors: [
995 {
996 suggestions: [
997 {
998 messageId: 'fixTo',
999 output: normalizeIndent`
1000 import { useQuery, skipToken } from "@tanstack/react-query";
1001 const fetch = true
1002​
1003 function Component({ id }) {
1004 useQuery({
1005 queryKey: [id],
1006 queryFn: fetch ? () => Promise.resolve(id) : skipToken
1007 })
1009 `,
1010 },
1011 ],
1012 messageId: 'missingDeps',
1013 data: { deps: 'id' },
1014 },
1015 ],
1016 },
1018 name: 'should fail in Vue file when deps are missing (script setup)',
1019 filename: 'Component.vue',
1020 code: normalizeIndent`
1021 import { useQuery } from '@tanstack/vue-query'
1022​
1023 const id = 1
1024 useQuery({
1025 queryKey: ['entity'],
1026 queryFn: () => fetchEntity(id),
1027 })
1028 `,
1029 errors: [
1031 messageId: 'missingDeps',
1032 data: { deps: 'id' },
1033 suggestions: [
1035 messageId: 'fixTo',
1036 data: { result: "['entity', id]" },
1037 output: normalizeIndent`
1038 import { useQuery } from '@tanstack/vue-query'
1039​
1040 const id = 1
1041 useQuery({
1042 queryKey: ['entity', id],
1043 queryFn: () => fetchEntity(id),
1044 })
1045 `,
1046 },
1047 ],
1048 },
1049 ],
1050 },
1052 name: 'should fail in Vue file when multiple deps are missing',
1053 filename: 'Component.vue',
1054 code: normalizeIndent`
1055 import { useQuery } from '@tanstack/vue-query'
1056​
1057 const userId = 1
1058 const orgId = 2
1059 useQuery({
1060 queryKey: ['users'],
1061 queryFn: () => fetchUser(userId, orgId),
1062 })
1063 `,
1064 errors: [
1066 messageId: 'missingDeps',
1067 data: { deps: 'userId, orgId' },
1068 suggestions: [
1070 messageId: 'fixTo',
1071 data: { result: "['users', userId, orgId]" },
1072 output: normalizeIndent`
1073 import { useQuery } from '@tanstack/vue-query'
1074​
1075 const userId = 1
1076 const orgId = 2
1077 useQuery({
1078 queryKey: ['users', userId, orgId],
1079 queryFn: () => fetchUser(userId, orgId),
1080 })
1081 `,
1082 },
1083 ],
1084 },
1085 ],
1086 },
1087 ],