Skip to content
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

fix: stop stubs leaking with localVue #1056

Merged
merged 2 commits into from Dec 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions packages/create-instance/add-hook.js

This file was deleted.

9 changes: 4 additions & 5 deletions packages/create-instance/add-stubs.js
@@ -1,12 +1,11 @@
import { addHook } from './add-hook'
import { BEFORE_RENDER_LIFECYCLE_HOOK } from 'shared/consts'

export function addStubs (_Vue, stubComponents) {
function addStubComponentsMixin () {
Object.assign(this.$options.components, stubComponents)
}

addHook(_Vue.options, 'beforeMount', addStubComponentsMixin)
// beforeCreate is for components created in node, which
// never mount
addHook(_Vue.options, 'beforeCreate', addStubComponentsMixin)
_Vue.mixin({
[BEFORE_RENDER_LIFECYCLE_HOOK]: addStubComponentsMixin
})
}
14 changes: 7 additions & 7 deletions packages/create-instance/log-events.js
@@ -1,5 +1,4 @@
// @flow
import { addHook } from './add-hook'

export function logEvents (
vm: Component,
Expand All @@ -15,10 +14,11 @@ export function logEvents (
}

export function addEventLogger (_Vue: Component): void {
addHook(_Vue.options, 'beforeCreate', function () {
this.__emitted = Object.create(null)
this.__emittedByOrder = []
logEvents(this, this.__emitted, this.__emittedByOrder)
}
)
_Vue.mixin({
beforeCreate: function () {
this.__emitted = Object.create(null)
this.__emittedByOrder = []
logEvents(this, this.__emitted, this.__emittedByOrder)
}
})
}
9 changes: 4 additions & 5 deletions packages/create-instance/patch-render.js
@@ -1,8 +1,8 @@
import { createStubFromComponent } from './create-component-stubs'
import { resolveComponent, semVerGreaterThan } from 'shared/util'
import { isReservedTag } from 'shared/validators'
import { addHook } from './add-hook'
import Vue from 'vue'
import { BEFORE_RENDER_LIFECYCLE_HOOK } from 'shared/consts'

const isWhitelisted = (el, whitelist) => resolveComponent(el, whitelist)
const isAlreadyStubbed = (el, stubs) => stubs.has(el)
Expand All @@ -11,9 +11,6 @@ const isDynamicComponent = cmp => typeof cmp === 'function' && !cmp.cid
const CREATE_ELEMENT_ALIAS = semVerGreaterThan(Vue.version, '2.1.5')
? '_c'
: '_h'
const LIFECYCLE_HOOK = semVerGreaterThan(Vue.version, '2.1.8')
? 'beforeCreate'
: 'beforeMount'

function shouldExtend (component, _Vue) {
return (
Expand Down Expand Up @@ -126,5 +123,7 @@ export function patchRender (_Vue, stubs, stubAllComponents) {
vm.$createElement = createElement
}

addHook(_Vue.options, LIFECYCLE_HOOK, patchRenderMixin)
_Vue.mixin({
[BEFORE_RENDER_LIFECYCLE_HOOK]: patchRenderMixin
})
}
@@ -1,4 +1,5 @@
import Vue from 'vue'
import { semVerGreaterThan } from './util'

export const NAME_SELECTOR = 'NAME_SELECTOR'
export const COMPONENT_SELECTOR = 'COMPONENT_SELECTOR'
Expand All @@ -10,3 +11,8 @@ export const VUE_VERSION = Number(
)
export const FUNCTIONAL_OPTIONS =
VUE_VERSION >= 2.5 ? 'fnOptions' : 'functionalOptions'

export const BEFORE_RENDER_LIFECYCLE_HOOK =
semVerGreaterThan(Vue.version, '2.1.8')
? 'beforeCreate'
: 'beforeMount'
2 changes: 1 addition & 1 deletion packages/test-utils/src/find.js
Expand Up @@ -5,7 +5,7 @@ import {
DOM_SELECTOR,
REF_SELECTOR,
COMPONENT_SELECTOR
} from './consts'
} from 'shared/consts'
import { throwError, vueVersion } from 'shared/util'
import { matches } from './matches'

Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/src/get-selector.js
Expand Up @@ -13,7 +13,7 @@ import {
NAME_SELECTOR,
DOM_SELECTOR,
INVALID_SELECTOR
} from './consts'
} from 'shared/consts'

function getSelectorType (
selector: Selector
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/src/matches.js
Expand Up @@ -2,7 +2,7 @@ import {
DOM_SELECTOR,
COMPONENT_SELECTOR,
FUNCTIONAL_OPTIONS
} from './consts'
} from 'shared/consts'

export function vmMatchesName (vm, name) {
return !!name && (
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/src/set-watchers-to-sync.js
@@ -1,6 +1,6 @@
// @flow

import { VUE_VERSION } from './consts'
import { VUE_VERSION } from 'shared/consts'

function setDepsSync (dep): void {
dep.subs.forEach(setWatcherSync)
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/src/wrapper.js
Expand Up @@ -5,7 +5,7 @@ import getSelector from './get-selector'
import {
REF_SELECTOR,
FUNCTIONAL_OPTIONS
} from './consts'
} from 'shared/consts'
import config from './config'
import WrapperArray from './wrapper-array'
import ErrorWrapper from './error-wrapper'
Expand Down
19 changes: 18 additions & 1 deletion test/specs/mounting-options/localVue.spec.js
Expand Up @@ -4,7 +4,7 @@ import {
isRunningPhantomJS,
vueVersion
} from '~resources/utils'
import { createLocalVue } from '~vue/test-utils'
import { createLocalVue, shallowMount, mount } from '~vue/test-utils'
import { itSkipIf, itRunIf, itDoNotRunIf } from 'conditional-specs'
import Vuex from 'vuex'

Expand Down Expand Up @@ -196,4 +196,21 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
}
expect(wrapper.findAll(ChildComponent).length).to.equal(1)
})

itRunIf(
mountingMethod.name === 'mount',
'does not affect future tests', () => {
const ChildComponent = {
template: '<span></span>'
}
const TestComponent = {
template: '<child-component />',
components: { ChildComponent }
}
const localVue = createLocalVue()
localVue.use(Vuex)
shallowMount(TestComponent, { localVue })
const wrapper = mount(TestComponent, { localVue })
expect(wrapper.html()).to.contain('span')
})
})