Skip to content

fix: prefer the searched component over one sharing its name in findComponent - #3080

Merged
cexbrayat merged 2 commits into
vuejs:mainfrom
ValentinYoushkevich:fix/find-component-same-name
Dec 4, 2026
Merged

cexbrayat merged 2 commits into
vuejs:mainfrom
ValentinYoushkevich:fix/find-component-same-name

Conversation

@ValentinYoushkevich

Copy link
Copy Markdown
Contributor

Fixes #3074

As discussed in #3074, this also covers the deeper case left out of #3077.

When a component renders another one with the same name (like a Button.vue with <script setup> wrapping a library Button), findComponent(LibButton) returns the wrapper instead of LibButton. On the mounted component this comes from #3063, deeper in the tree it already happens in 2.6.0: playground.

findComponent now prefers a component rendered from the searched definition (or its stub) over one that only matches by name. That check moves out of matches into a matchesDefinition helper. findAllComponents is unchanged, so when its first result only shares the name, findComponent now returns a later one.

@netlify

netlify Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

👷 Deploy request for vue-test-utils-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 12b69a0

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 12b69a0

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

Could we rank the matching component instances before creating wrappers? If I understand correctly,findAllComponents() creates wrappers for every candidate, which runs wrapper plugins and installs native event listeners even for components that findComponent() won’t return no?

@ValentinYoushkevich

Copy link
Copy Markdown
Contributor Author

Good point, updated: findComponent() now ranks the matching instances first and only wraps the one it returns. The DOMWrapper scope check moved from its findAllComponents() override to a small ownsComponent() hook, so it can run on a single candidate; from a DOMWrapper, candidates rendered outside its element are still wrapped to check where they render, as before.

This also applies to the cases that don't involve a same-named component: findComponent() used findAllComponents()[0] before, so every match was wrapped. I added a test with a VueWrapper plugin that checks only the returned component gets wrapped.

@cexbrayat
cexbrayat merged commit f72bbaf into vuejs:main Dec 4, 2026
6 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: findComponent returns the mounted component instead of a same-named component it renders

2 participants