Repository navigation
Description
Describe the bug
@vue/test-utils records component emit calls into a module-level events object in src/emit.ts, keyed by each component's uid:
// src/emit.ts
const events: Events = {}
export const recordEvent = (vm, event, args) => {
// ...
events[cid][event].push(args) // cid = emitting component's uid
}
export const removeEventHistory = (vm) => {
const cid = vm.$.uid
delete events[cid] // only the root wrapper's uid
}recordEvent runs for any component that emits. The devtools component:emit hook is global (attachEmitListener overrides __VUE_DEVTOOLS_GLOBAL_HOOK__.emit), and native DOM events attached by each VueWrapper also call recordEvent(vm.$, ...). On wrapper.unmount(), however, removeEventHistory(this.vm) (in src/vueWrapper.ts) deletes only the root component's uid.
So entries recorded under a child component's uid stay in events after unmount. Vue uids are monotonically increasing, so these entries are not reused or overwritten by later mounts. When a recorded payload is a native DOM Event, it holds a reference to event.target, which keeps the associated DOM nodes (and the handlers and reactive values they reference) reachable. With Vitest isolate: false (--no-isolate), events is shared across test files, so uncleared entries carry over between files.
The root cause is the asymmetry: removeEventHistory deletes one uid (the root), while recordEvent writes under the uid of every emitting component, including descendants.
Reproduction
Expected behavior
After wrapper.unmount(), the recorded emit history for the whole component tree (root and descendants, including native DOM Event payloads) should be removed from events. In the reproduction, the child case should pass like the root control.
Environment
System:
OS: macOS 26.5.1
CPU: (8) arm64 Apple M1
Memory: 87.31 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
npmPackages:
vitest: 4.1.9 => 4.1.9
vue: 3.5.38 => 3.5.38
Additional context
- Native DOM events go through the same path:
attachNativeEventListener(insrc/vueWrapper.ts) runs in the constructor of eachVueWrapper, including child wrappers fromfindComponent, and records the raw DOMEventviarecordEvent(vm.$, eventName, args)under the child's uid, which is not cleared. - Related inconsistency: for functional components
recordEventrecords underparent.uid(it walks up whiletypeis a function), whileremoveEventHistoryusesvm.$.uid, so the keys can differ there as well. - Possible fixes: on unmount, walk the component tree and delete each descendant uid (or group entries per app and drop them together); or key
eventsby the component instance in aWeakMapso entries are released when the instance is collected. - Context:
removeEventHistorywas introduced in Fix/emitted events cache #1449 to clear emit history on unmount, but only for the root uid. A separate unmount leak insrc/vueWrapper.tswas addressed in fix: cleanup event listeners on unmount #1793 (for Bug: Possible memory leak when using real Vuex store in plugins #1787), which noted "there might be some more to optimize"; theeventsmap insrc/emit.tswas not part of that change.
Activity
fix: clear emitted event history for child components on unmount (vue…