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

exhaustive-deps.test.ts

922 lines26.0 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 not treat new Error as missing dependency',
231 code: normalizeIndent`
232 useQuery({
233 queryKey: ['foo'],
234 queryFn: () => Promise.reject(new Error('1')),
235 })
236 `,
237 },
238 {
239 name: 'should see id when there is a const assertion',
240 code: normalizeIndent`
241 const useX = (id: number) => {
242 return useQuery({
243 queryKey: ['foo', id] as const,
244 queryFn: async () => id,
245 })
246 }
247 `,
248 },
249 {
250 name: 'should not fail if queryKey is having the whole object while queryFn uses some props of it',
251 code: normalizeIndent`
252 const state = { foo: 'foo', bar: 'bar' }
253​
254 useQuery({
255 queryKey: ['state', state],
256 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
257 })
258 `,
259 },
260 {
261 name: 'should not fail if queryKey does not include an internal dependency',
262 code: normalizeIndent`
263 useQuery({
264 queryKey: ["api"],
265 queryFn: async () => {
266 const response = Promise.resolve([]);
267 const data = await response.json();
268 return data[0].name;
269 },
270 });
271 `,
272 },
273 {
274 name: 'should ignore constants defined out of scope (react component, function declaration)',
275 code: `
276 const CONST_VAL = 1
277 function MyComponent() {
278 useQuery({
279 queryKey: ["foo"],
280 queryFn: () => CONST_VAL
281 });
282 }
283 `,
284 },
285 {
286 name: 'should ignore constants defined out of scope (react component, function expression)',
287 code: `
288 const CONST_VAL = 1
289 const MyComponent = () => {
290 useQuery({
291 queryKey: ["foo"],
292 queryFn: () => CONST_VAL
293 });
294 }
295 `,
296 },
297 {
298 name: 'should ignore constants defined out of scope (react component, anonymous function)',
299 code: `
300 const CONST_VAL = 1
301 const MyComponent = function () {
302 useQuery({
303 queryKey: ["foo"],
304 queryFn: () => CONST_VAL
305 });
306 }
307 `,
308 },
309 {
310 name: 'should ignore constants defined out of scope (non react component/hook function)',
311 code: `
312 const CONST_VAL = 1
313 function fn() {
314 return {
315 queryKey: ["foo"],
316 queryFn: () => CONST_VAL
317 }
318 }
319 `,
320 },
321 {
322 name: 'should ignore constants defined out of scope (react hook, function declaration)',
323 code: `
324 const CONST_VAL = 1
325 function useHook() {
326 useQuery({
327 queryKey: ["foo"],
328 queryFn: () => CONST_VAL
329 });
330 }
331 `,
332 },
333 {
334 name: 'should ignore constants defined out of scope (react hook, function expression)',
335 code: `
336 const CONST_VAL = 1
337 const useHook = () => {
338 useQuery({
339 queryKey: ["foo"],
340 queryFn: () => CONST_VAL
341 });
342 }
343 `,
344 },
345 {
346 name: 'should ignore constants defined out of scope (react hook, anonymous function)',
347 code: `
348 const CONST_VAL = 1
349 const useHook = function () {
350 useQuery({
351 queryKey: ["foo"],
352 queryFn: () => CONST_VAL
353 });
354 }
355 `,
356 },
357 {
358 name: 'query key with nullish coalescing operator',
359 code: `
360 const factory = (id: number) => ['foo', id];
361 function Component({ id }) {
362 useQuery({
363 queryKey: factory(id ?? -1),
364 queryFn: () => Promise.resolve({ id })
365 });
366 }
367 `,
368 },
369 {
370 name: 'instanceof value should not be in query key',
371 code: `
372 class SomeClass {}
373​
374 function Component({ value }) {
375 useQuery({
376 queryKey: ['foo'],
377 queryFn: () => {
378 return value instanceof SomeClass;
379 }
380 });
381 }
382 `,
383 },
384 {
385 name: 'queryFn as a ternary expression with dep and a skipToken',
386 code: normalizeIndent`
387 import { useQuery, skipToken } from "@tanstack/react-query";
388 const fetch = true
389​
390 function Component({ id }) {
391 useQuery({
392 queryKey: [id],
393 queryFn: fetch ? () => Promise.resolve(id) : skipToken
394 })
395 }
396 `,
397 },
398 {
399 name: 'should not fail when queryFn uses nullish coalescing operator',
400 code: normalizeIndent`
401 useQuery({
402 queryKey: ["foo", options],
403 queryFn: () => options?.params ?? options
404 });
405 `,
406 },
407 {
408 name: 'should not fail when queryKey uses arrow function to produce a key',
409 code: normalizeIndent`
410 const obj = reactive<{ boo?: string }>({});
411​
412 const query = useQuery({
413 queryKey: ['foo', () => obj.boo],
414 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
415 enable: () => !!obj.boo,
416 });
417 `,
418 },
419 {
420 name: 'should not fail when queryKey uses arrow function to produce a key as the body return',
421 code: normalizeIndent`
422 const obj = reactive<{ boo?: string }>({});
423​
424 const query = useQuery({
425 queryKey: ['foo', () => { return obj.boo }],
426 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
427 enable: () => !!obj.boo,
428 });
429 `,
430 },
431 {
432 name: 'should not fail when queryKey uses function expression to produce a key as the body return',
433 code: normalizeIndent`
434 const obj = reactive<{ boo?: string }>({});
435​
436 const query = useQuery({
437 queryKey: ['foo', function() {
438 return obj.boo
439 }],
440 queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
441 enable: () => !!obj.boo,
442 });
443 `,
444 },
445 {
446 name: 'should not fail when queryFn inside queryOptions contains a reference to an external variable',
447 code: normalizeIndent`
448 const EXTERNAL = 1;
449​
450 export const queries = {
451 foo: queryOptions({
452 queryKey: ['foo'],
453 queryFn: () => Promise.resolve(EXTERNAL),
454 }),
455 };
456 `,
457 },
458 {
459 name: 'should pass with optional chaining as key',
460 code: `
461 function useTest(data?: any) {
462 return useQuery({
463 queryKey: ['query-name', data?.address],
464 queryFn: async () => sendQuery(data.address),
465 enabled: !!data?.address,
466 })
467 }
468 `,
469 },
470 {
471 name: 'should pass with optional chaining as key and non-null assertion in queryFn',
472 code: `
473 function useTest(data?: any) {
474 return useQuery({
475 queryKey: ['query-name', data?.address],
476 queryFn: async () => sendQuery(data!.address),
477 enabled: !!data?.address,
478 })
479 }
480 `,
481 },
482 {
483 name: 'should pass with optional chaining as key and non-null assertion at the end of the variable in queryFn',
484 code: `
485 function useTest(data?: any) {
486 return useQuery({
487 queryKey: ['query-name', data?.address],
488 queryFn: async () => sendQuery(data!.address!),
489 enabled: !!data?.address,
490 })
491 }
492 `,
493 },
494 ],
495 invalid: [
496 {
497 name: 'should fail when deps are missing in query factory',
498 code: normalizeIndent`
499 const todoQueries = {
500 list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
501 detail: (id) => ({ queryKey: ['entity'], queryFn: () => fetchEntity(id) })
502 }
503 `,
504 errors: [
505 {
506 messageId: 'missingDeps',
507 data: { deps: 'id' },
508 suggestions: [
509 {
510 messageId: 'fixTo',
511 data: { result: "['entity', id]" },
512 output: normalizeIndent`
513 const todoQueries = {
514 list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
515 detail: (id) => ({ queryKey: ['entity', id], queryFn: () => fetchEntity(id) })
516 }
517 `,
518 },
519 ],
520 },
521 ],
522 },
523 {
524 name: 'should fail when no deps are passed (react)',
525 code: normalizeIndent`
526 function Component() {
527 const id = 1;
528 useQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
529 }
530 `,
531 errors: [
532 {
533 messageId: 'missingDeps',
534 data: { deps: 'id' },
535 suggestions: [
536 {
537 messageId: 'fixTo',
538 data: { result: '["entity", id]' },
539 output: normalizeIndent`
540 function Component() {
541 const id = 1;
542 useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
543 }
544 `,
545 },
546 ],
547 },
548 ],
549 },
550 {
551 name: 'should fail when no deps are passed (solid)',
552 code: normalizeIndent`
553 function Component() {
554 const id = 1;
555 createQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
556 }
557 `,
558 errors: [
559 {
560 messageId: 'missingDeps',
561 data: { deps: 'id' },
562 suggestions: [
563 {
564 messageId: 'fixTo',
565 data: { result: '["entity", id]' },
566 output: normalizeIndent`
567 function Component() {
568 const id = 1;
569 createQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
570 }
571 `,
572 },
573 ],
574 },
575 ],
576 },
577 {
578 name: 'should fail when deps are passed incorrectly',
579 code: normalizeIndent`
580 function Component() {
581 const id = 1;
582 useQuery({ queryKey: ["entity/\${id}"], queryFn: () => api.getEntity(id) });
583 }
584 `,
585 errors: [
586 {
587 messageId: 'missingDeps',
588 data: { deps: 'id' },
589 suggestions: [
590 {
591 messageId: 'fixTo',
592 data: { result: '["entity/${id}", id]' },
593 output: normalizeIndent`
594 function Component() {
595 const id = 1;
596 useQuery({ queryKey: ["entity/\${id}", id], queryFn: () => api.getEntity(id) });
597 }
598 `,
599 },
600 ],
601 },
602 ],
603 },
604 {
605 name: 'should pass missing dep while key has a template literal',
606 code: normalizeIndent`
607 function Component() {
608 const a = 1;
609 const b = 2;
610 useQuery({ queryKey: [\`entity/\${a}\`], queryFn: () => api.getEntity(a, b) });
611 }
612 `,
613 errors: [
614 {
615 messageId: 'missingDeps',
616 data: { deps: 'b' },
617 suggestions: [
618 {
619 messageId: 'fixTo',
620 data: { result: '[`entity/${a}`, b]' },
621 output: normalizeIndent`
622 function Component() {
623 const a = 1;
624 const b = 2;
625 useQuery({ queryKey: [\`entity/\${a}\`, b], queryFn: () => api.getEntity(a, b) });
626 }
627 `,
628 },
629 ],
630 },
631 ],
632 },
633 {
634 name: 'should fail when dep exists inside setter and missing in queryKey',
635 code: normalizeIndent`
636 function Component() {
637 const [id] = React.useState(1);
638 useQuery({
639 queryKey: ["entity"],
640 queryFn: () => {
641 const { data } = axios.get(\`.../\${id}\`);
642 return data;
643 }
644 });
645 }
646 `,
647 errors: [
648 {
649 messageId: 'missingDeps',
650 data: { deps: 'id' },
651 suggestions: [
652 {
653 messageId: 'fixTo',
654 data: { result: '["entity", id]' },
655 output: normalizeIndent`
656 function Component() {
657 const [id] = React.useState(1);
658 useQuery({
659 queryKey: ["entity", id],
660 queryFn: () => {
661 const { data } = axios.get(\`.../\${id}\`);
662 return data;
663 }
664 });
665 }
666 `,
667 },
668 ],
669 },
670 ],
671 },
672 {
673 name: 'should fail when dep does not exist while having a complex queryKey',
674 code: normalizeIndent`
675 const todoQueries = {
676 key: (a, b, c, d, e) => ({
677 queryKey: ["entity", a, [b], { c }, 1, true],
678 queryFn: () => api.getEntity(a, b, c, d, e)
679 })
680 }
681 `,
682 errors: [
683 {
684 messageId: 'missingDeps',
685 data: { deps: 'd, e' },
686 suggestions: [
687 {
688 messageId: 'fixTo',
689 data: { result: '["entity", a, [b], { c }, 1, true, d, e]' },
690 output: normalizeIndent`
691 const todoQueries = {
692 key: (a, b, c, d, e) => ({
693 queryKey: ["entity", a, [b], { c }, 1, true, d, e],
694 queryFn: () => api.getEntity(a, b, c, d, e)
695 })
696 }
697 `,
698 },
699 ],
700 },
701 ],
702 },
703 {
704 name: 'should fail when dep does not exist while having a complex queryKey #2',
705 code: normalizeIndent`
706 const todoQueries = {
707 key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
708 queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7]],
709 queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
710 }),
711 };
712 `,
713 errors: [
714 {
715 messageId: 'missingDeps',
716 data: { deps: 'dep8' },
717 suggestions: [
718 {
719 messageId: 'fixTo',
720 data: {
721 result:
722 "['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8]",
723 },
724 output: normalizeIndent`
725 const todoQueries = {
726 key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
727 queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8],
728 queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
729 }),
730 };
731 `,
732 },
733 ],
734 },
735 ],
736 },
737 {
738 name: 'should fail when two deps that depend on each other are missing',
739 code: normalizeIndent`
740 function Component({ map, key }) {
741 useQuery({ queryKey: ["key"], queryFn: () => api.get(map[key]) });
742 }
743 `,
744 errors: [
745 {
746 messageId: 'missingDeps',
747 data: { deps: 'map[key]' },
748 suggestions: [
749 {
750 messageId: 'fixTo',
751 data: {
752 result: '["key", map[key]]',
753 },
754 output: normalizeIndent`
755 function Component({ map, key }) {
756 useQuery({ queryKey: ["key", map[key]], queryFn: () => api.get(map[key]) });
757 }
758 `,
759 },
760 ],
761 },
762 ],
763 },
764 {
765 name: 'should fail when a queryKey is a reference of an array expression with a missing dep',
766 code: normalizeIndent`
767 function Component() {
768 const x = 5;
769 const queryKey = ['foo']
770 useQuery({ queryKey, queryFn: () => x })
771 }
772 `,
773 errors: [
774 {
775 messageId: 'missingDeps',
776 data: { deps: 'x' },
777 suggestions: [
778 {
779 messageId: 'fixTo',
780 data: {
781 result: "['foo', x]",
782 },
783 output: normalizeIndent`
784 function Component() {
785 const x = 5;
786 const queryKey = ['foo', x]
787 useQuery({ queryKey, queryFn: () => x })
788 }
789 `,
790 },
791 ],
792 },
793 ],
794 },
795 {
796 name: 'should fail when queryKey is a queryKeyFactory while having missing dep',
797 code: normalizeIndent`
798 const fooQueryKeyFactory = { foo: () => ['foo'] as const }
799​
800 const useFoo = (num: number) =>
801 useQuery({
802 queryKey: fooQueryKeyFactory.foo(),
803 queryFn: () => Promise.resolve(num),
804 })
805 `,
806 errors: [
807 {
808 messageId: 'missingDeps',
809 data: { deps: 'num' },
810 },
811 ],
812 },
813 {
814 name: 'should fail if queryFn is using multiple object props when only one of them is in the queryKey',
815 code: normalizeIndent`
816 function Component() {
817 const state = { foo: 'foo', bar: 'bar' }
818​
819 useQuery({
820 queryKey: ['state', state.foo],
821 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
822 })
823 }
824 `,
825 errors: [
826 {
827 suggestions: [
828 {
829 messageId: 'fixTo',
830 output: normalizeIndent`
831 function Component() {
832 const state = { foo: 'foo', bar: 'bar' }
833​
834 useQuery({
835 queryKey: ['state', state.foo, state.bar],
836 queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
837 })
838 }
839 `,
840 },
841 ],
842 messageId: 'missingDeps',
843 data: { deps: 'state.bar' },
844 },
845 ],
846 },
847 {
848 name: 'should fail if queryFn is invalid while using FunctionExpression syntax',
849 code: normalizeIndent`
850 function Component() {
851 const id = 1;
852​
853 useQuery({
854 queryKey: [],
855 queryFn() {
856 Promise.resolve(id)
857 }
858 });
859 }
860 `,
861 errors: [
862 {
863 suggestions: [
864 {
865 messageId: 'fixTo',
866 output: normalizeIndent`
867 function Component() {
868 const id = 1;
869​
870 useQuery({
871 queryKey: [id],
872 queryFn() {
873 Promise.resolve(id)
874 }
875 });
876 }
877 `,
878 },
879 ],
880 messageId: 'missingDeps',
881 data: { deps: 'id' },
882 },
883 ],
884 },
885 {
886 name: 'should fail if queryFn is a ternary expression with missing dep and a skipToken',
887 code: normalizeIndent`
888 import { useQuery, skipToken } from "@tanstack/react-query";
889 const fetch = true
890​
891 function Component({ id }) {
892 useQuery({
893 queryKey: [],
894 queryFn: fetch ? () => Promise.resolve(id) : skipToken
895 })
896 }
897 `,
898 errors: [
899 {
900 suggestions: [
901 {
902 messageId: 'fixTo',
903 output: normalizeIndent`
904 import { useQuery, skipToken } from "@tanstack/react-query";
905 const fetch = true
906​
907 function Component({ id }) {
908 useQuery({
909 queryKey: [id],
910 queryFn: fetch ? () => Promise.resolve(id) : skipToken
911 })
912 }
913 `,
914 },
915 ],
916 messageId: 'missingDeps',
917 data: { deps: 'id' },
918 },
919 ],
920 },
921 ],
922})