Skip to content

Commit 50eecb2

Browse files
authored
test(vue-query/useQueries): add test for outside scope warning in development mode (#10240)
1 parent ef2eb66 commit 50eecb2

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

packages/vue-query/src/__tests__/useQueries.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,29 @@ describe('useQueries', () => {
369369
expect(fetchFn).toHaveBeenCalledTimes(6)
370370
})
371371

372+
test('should warn when used outside of setup function in development mode', () => {
373+
vi.stubEnv('NODE_ENV', 'development')
374+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
375+
376+
try {
377+
useQueries({
378+
queries: [
379+
{
380+
queryKey: ['outsideScope'],
381+
queryFn: () => sleep(0).then(() => 'data'),
382+
},
383+
],
384+
})
385+
386+
expect(warnSpy).toHaveBeenCalledWith(
387+
'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.',
388+
)
389+
} finally {
390+
warnSpy.mockRestore()
391+
vi.unstubAllEnvs()
392+
}
393+
})
394+
372395
test('should work with options getter and be reactive', async () => {
373396
const fetchFn = vi.fn(() => 'foo')
374397
const key1 = ref('key1')

0 commit comments

Comments
 (0)