Skip to content

Commit f94c99c

Browse files
committed
fix: reconcile the overridden prototype of component and _Vue mocks and mixins
1 parent e8d9547 commit f94c99c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22

3+
import Vue from 'vue'
34
import { createSlotVNodes } from './create-slot-vnodes'
45
import addMocks from './add-mocks'
56
import { addEventLogger } from './log-events'
@@ -109,10 +110,35 @@ export default function createInstance (
109110
component.options._base = _Vue
110111
}
111112

113+
function getRootVueProto (obj) {
114+
while (obj) {
115+
if (Object.getPrototypeOf(obj) === Vue.prototype) {
116+
return obj
117+
}
118+
119+
obj = Object.getPrototypeOf(obj)
120+
}
121+
}
122+
123+
function getExtendedComponent (component, instanceOptions) {
124+
// extend _Vue to merge the mixins on _Vue
125+
const extendedComponent = component.extend(_Vue).extend(instanceOptions)
126+
127+
// cache subclass constructor
128+
component.options._Ctor[extendedComponent.cid] = extendedComponent
129+
130+
// to keep the possible overridden prototype and _Vue mocks on prototype,
131+
// we need change the proto chains manually
132+
// @see https://github.com/vuejs/vue-test-utils/pull/856
133+
const root = getRootVueProto(extendedComponent.prototype)
134+
Object.setPrototypeOf(root, _Vue.prototype)
135+
136+
return extendedComponent
137+
}
138+
112139
// extend component from _Vue to add properties and mixins
113-
// extend does not work correctly for sub class components in Vue < 2.2
114-
const Constructor = typeof component === 'function' && vueVersion < 2.3
115-
? component.extend(instanceOptions)
140+
const Constructor = typeof component === 'function'
141+
? getExtendedComponent(component, instanceOptions)
116142
: _Vue.extend(component).extend(instanceOptions)
117143

118144
// Keep reference to component mount was called with

Diff for: test/specs/mount.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
156156
expect(stub).not.called
157157
})
158158

159-
it.skip('overrides component prototype', () => {
159+
it('overrides component prototype', () => {
160160
const mountSpy = sinon.spy()
161161
const destroySpy = sinon.spy()
162162
const Component = Vue.extend({})

0 commit comments

Comments
 (0)