Skip to content

fix: clear emitted event history for child components on unmount - #2898

Merged
cexbrayat merged 1 commit into
vuejs:mainfrom
ebiryu:fix/emit-history-child-unmount
Jul 6, 2026
Merged

cexbrayat merged 1 commit into
vuejs:mainfrom
ebiryu:fix/emit-history-child-unmount

Conversation

@ebiryu

@ebiryu ebiryu commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

removeEventHistory cleared only the root component's entry from the module-level events map in src/emit.ts, so emit histories recorded under descendant components' uids were never removed on wrapper.unmount().

recordEvent writes under the uid of every component that emits: the devtools component:emit hook is global, and attachNativeEventListener records native DOM events against each wrapper (including child wrappers from findComponent). Native DOM Event payloads keep event.target and its DOM subtree reachable. With Vitest isolate: false the events map 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 / nodesAsObject are duplicated from src/utils/find.ts to avoid coupling this path to the selector code; happy to extract a shared util if preferred.
  • Components unmounted earlier in a test (e.g. via v-if) are not reachable from the tree at root-unmount time. A WeakMap-keyed store would also cover that and could be a follow-up.

@netlify

netlify Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit afca8bc
🔍 Latest deploy log https://app.netlify.com/projects/vue-test-utils-docs/deploys/6a4b93c54f21190008c22935
😎 Deploy Preview https://deploy-preview-2898--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cexbrayat cexbrayat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@ebiryu
ebiryu force-pushed the fix/emit-history-child-unmount branch from e098ba8 to afca8bc Compare July 6, 2026 11:38
@ebiryu

ebiryu commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

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).

@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@vue/test-utils@2898

commit: afca8bc

@cexbrayat cexbrayat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ebiryu

@cexbrayat
cexbrayat merged commit 0b8653f into vuejs:main Jul 6, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: emitted event history for child components is not cleared on unmount

2 participants