Skip to content

fix: reconcile the overridden prototype of component with _Vue mixins #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,23 @@ export default function createInstance (
component.options._base = _Vue
}

function getExtendedComponent (component, instanceOptions) {
const extendedComponent = component.extend(instanceOptions)
// to keep the possible overridden prototype and _Vue mixins,
// we need change the proto chains manually
// @see https://github.com/vuejs/vue-test-utils/pull/856
// code below equals to
// `extendedComponent.prototype.__proto__.__proto__ = _Vue.prototype`
const extendedComponentProto =
Object.getPrototypeOf(extendedComponent.prototype)
Object.setPrototypeOf(extendedComponentProto, _Vue.prototype)

return extendedComponent
}

// extend component from _Vue to add properties and mixins
const Constructor = typeof component === 'function' && vueVersion < 2.3
? component.extend(instanceOptions)
const Constructor = typeof component === 'function'
? getExtendedComponent(component, instanceOptions)
: _Vue.extend(component).extend(instanceOptions)

Object.keys(instanceOptions.components || {}).forEach(key => {
Expand Down
4 changes: 2 additions & 2 deletions test/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
expect(stub).not.called
})

it.skip('overrides component prototype', () => {
it('overrides component prototype', () => {
const mountSpy = sinon.spy()
const destroySpy = sinon.spy()
const Component = Vue.extend({})
const { $mount: originalMount, $destroy: originalDestroy } = Component.prototype
Component.prototype.$mount = function (...args) {
mountSpy()
originalMount.apply(this, args)
mountSpy()
return this
}
Component.prototype.$destroy = function () {
Expand Down