Skip to content

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

Closed

Description

@ebiryu

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

https://stackblitz.com/edit/github-goth86bt-gh1t8tyt?file=src%2Fcomponents%2F__tests__%2Femit-leak.spec.ts

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 (in src/vueWrapper.ts) runs in the constructor of each VueWrapper, including child wrappers from findComponent, and records the raw DOM Event via recordEvent(vm.$, eventName, args) under the child's uid, which is not cleared.
  • Related inconsistency: for functional components recordEvent records under parent.uid (it walks up while type is a function), while removeEventHistory uses vm.$.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 events by the component instance in a WeakMap so entries are released when the instance is collected.
  • Context: removeEventHistory was introduced in Fix/emitted events cache #1449 to clear emit history on unmount, but only for the root uid. A separate unmount leak in src/vueWrapper.ts was 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"; the events map in src/emit.ts was not part of that change.

Activity

  1. added
    bug
    on Jul 5, 2026
  2. added theissue type on Jul 5, 2026
  3. added a commit that references this issue on Jul 31, 2026
    0b8653f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions