From 99f2a2f784ab37379ac1f5270307540b6aea8831 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Thu, 5 Mar 2026 14:23:37 +0900 Subject: [PATCH] test(vue-query/useQueries): add test for outside scope warning in development mode --- .../src/__tests__/useQueries.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/vue-query/src/__tests__/useQueries.test.ts b/packages/vue-query/src/__tests__/useQueries.test.ts index 3dca555395a..68cfe9e74c6 100644 --- a/packages/vue-query/src/__tests__/useQueries.test.ts +++ b/packages/vue-query/src/__tests__/useQueries.test.ts @@ -369,6 +369,29 @@ describe('useQueries', () => { expect(fetchFn).toHaveBeenCalledTimes(6) }) + test('should warn when used outside of setup function in development mode', () => { + vi.stubEnv('NODE_ENV', 'development') + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) + + try { + useQueries({ + queries: [ + { + queryKey: ['outsideScope'], + queryFn: () => sleep(0).then(() => 'data'), + }, + ], + }) + + 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() + } + }) + test('should work with options getter and be reactive', async () => { const fetchFn = vi.fn(() => 'foo') const key1 = ref('key1')