diff --git a/src/utils/componentName.ts b/src/utils/componentName.ts index 77535f0a3..b00cc98ed 100644 --- a/src/utils/componentName.ts +++ b/src/utils/componentName.ts @@ -28,6 +28,14 @@ export const getComponentRegisteredName = ( } } + // try to infer the name based on global resolution + const globalRegistry = instance.appContext.components + for (const key in globalRegistry) { + if (globalRegistry[key] === type) { + return key + } + } + // try to retrieve name imported in script setup return getComponentNameInSetup(instance.parent, type) || null } diff --git a/tests/mountingOptions/global.stubs.spec.ts b/tests/mountingOptions/global.stubs.spec.ts index cf9b57a5d..cad7780e5 100644 --- a/tests/mountingOptions/global.stubs.spec.ts +++ b/tests/mountingOptions/global.stubs.spec.ts @@ -367,6 +367,36 @@ describe('mounting options: stubs', () => { expect(wrapper.html()).toBe('') }) + it('stubs a globally registered component by its registration name', () => { + const LibraryComponent = defineComponent({ + name: 'LibraryInternalName', + template: 'real library component' + }) + + const Plugin = { + install(app: any) { + app.component('GlobalAlias', LibraryComponent) + } + } + + const Component = defineComponent({ + template: '
' + }) + + const wrapper = mount(Component, { + global: { + plugins: [Plugin], + stubs: { + GlobalAlias: true + } + } + }) + + expect(wrapper.html()).toBe( + '
\n' + ' \n' + '
' + ) + }) + it('stubs components within script setup', () => { const wrapper = mount(ScriptSetupWithChildren as any, { global: {