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

inject-mutation.test.ts

758 lines23.0 KB

1import {
2 ApplicationRef,
3 Component,
4 Injector,
5 input,
6 provideZonelessChangeDetection,
7 signal,
8} from '@angular/core'
9import { TestBed } from '@angular/core/testing'
10import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
11import { By } from '@angular/platform-browser'
12import { render } from '@testing-library/angular'
13import { queryKey, sleep } from '@tanstack/query-test-utils'
14import { QueryClient, injectMutation, provideTanStackQuery } from '..'
15import { expectSignals, setFixtureSignalInputs } from './test-utils'
16​
17describe('injectMutation', () => {
18 let queryClient: QueryClient
19​
20 beforeEach(() => {
21 queryClient = new QueryClient()
22 vi.useFakeTimers()
23 TestBed.configureTestingModule({
24 providers: [
25 provideZonelessChangeDetection(),
26 provideTanStackQuery(queryClient),
27 ],
28 })
29 })
30​
31 afterEach(() => {
32 vi.useRealTimers()
33 })
34​
35 it('should be in idle state initially', () => {
36 const mutation = TestBed.runInInjectionContext(() => {
37 return injectMutation(() => ({
38 mutationFn: (params) => sleep(0).then(() => params),
39 }))
40 })
41​
42 expectSignals(mutation, {
43 isIdle: true,
44 isPending: false,
45 isError: false,
46 isSuccess: false,
47 })
48 })
49​
50 it('should change state after invoking mutate', async () => {
51 const result = 'Mock data'
52​
53 @Component({
54 template: `
55 <div>isIdle: {{ mutation.isIdle() }}</div>
56 <div>isPending: {{ mutation.isPending() }}</div>
57 <div>isError: {{ mutation.isError() }}</div>
58 <div>isSuccess: {{ mutation.isSuccess() }}</div>
59 <div>data: {{ mutation.data() ?? 'none' }}</div>
60 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
61 `,
62 })
63 class Page {
64 readonly mutation = injectMutation(() => ({
65 mutationFn: (params: string) => sleep(10).then(() => params),
66 }))
67 }
68​
69 const rendered = await render(Page)
70​
71 rendered.fixture.componentInstance.mutation.mutate(result)
72 await vi.advanceTimersByTimeAsync(0)
73 rendered.fixture.detectChanges()
74​
75 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
76 expect(rendered.getByText('isPending: true')).toBeInTheDocument()
77 expect(rendered.getByText('isError: false')).toBeInTheDocument()
78 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
79 expect(rendered.getByText('data: none')).toBeInTheDocument()
80 expect(rendered.getByText('error: none')).toBeInTheDocument()
81 })
82​
83 it('should return error when request fails', async () => {
84 @Component({
85 template: `
86 <div>isIdle: {{ mutation.isIdle() }}</div>
87 <div>isPending: {{ mutation.isPending() }}</div>
88 <div>isError: {{ mutation.isError() }}</div>
89 <div>isSuccess: {{ mutation.isSuccess() }}</div>
90 <div>data: {{ mutation.data() ?? 'none' }}</div>
91 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
92 `,
93 })
94 class Page {
95 readonly mutation = injectMutation(() => ({
96 mutationFn: () =>
97 sleep(10).then(() => Promise.reject(new Error('Some error'))),
98 }))
99 }
100​
101 const rendered = await render(Page)
102​
103 rendered.fixture.componentInstance.mutation.mutate()
104​
105 await vi.advanceTimersByTimeAsync(11)
106 rendered.fixture.detectChanges()
107​
108 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
109 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
110 expect(rendered.getByText('isError: true')).toBeInTheDocument()
111 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
112 expect(rendered.getByText('data: none')).toBeInTheDocument()
113 expect(rendered.getByText('error: Some error')).toBeInTheDocument()
114 })
115​
116 it('should return data when request succeeds', async () => {
117 const result = 'Mock data'
118​
119 @Component({
120 template: `
121 <div>isIdle: {{ mutation.isIdle() }}</div>
122 <div>isPending: {{ mutation.isPending() }}</div>
123 <div>isError: {{ mutation.isError() }}</div>
124 <div>isSuccess: {{ mutation.isSuccess() }}</div>
125 <div>data: {{ mutation.data() ?? 'none' }}</div>
126 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
127 `,
128 })
129 class Page {
130 readonly mutation = injectMutation(() => ({
131 mutationFn: (params: string) => sleep(10).then(() => params),
132 }))
133 }
134​
135 const rendered = await render(Page)
136​
137 rendered.fixture.componentInstance.mutation.mutate(result)
138​
139 await vi.advanceTimersByTimeAsync(11)
140 rendered.fixture.detectChanges()
141​
142 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
143 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
144 expect(rendered.getByText('isError: false')).toBeInTheDocument()
145 expect(rendered.getByText('isSuccess: true')).toBeInTheDocument()
146 expect(rendered.getByText(`data: ${result}`)).toBeInTheDocument()
147 expect(rendered.getByText('error: none')).toBeInTheDocument()
148 })
149​
150 it('should update mutation when reactive options change', () => {
151 const mutationCache = queryClient.getMutationCache()
152 // Signal will be updated before the mutation is called
153 // this test confirms that the mutation uses the updated value
154 const key1 = queryKey()
155 const key2 = queryKey()
156 const mutationKey = signal(key1)
157 const mutation = TestBed.runInInjectionContext(() => {
158 return injectMutation(() => ({
159 mutationKey: mutationKey(),
160 mutationFn: (params: string) => sleep(0).then(() => params),
161 }))
162 })
163​
164 mutationKey.set(key2)
165​
166 mutation.mutate('xyz')
167​
168 const mutations = mutationCache.find({ mutationKey: key2 })
169​
170 expect(mutations?.options.mutationKey).toEqual(key2)
171 })
172​
173 it('should reset state after invoking mutation.reset', async () => {
174 @Component({
175 template: `
176 <div>isIdle: {{ mutation.isIdle() }}</div>
177 <div>isPending: {{ mutation.isPending() }}</div>
178 <div>isError: {{ mutation.isError() }}</div>
179 <div>isSuccess: {{ mutation.isSuccess() }}</div>
180 <div>data: {{ mutation.data() ?? 'none' }}</div>
181 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
182 `,
183 })
184 class Page {
185 readonly mutation = injectMutation(() => ({
186 mutationFn: () =>
187 sleep(10).then(() => Promise.reject(new Error('Some error'))),
188 }))
189 }
190​
191 const rendered = await render(Page)
192​
193 rendered.fixture.componentInstance.mutation.mutate()
194​
195 await vi.advanceTimersByTimeAsync(11)
196 rendered.fixture.detectChanges()
197​
198 expect(rendered.getByText('isError: true')).toBeInTheDocument()
199 expect(rendered.getByText('error: Some error')).toBeInTheDocument()
200​
201 rendered.fixture.componentInstance.mutation.reset()
202​
203 await vi.advanceTimersByTimeAsync(0)
204 rendered.fixture.detectChanges()
205​
206 expect(rendered.getByText('isIdle: true')).toBeInTheDocument()
207 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
208 expect(rendered.getByText('isError: false')).toBeInTheDocument()
209 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
210 expect(rendered.getByText('data: none')).toBeInTheDocument()
211 expect(rendered.getByText('error: none')).toBeInTheDocument()
212 })
213​
214 describe('side effects', () => {
215 beforeEach(() => {
216 vi.clearAllMocks()
217 })
218​
219 it('should call onMutate when passed as an option', async () => {
220 const onMutate = vi.fn()
221 const mutation = TestBed.runInInjectionContext(() => {
222 return injectMutation(() => ({
223 mutationFn: (params: string) => sleep(10).then(() => params),
224 onMutate,
225 }))
226 })
227​
228 mutation.mutate('')
229​
230 await vi.advanceTimersByTimeAsync(0)
231​
232 expect(onMutate).toHaveBeenCalledTimes(1)
233 })
234​
235 it('should call onError when passed as an option', async () => {
236 const onError = vi.fn()
237 const mutation = TestBed.runInInjectionContext(() => {
238 return injectMutation(() => ({
239 mutationFn: (_params: string) =>
240 sleep(10).then(() => Promise.reject(new Error('Some error'))),
241 onError,
242 }))
243 })
244​
245 mutation.mutate('')
246​
247 await vi.advanceTimersByTimeAsync(11)
248​
249 expect(onError).toHaveBeenCalledTimes(1)
250 })
251​
252 it('should call onSuccess when passed as an option', async () => {
253 const onSuccess = vi.fn()
254 const mutation = TestBed.runInInjectionContext(() => {
255 return injectMutation(() => ({
256 mutationFn: (params: string) => sleep(10).then(() => params),
257 onSuccess,
258 }))
259 })
260​
261 mutation.mutate('')
262​
263 await vi.advanceTimersByTimeAsync(11)
264​
265 expect(onSuccess).toHaveBeenCalledTimes(1)
266 })
267​
268 it('should call onSettled when passed as an option', async () => {
269 const onSettled = vi.fn()
270 const mutation = TestBed.runInInjectionContext(() => {
271 return injectMutation(() => ({
272 mutationFn: (params: string) => sleep(10).then(() => params),
273 onSettled,
274 }))
275 })
276​
277 mutation.mutate('')
278​
279 await vi.advanceTimersByTimeAsync(11)
280​
281 expect(onSettled).toHaveBeenCalledTimes(1)
282 })
283​
284 it('should call onError when passed as an argument of mutate function', async () => {
285 const onError = vi.fn()
286 const mutation = TestBed.runInInjectionContext(() => {
287 return injectMutation(() => ({
288 mutationFn: (_params: string) =>
289 sleep(10).then(() => Promise.reject(new Error('Some error'))),
290 }))
291 })
292​
293 mutation.mutate('', { onError })
294​
295 await vi.advanceTimersByTimeAsync(11)
296​
297 expect(onError).toHaveBeenCalledTimes(1)
298 })
299​
300 it('should call onSuccess when passed as an argument of mutate function', async () => {
301 const onSuccess = vi.fn()
302 const mutation = TestBed.runInInjectionContext(() => {
303 return injectMutation(() => ({
304 mutationFn: (params: string) => sleep(10).then(() => params),
305 }))
306 })
307​
308 mutation.mutate('', { onSuccess })
309​
310 await vi.advanceTimersByTimeAsync(11)
311​
312 expect(onSuccess).toHaveBeenCalledTimes(1)
313 })
314​
315 it('should call onSettled when passed as an argument of mutate function', async () => {
316 const onSettled = vi.fn()
317 const mutation = TestBed.runInInjectionContext(() => {
318 return injectMutation(() => ({
319 mutationFn: (params: string) => sleep(10).then(() => params),
320 }))
321 })
322​
323 mutation.mutate('', { onSettled })
324​
325 await vi.advanceTimersByTimeAsync(11)
326​
327 expect(onSettled).toHaveBeenCalledTimes(1)
328 })
329​
330 it('should fire both onSettled functions', async () => {
331 const onSettled = vi.fn()
332 const onSettledOnFunction = vi.fn()
333 const mutation = TestBed.runInInjectionContext(() => {
334 return injectMutation(() => ({
335 mutationFn: (params: string) => sleep(10).then(() => params),
336 onSettled,
337 }))
338 })
339​
340 mutation.mutate('', { onSettled: onSettledOnFunction })
341​
342 await vi.advanceTimersByTimeAsync(11)
343​
344 expect(onSettled).toHaveBeenCalledTimes(1)
345 expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
346 })
347 })
348​
349 it('should support required signal inputs', async () => {
350 const mutationCache = queryClient.getMutationCache()
351​
352 @Component({
353 selector: 'app-fake',
354 template: `
355 <button (click)="mutate()"></button>
356 <span>{{ mutation.data() }}</span>
357 `,
358 })
359 class FakeComponent {
360 name = input.required<string>()
361​
362 mutation = injectMutation(() => ({
363 mutationKey: ['fake', this.name()],
364 mutationFn: () => sleep(10).then(() => this.name()),
365 }))
366​
367 mutate(): void {
368 this.mutation.mutate()
369 }
370 }
371​
372 const fixture = TestBed.createComponent(FakeComponent)
373 const { debugElement } = fixture
374 setFixtureSignalInputs(fixture, { name: 'value' })
375​
376 const button = debugElement.query(By.css('button'))
377 button.triggerEventHandler('click')
378​
379 await vi.advanceTimersByTimeAsync(11)
380 fixture.detectChanges()
381​
382 const text = debugElement.query(By.css('span')).nativeElement.textContent
383 expect(text).toEqual('value')
384 const mutation = mutationCache.find({ mutationKey: ['fake', 'value'] })
385 expect(mutation?.options.mutationKey).toStrictEqual(['fake', 'value'])
386 })
387​
388 it('should update options on required signal input change', async () => {
389 const mutationCache = queryClient.getMutationCache()
390​
391 @Component({
392 selector: 'app-fake',
393 template: `
394 <button (click)="mutate()"></button>
395 <span>{{ mutation.data() }}</span>
396 `,
397 })
398 class FakeComponent {
399 name = input.required<string>()
400​
401 mutation = injectMutation(() => ({
402 mutationKey: ['fake', this.name()],
403 mutationFn: () => sleep(10).then(() => this.name()),
404 }))
405​
406 mutate(): void {
407 this.mutation.mutate()
408 }
409 }
410​
411 const fixture = TestBed.createComponent(FakeComponent)
412 const { debugElement } = fixture
413 setFixtureSignalInputs(fixture, { name: 'value' })
414​
415 const button = debugElement.query(By.css('button'))
416 const span = debugElement.query(By.css('span'))
417​
418 button.triggerEventHandler('click')
419 await vi.advanceTimersByTimeAsync(11)
420 fixture.detectChanges()
421​
422 expect(span.nativeElement.textContent).toEqual('value')
423​
424 setFixtureSignalInputs(fixture, { name: 'updatedValue' })
425​
426 button.triggerEventHandler('click')
427 await vi.advanceTimersByTimeAsync(11)
428 fixture.detectChanges()
429​
430 expect(span.nativeElement.textContent).toEqual('updatedValue')
431​
432 const mutations = mutationCache.findAll()
433 expect(mutations.length).toBe(2)
434 const [mutation1, mutation2] = mutations
435 expect(mutation1!.options.mutationKey).toEqual(['fake', 'value'])
436 expect(mutation2!.options.mutationKey).toEqual(['fake', 'updatedValue'])
437 })
438​
439 describe('throwOnError', () => {
440 it('should evaluate throwOnError when mutation is expected to throw', async () => {
441 const key = queryKey()
442 const err = new Error('Expected mock error. All is well!')
443 const boundaryFn = vi.fn()
444 const { mutate } = TestBed.runInInjectionContext(() => {
445 return injectMutation(() => ({
446 mutationKey: key,
447 mutationFn: () => {
448 return Promise.reject(err)
449 },
450 throwOnError: boundaryFn,
451 }))
452 })
453​
454 TestBed.tick()
455​
456 mutate()
457​
458 await vi.advanceTimersByTimeAsync(0)
459​
460 expect(boundaryFn).toHaveBeenCalledTimes(1)
461 expect(boundaryFn).toHaveBeenCalledWith(err)
462 })
463​
464 it('should throw when throwOnError is true and mutate is used', async () => {
465 const key = queryKey()
466 const { mutate } = TestBed.runInInjectionContext(() => {
467 return injectMutation(() => ({
468 mutationKey: key,
469 mutationFn: () => {
470 return Promise.reject(
471 new Error('Expected mock error. All is well!'),
472 )
473 },
474 throwOnError: true,
475 }))
476 })
477​
478 TestBed.tick()
479​
480 mutate()
481​
482 await expect(vi.advanceTimersByTimeAsync(0)).rejects.toThrow(
483 'Expected mock error. All is well!',
484 )
485 })
486 })
487​
488 it('should throw when throwOnError is true', async () => {
489 const key = queryKey()
490 const err = new Error('Expected mock error. All is well!')
491 const { mutateAsync } = TestBed.runInInjectionContext(() => {
492 return injectMutation(() => ({
493 mutationKey: key,
494 mutationFn: () => {
495 return Promise.reject(err)
496 },
497 throwOnError: true,
498 }))
499 })
500​
501 await expect(() => mutateAsync()).rejects.toThrow(err)
502 })
503​
504 it('should throw when throwOnError function returns true', async () => {
505 const key = queryKey()
506 const err = new Error('Expected mock error. All is well!')
507 const { mutateAsync } = TestBed.runInInjectionContext(() => {
508 return injectMutation(() => ({
509 mutationKey: key,
510 mutationFn: () => {
511 return Promise.reject(err)
512 },
513 throwOnError: () => true,
514 }))
515 })
516​
517 await expect(() => mutateAsync()).rejects.toThrow(err)
518 })
519​
520 it('should resolve mutateAsync with the value returned from mutationFn', async () => {
521 const key = queryKey()
522 const { mutateAsync } = TestBed.runInInjectionContext(() => {
523 return injectMutation(() => ({
524 mutationKey: key,
525 mutationFn: (params: string) => sleep(10).then(() => params),
526 }))
527 })
528​
529 const promise = mutateAsync('Mock data')
530 await vi.advanceTimersByTimeAsync(11)
531​
532 await expect(promise).resolves.toBe('Mock data')
533 })
534​
535 describe('injection context', () => {
536 it('should throw NG0203 with descriptive error outside injection context', () => {
537 const key = queryKey()
538 expect(() => {
539 injectMutation(() => ({
540 mutationKey: key,
541 mutationFn: () => Promise.resolve(),
542 }))
543 }).toThrow(/NG0203(.*?)injectMutation/)
544 })
545​
546 it('should be usable outside injection context when passing an injector', () => {
547 const key = queryKey()
548 expect(() => {
549 injectMutation(
550 () => ({
551 mutationKey: key,
552 mutationFn: () => Promise.resolve(),
553 }),
554 {
555 injector: TestBed.inject(Injector),
556 },
557 )
558 }).not.toThrow()
559 })
560​
561 it('should complete mutation before whenStable() resolves', async () => {
562 const app = TestBed.inject(ApplicationRef)
563 let mutationStarted = false
564 let mutationCompleted = false
565​
566 const key = queryKey()
567 const mutation = TestBed.runInInjectionContext(() =>
568 injectMutation(() => ({
569 mutationKey: key,
570 mutationFn: async (data: string) => {
571 mutationStarted = true
572 await sleep(50)
573 mutationCompleted = true
574 return `processed: ${data}`
575 },
576 })),
577 )
578​
579 // Initial state
580 expect(mutation.data()).toBeUndefined()
581 expect(mutationStarted).toBe(false)
582​
583 // Start mutation
584 mutation.mutate('test')
585​
586 // Wait for mutation to start and Angular to be "stable"
587 const stablePromise = app.whenStable()
588 await vi.advanceTimersByTimeAsync(60)
589 await stablePromise
590​
591 // After whenStable(), mutation should be complete
592 expect(mutationStarted).toBe(true)
593 expect(mutationCompleted).toBe(true)
594 expect(mutation.isSuccess()).toBe(true)
595 expect(mutation.data()).toBe('processed: test')
596 })
597​
598 it('should handle synchronous mutation with retry', async () => {
599 const app = TestBed.inject(ApplicationRef)
600 let attemptCount = 0
601​
602 const mutation = TestBed.runInInjectionContext(() =>
603 injectMutation(() => ({
604 retry: 2,
605 retryDelay: 0, // No delay for synchronous retry
606 mutationFn: async (data: string) => {
607 attemptCount++
608 if (attemptCount <= 2) {
609 throw new Error(`Sync attempt ${attemptCount} failed`)
610 }
611 return `processed: ${data}`
612 },
613 })),
614 )
615​
616 // Start mutation
617 mutation.mutate('retry-test')
618​
619 // Synchronize pending effects for each retry attempt
620 TestBed.tick()
621 await Promise.resolve()
622 await vi.advanceTimersByTimeAsync(10)
623​
624 TestBed.tick()
625 await Promise.resolve()
626 await vi.advanceTimersByTimeAsync(10)
627​
628 TestBed.tick()
629​
630 const stablePromise = app.whenStable()
631 await Promise.resolve()
632 await vi.advanceTimersByTimeAsync(10)
633 await stablePromise
634​
635 expect(mutation.isSuccess()).toBe(true)
636 expect(mutation.data()).toBe('processed: retry-test')
637 expect(attemptCount).toBe(3) // Initial + 2 retries
638 })
639​
640 it('should handle multiple synchronous mutations on same key', async () => {
641 const app = TestBed.inject(ApplicationRef)
642 let callCount = 0
643​
644 const key = queryKey()
645​
646 const mutation1 = TestBed.runInInjectionContext(() =>
647 injectMutation(() => ({
648 mutationKey: key,
649 mutationFn: async (data: string) => {
650 callCount++
651 return `mutation1: ${data}`
652 },
653 })),
654 )
655​
656 const mutation2 = TestBed.runInInjectionContext(() =>
657 injectMutation(() => ({
658 mutationKey: key,
659 mutationFn: async (data: string) => {
660 callCount++
661 return `mutation2: ${data}`
662 },
663 })),
664 )
665​
666 // Start both mutations
667 mutation1.mutate('test1')
668 mutation2.mutate('test2')
669​
670 // Synchronize pending effects
671 TestBed.tick()
672​
673 const stablePromise = app.whenStable()
674 // Flush microtasks to allow TanStack Query's scheduled notifications to process
675 await Promise.resolve()
676 await vi.advanceTimersByTimeAsync(1)
677 await stablePromise
678​
679 expect(mutation1.isSuccess()).toBe(true)
680 expect(mutation1.data()).toBe('mutation1: test1')
681 expect(mutation2.isSuccess()).toBe(true)
682 expect(mutation2.data()).toBe('mutation2: test2')
683 expect(callCount).toBe(2)
684 })
685​
686 it('should handle synchronous mutation with optimistic updates', async () => {
687 const app = TestBed.inject(ApplicationRef)
688 const testQueryKey = queryKey()
689 let onMutateCalled = false
690 let onSuccessCalled = false
691​
692 // Set initial data
693 queryClient.setQueryData(testQueryKey, 'initial')
694​
695 const mutation = TestBed.runInInjectionContext(() =>
696 injectMutation(() => ({
697 mutationFn: async (data: string) => `final: ${data}`, // Synchronous resolution
698 onMutate: async (variables) => {
699 onMutateCalled = true
700 const previousData = queryClient.getQueryData(testQueryKey)
701 queryClient.setQueryData(testQueryKey, `optimistic: ${variables}`)
702 return { previousData }
703 },
704 onSuccess: (data) => {
705 onSuccessCalled = true
706 queryClient.setQueryData(testQueryKey, data)
707 },
708 })),
709 )
710​
711 // Start mutation
712 mutation.mutate('test')
713​
714 // Synchronize pending effects
715 TestBed.tick()
716​
717 const stablePromise = app.whenStable()
718 // Flush microtasks to allow TanStack Query's scheduled notifications to process
719 await Promise.resolve()
720 await vi.advanceTimersByTimeAsync(1)
721 await stablePromise
722​
723 expect(onMutateCalled).toBe(true)
724 expect(onSuccessCalled).toBe(true)
725 expect(mutation.isSuccess()).toBe(true)
726 expect(mutation.data()).toBe('final: test')
727 expect(queryClient.getQueryData(testQueryKey)).toBe('final: test')
728 })
729​
730 it('should handle synchronous mutation cancellation', async () => {
731 const app = TestBed.inject(ApplicationRef)
732​
733 const key = queryKey()
734 const mutation = TestBed.runInInjectionContext(() =>
735 injectMutation(() => ({
736 mutationKey: key,
737 mutationFn: async (data: string) => `processed: ${data}`, // Synchronous resolution
738 })),
739 )
740​
741 // Start mutation
742 mutation.mutate('test')
743​
744 // Synchronize pending effects
745 TestBed.tick()
746​
747 const stablePromise = app.whenStable()
748 // Flush microtasks to allow TanStack Query's scheduled notifications to process
749 await Promise.resolve()
750 await vi.advanceTimersByTimeAsync(1)
751 await stablePromise
752​
753 // Synchronous mutations complete immediately
754 expect(mutation.isSuccess()).toBe(true)
755 expect(mutation.data()).toBe('processed: test')
756 })
757 })
758})