@@ -12,6 +12,7 @@ import { hyphenate } from './utils/vueShared'
12
12
import { MOUNT_COMPONENT_REF , MOUNT_PARENT_NAME } from './constants'
13
13
import { config } from './config'
14
14
import { matchName } from './utils/matchName'
15
+ import { ComponentInternalInstance } from '@vue/runtime-core'
15
16
16
17
interface StubOptions {
17
18
name ?: string
@@ -74,18 +75,7 @@ export function stubComponents(
74
75
stubs : Record < any , any > = { } ,
75
76
shallow : boolean = false
76
77
) {
77
- transformVNodeArgs ( ( args ) => {
78
- const locallyRegisteredComponents = ( args [ 0 ] as any ) . components as
79
- | Record < string , VNodeTypes >
80
- | undefined
81
- if ( locallyRegisteredComponents ) {
82
- for ( const registrationName in locallyRegisteredComponents ) {
83
- const component = locallyRegisteredComponents [ registrationName ]
84
- if ( ! component [ 'name' ] && ! component [ 'displayName' ] ) {
85
- component [ 'name' ] = registrationName
86
- }
87
- }
88
- }
78
+ transformVNodeArgs ( ( args , instance : ComponentInternalInstance | null ) => {
89
79
const [ nodeType , props , children , patchFlag , dynamicProps ] = args
90
80
const type = nodeType as VNodeTypes
91
81
// args[0] can either be:
@@ -102,7 +92,19 @@ export function stubComponents(
102
92
}
103
93
104
94
if ( isComponent ( type ) || isFunctionalComponent ( type ) ) {
105
- const name = type [ 'name' ] || type [ 'displayName' ]
95
+ let name = type [ 'name' ] || type [ 'displayName' ]
96
+
97
+ // if no name, then check the locally registered components in the parent
98
+ if ( ! name && instance && instance . parent ) {
99
+ // try to infer the name based on local resolution
100
+ const registry = ( instance . type as any ) . components
101
+ for ( const key in registry ) {
102
+ if ( registry [ key ] === type ) {
103
+ name = key
104
+ break
105
+ }
106
+ }
107
+ }
106
108
if ( ! name && ! shallow ) {
107
109
return args
108
110
}
0 commit comments