Repository navigation
Description
Describe the bug
Hi,
VueWrapper.unmount() clears the emitted events history before the app is unmounted :
Lines 316 to 330 in 9736ced
| unmount() { | |
| // preventing dispose of child component | |
| if (!this.__app) { | |
| throw new Error( | |
| `wrapper.unmount() can only be called by the root wrapper` | |
| ) | |
| } | |
| // Clear emitted events cache for this component instance | |
| removeEventHistory(this.vm) | |
| this.cleanUpCallbacks.forEach(cb => cb()) | |
| this.cleanUpCallbacks = [] | |
| this.__app.unmount() | |
| } |
removeEventHistory(this.vm) // clear history
[...]
this.__app.unmount() // unmount, emit recorder is still active
The recording hook stays, so any emit that happens while the app is being unmounted passes through recordEvent, recreating events[uid] and recordedUidsByRoot[rootUid] after they were deleted :
Lines 66 to 103 in 9736ced
| export const recordEvent = ( | |
| vm: ComponentInternalInstance, | |
| event: string, | |
| args: unknown[] | |
| ): void => { | |
| // Functional component wrapper creates a parent component | |
| let wrapperVm = vm | |
| while (typeof wrapperVm?.type === 'function') wrapperVm = wrapperVm.parent! | |
| const cid = wrapperVm.uid | |
| const rootCid = wrapperVm.root.uid | |
| if (!recordedUidsByRoot.has(rootCid)) { | |
| recordedUidsByRoot.set(rootCid, new Set()) | |
| } | |
| recordedUidsByRoot.get(rootCid)!.add(cid) | |
| if (!(cid in events)) { | |
| events[cid] = {} | |
| } | |
| if (!(event in events[cid])) { | |
| events[cid][event] = [] | |
| } | |
| // Record the event message sent by the emit | |
| events[cid][event].push(args) | |
| } | |
| export const removeEventHistory = (vm: ComponentPublicInstance): void => { | |
| const rootCid = vm.$.root.uid | |
| const uids = recordedUidsByRoot.get(rootCid) | |
| if (!uids) return | |
| for (const uid of uids) { | |
| delete events[uid] | |
| } | |
| recordedUidsByRoot.delete(rootCid) | |
| } |
Nothing clears them.
If the payload is DOM event, event.target is kept.
The detached DOM tree, the component instance, and the app, all stay reachable for as long a @vue/test-utils module survives.
By default, it's the whole test file, and with isolate: false (our case), it's the whole worker.
Reka UI handles such case with FocusScope, that emits unmountAutoFocus, plus a CustomEvent from a watcher cleanup. Components such as Dialog, Popover, Menu, Select and Drawer emit it again as closeAutoFocus :
https://github.com/unovue/reka-ui/blob/78fb7748aa4f87cdaad60d0282002ab685d6bf58/packages/core/src/FocusScope/FocusScope.vue#L250-L256
I have checked this issue as well : #2897
This new one makes sure every recorded uid is cleared on unmount, and that the clearing runs before the unmount emits happen.
Disclaimer : This is my first time contributing to a project.
I have used agentic tools to find the root cause, as I was having terrible CPU usage when running tests. Values were measured on my machine after a pnpm patch.
Reproduction
Expected behavior
Actual on 2.5.1 :
AssertionError: expected 50 to be +0
AssertionError: expected [ [ CustomEvent{ isTrusted: false } ] ] to be undefined
The same component that emits onMounted is released (0 of 50 kept).
After wrapper.unmount(), no emit history is kept for the unmounted tree, including events emitted by the unmount.
Current existing assertion for events before unmount :
Lines 366 to 388 in 9736ced
| it('clears emitted event history of child components on unmount', async () => { | |
| const Child = defineComponent({ | |
| name: 'Child', | |
| emits: ['foo'], | |
| setup(_, { emit }) { | |
| emit('foo', 'bar') | |
| return () => h('div', 'child') | |
| } | |
| }) | |
| const Parent = defineComponent({ | |
| name: 'Parent', | |
| render: () => h(Child) | |
| }) | |
| const wrapper = mount(Parent) | |
| const child = wrapper.findComponent(Child) | |
| expect(child.emitted('foo')).toHaveLength(1) | |
| wrapper.unmount() | |
| // the child's history must be removed too, not only the root's | |
| expect(child.emitted('foo')).toBeUndefined() | |
| }) |
Environment
MacOS 27.0.1, Apple M4
Node: 22.23.4
npmPackages:
@vue/test-utils: 2.5.1 => 2.5.1
jsdom: 30.1.3 => 30.1.3
vitest: 5.0.5 => 5.0.5
vue: 3.5.44 => 3.5.44
Additional context
Applying the fix locally through a pnpm patch reduced the heap at the end of the run from 3.3GB to 1.20GB (tried twice).
Only side effect was a test reading wrapper.emitted() after an unmount() now gets undefined, which was mitigated with { onLeave: spy }.
clear the history after the app is unmounted :
unmount() {
...
- // Clear emitted events cache for this component instance
- removeEventHistory(this.vm)
-
this.cleanUpCallbacks.forEach(cb => cb())
this.cleanUpCallbacks = []
this.__app.unmount()
+
+ // after unmounting, so that events emitted by unmount hooks are cleared too
+ removeEventHistory(this.vm)
}
Thank you.
Activity
cexbrayat commentedon Oct 26, 2026
Hi @Naaath
Thank you for detailed issue 👍 @ondrhn already opened a PR, so this should be fixed in the next release!
Naaath commentedon Oct 26, 2026
Hi @cexbrayat
Thank you so much, that is great to hear !
Can't wait for the next release, appreciate it a ton
Thanks for your work !