Skip to content

Commit b6fd093

Browse files
authored
test(vue-query/useQuery): add test for outside scope warning in development mode (#10223)
* test(vue-query/useQuery): add test for outside scope warning in development mode * refactor(vue-query/useQuery): wrap outside scope warning test with 'try/finally' for cleanup safety
1 parent 2d3f5b3 commit b6fd093

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,27 @@ describe('useQuery', () => {
460460
})
461461
})
462462

463+
describe('outside scope warning', () => {
464+
test('should warn when used outside of setup function in development mode', () => {
465+
vi.stubEnv('NODE_ENV', 'development')
466+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
467+
468+
try {
469+
useQuery({
470+
queryKey: ['outsideScope'],
471+
queryFn: () => sleep(0).then(() => 'data'),
472+
})
473+
474+
expect(warnSpy).toHaveBeenCalledWith(
475+
'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.',
476+
)
477+
} finally {
478+
warnSpy.mockRestore()
479+
vi.unstubAllEnvs()
480+
}
481+
})
482+
})
483+
463484
describe('suspense', () => {
464485
test('should return a Promise', () => {
465486
const getCurrentInstanceSpy = getCurrentInstance as Mock

0 commit comments

Comments
 (0)