Repository navigation
fix: clear emitted event history for child components on unmount - #2898
Conversation
✅ Deploy Preview for vue-test-utils-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
cexbrayat
left a comment
There was a problem hiding this comment.
Thanks for the fix!
I think the tree-walk approach still misses one case: a child component that emits, then gets removed before the root wrapper is unmounted, for example via v-if.
At wrapper.unmount() time, that child is no longer reachable from root.subTree, so its uid is not collected and its entry remains in the module-level events map.
A possible fix is maybe to track recorded uids in a Set while events are recorded, scoped by root component:
const events: Events = {}
const recordedUidsByRoot = new Map<number, Set<number>>()Then in recordEvent, after resolving wrapperVm:
const cid = wrapperVm.uid
const rootCid = wrapperVm.root.uid
if (!recordedUidsByRoot.has(rootCid)) {
recordedUidsByRoot.set(rootCid, new Set())
}
recordedUidsByRoot.get(rootCid)!.add(cid)and then use this set in the cleanup, without having to collect uids?
This is pseudo cod, maybe this does not work, but can you give it a try?
removeEventHistory only deleted the root component's uid from the module-level events map, so histories recorded for descendant components (via the global devtools component:emit hook and native DOM event listeners on each wrapper) were never removed. With Vitest isolate:false these entries, including native DOM Event payloads that retain DOM nodes, accumulated across test files. Track the uids that record events under each root component, and clear that whole set on unmount. This also covers components removed before the root (e.g. via v-if) that are no longer reachable from the root's render tree.
e098ba8 to
afca8bc
Compare
|
Thanks @cexbrayat. I switched to the approach you suggested and it works well. Also added a test for the case you mentioned (a child removed before the root unmounts). |
commit: |
Summary
removeEventHistorycleared only the root component's entry from the module-leveleventsmap insrc/emit.ts, so emit histories recorded under descendant components' uids were never removed onwrapper.unmount().recordEventwrites under the uid of every component that emits: the devtoolscomponent:emithook is global, andattachNativeEventListenerrecords native DOM events against each wrapper (including child wrappers fromfindComponent). Native DOMEventpayloads keepevent.targetand its DOM subtree reachable. With Vitestisolate: falsetheeventsmap is shared across files, so these entries accumulate.This walks the component tree on unmount and removes every component instance's entry.
Fixes #2897
Notes
aggregateChildren/nodesAsObjectare duplicated fromsrc/utils/find.tsto avoid coupling this path to the selector code; happy to extract a shared util if preferred.v-if) are not reachable from the tree at root-unmount time. AWeakMap-keyed store would also cover that and could be a follow-up.