From 16f0f1635031a4a2e82d583b4ac614f8d47ba3f0 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Thu, 5 Mar 2026 14:08:32 +0900 Subject: [PATCH 1/2] test(vue-query/useMutation): add test for outside scope warning in development mode --- .../vue-query/src/__tests__/useMutation.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/vue-query/src/__tests__/useMutation.test.ts b/packages/vue-query/src/__tests__/useMutation.test.ts index 88991b58dc1..e2888ec00ca 100644 --- a/packages/vue-query/src/__tests__/useMutation.test.ts +++ b/packages/vue-query/src/__tests__/useMutation.test.ts @@ -386,6 +386,21 @@ describe('useMutation', () => { }) }) + test('should warn when used outside of setup function in development mode', () => { + vi.stubEnv('NODE_ENV', 'development') + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) + + useMutation({ + mutationFn: (params: string) => sleep(0).then(() => params), + }) + + vi.unstubAllEnvs() + + expect(warnSpy).toHaveBeenCalledWith( + 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.', + ) + }) + describe('throwOnError', () => { test('should evaluate throwOnError when mutation is expected to throw', async () => { const err = new Error('Expected mock error. All is well!') From 9621beab63a6f2abe6f4ae25ee4935d404426cc0 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Thu, 5 Mar 2026 14:20:15 +0900 Subject: [PATCH 2/2] refactor(vue-query/useMutation): wrap outside scope warning test with 'try/finally' for cleanup safety --- .../src/__tests__/useMutation.test.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/vue-query/src/__tests__/useMutation.test.ts b/packages/vue-query/src/__tests__/useMutation.test.ts index e2888ec00ca..cd135da7147 100644 --- a/packages/vue-query/src/__tests__/useMutation.test.ts +++ b/packages/vue-query/src/__tests__/useMutation.test.ts @@ -390,15 +390,18 @@ describe('useMutation', () => { vi.stubEnv('NODE_ENV', 'development') const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) - useMutation({ - mutationFn: (params: string) => sleep(0).then(() => params), - }) - - vi.unstubAllEnvs() + try { + useMutation({ + mutationFn: (params: string) => sleep(0).then(() => params), + }) - expect(warnSpy).toHaveBeenCalledWith( - 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.', - ) + expect(warnSpy).toHaveBeenCalledWith( + 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.', + ) + } finally { + warnSpy.mockRestore() + vi.unstubAllEnvs() + } }) describe('throwOnError', () => {