diff --git a/src/apis/effectScope.ts b/src/apis/effectScope.ts index e2aac9d8..657276f0 100644 --- a/src/apis/effectScope.ts +++ b/src/apis/effectScope.ts @@ -102,7 +102,7 @@ export function onScopeDispose(fn: () => void) { activeEffectScope.cleanups.push(fn) } else if (__DEV__) { warn( - `onDispose() is called when there is no active effect scope ` + + `onDispose() is called when there is no active effect scope` + ` to be associated with.` ) } diff --git a/test/v3/reactivity/effectScope.spec.ts b/test/v3/reactivity/effectScope.spec.ts index 77d0582d..ec5a5e8e 100644 --- a/test/v3/reactivity/effectScope.spec.ts +++ b/test/v3/reactivity/effectScope.spec.ts @@ -209,6 +209,25 @@ describe('reactivity/effect/scope', () => { expect(dummy).toBe(7) }) + it('should warn onDispose() is called when there is no active effect scope', () => { + const spy = jest.fn() + const scope = new EffectScope() + scope.run(() => { + onScopeDispose(spy) + }) + + expect(spy).toHaveBeenCalledTimes(0) + + onScopeDispose(spy) + + expect( + '[Vue warn]: onDispose() is called when there is no active effect scope to be associated with.' + ).toHaveBeenWarned() + + scope.stop() + expect(spy).toHaveBeenCalledTimes(1) + }) + it('test with higher level APIs', async () => { const r = ref(1)