Skip to content

Commit 2648213

Browse files
authored
fix: stop extending from constructor functions (#1014)
1 parent 4bd1837 commit 2648213

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed

Diff for: packages/create-instance/create-instance.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export default function createInstance (
8484
if (vueVersion > 2.2) {
8585
for (const c in _Vue.options.components) {
8686
if (!isRequiredComponent(c)) {
87-
const extendedComponent = _Vue.extend(_Vue.options.components[c])
88-
extendedComponent.options.$_vueTestUtils_original =
89-
_Vue.options.components[c]
90-
_Vue.component(c, _Vue.extend(_Vue.options.components[c]))
87+
const comp = _Vue.options.components[c]
88+
const options = comp.options ? comp.options : comp
89+
const extendedComponent = _Vue.extend(options)
90+
extendedComponent.options.$_vueTestUtils_original = comp
91+
_Vue.component(c, extendedComponent)
9192
}
9293
}
9394
}
@@ -105,13 +106,10 @@ export default function createInstance (
105106

106107
// extend component from _Vue to add properties and mixins
107108
// extend does not work correctly for sub class components in Vue < 2.2
108-
const Constructor = typeof component === 'function' && vueVersion < 2.3
109-
? component.extend(instanceOptions)
109+
const Constructor = typeof component === 'function'
110+
? _Vue.extend(component.options).extend(instanceOptions)
110111
: _Vue.extend(component).extend(instanceOptions)
111112

112-
// Keep reference to component mount was called with
113-
Constructor._vueTestUtilsRoot = component
114-
115113
// used to identify extended component using constructor
116114
Constructor.options.$_vueTestUtils_original = component
117115
if (options.slots) {

Diff for: packages/create-instance/extend-extended-components.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import { warn } from 'shared/util'
22
import { addHook } from './add-hook'
33

4-
function createdFrom (extendOptions, componentOptions) {
5-
while (extendOptions) {
6-
if (extendOptions === componentOptions) {
7-
return true
8-
}
9-
if (extendOptions._vueTestUtilsRoot === componentOptions) {
10-
return true
11-
}
12-
extendOptions = extendOptions.extendOptions
13-
}
14-
}
15-
164
function resolveComponents (options = {}, components = {}) {
175
let extendOptions = options.extendOptions
186
while (extendOptions) {
@@ -76,8 +64,7 @@ export function extendExtendedComponents (
7664
`config.logModifiedComponents option to false.`
7765
)
7866
}
79-
80-
const extendedComp = _Vue.extend(comp)
67+
const extendedComp = _Vue.extend(comp.options)
8168
// Used to identify component in a render tree
8269
extendedComp.options.$_vueTestUtils_original = comp
8370
extendedComponents[c] = extendedComp
@@ -94,7 +81,10 @@ export function extendExtendedComponents (
9481
})
9582
if (Object.keys(extendedComponents).length > 0) {
9683
addHook(_Vue.options, 'beforeCreate', function addExtendedOverwrites () {
97-
if (createdFrom(this.constructor, component)) {
84+
if (
85+
this.constructor.extendOptions === component ||
86+
this.$options.$_vueTestUtils_original === component
87+
) {
9888
Object.assign(
9989
this.$options.components,
10090
extendedComponents

Diff for: test/specs/mounting-options/localVue.spec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
6666
itSkipIf(
6767
vueVersion < 2.3,
6868
'is applied to deeply extended components', () => {
69-
const GrandChildComponent = Vue.extend(Vue.extend({
69+
const GrandChildComponent = Vue.extend({
7070
template: '<div>{{$route.params}}</div>'
71-
}))
72-
const ChildComponent = Vue.extend(Vue.extend(Vue.extend({
71+
})
72+
const ChildComponent = Vue.extend({
7373
template: '<div><grand-child-component />{{$route.params}}</div>',
7474
components: {
7575
GrandChildComponent
7676
}
77-
})))
78-
const TestComponent = Vue.extend(Vue.extend({
77+
})
78+
const TestComponent = Vue.extend({
7979
template: '<child-component />',
8080
components: { ChildComponent }
81-
}))
81+
})
8282
const localVue = createLocalVue()
8383
localVue.prototype.$route = {}
8484

@@ -119,16 +119,16 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
119119
template: '<div/>',
120120
extends: BaseGrandChildComponent
121121
}
122-
const ChildComponent = Vue.extend(({
122+
const ChildComponent = Vue.extend({
123123
template: '<div><grand-child-component />{{$route.params}}</div>',
124124
components: {
125125
GrandChildComponent
126126
}
127-
}))
128-
const TestComponent = Vue.extend(Vue.extend({
127+
})
128+
const TestComponent = Vue.extend({
129129
template: '<div><child-component /></div>',
130130
components: { ChildComponent }
131-
}))
131+
})
132132
const localVue = createLocalVue()
133133
localVue.prototype.$route = {}
134134

0 commit comments

Comments
 (0)