Skip to content

Commit eca0da2

Browse files
authored
test(vue-query/useMutation): add test for outside scope warning in development mode (#10238)
* test(vue-query/useMutation): add test for outside scope warning in development mode * refactor(vue-query/useMutation): wrap outside scope warning test with 'try/finally' for cleanup safety
1 parent 38cd749 commit eca0da2

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@ describe('useMutation', () => {
386386
})
387387
})
388388

389+
test('should warn when used outside of setup function in development mode', () => {
390+
vi.stubEnv('NODE_ENV', 'development')
391+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
392+
393+
try {
394+
useMutation({
395+
mutationFn: (params: string) => sleep(0).then(() => params),
396+
})
397+
398+
expect(warnSpy).toHaveBeenCalledWith(
399+
'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.',
400+
)
401+
} finally {
402+
warnSpy.mockRestore()
403+
vi.unstubAllEnvs()
404+
}
405+
})
406+
389407
describe('throwOnError', () => {
390408
test('should evaluate throwOnError when mutation is expected to throw', async () => {
391409
const err = new Error('Expected mock error. All is well!')

0 commit comments

Comments
 (0)