Skip to content

Commit 5500553

Browse files
authored
fix: stop stubs leaking with localVue (#1056)
1 parent 9abfda5 commit 5500553

File tree

11 files changed

+44
-33
lines changed

11 files changed

+44
-33
lines changed

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

-10
This file was deleted.

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { addHook } from './add-hook'
1+
import { BEFORE_RENDER_LIFECYCLE_HOOK } from 'shared/consts'
22

33
export function addStubs (_Vue, stubComponents) {
44
function addStubComponentsMixin () {
55
Object.assign(this.$options.components, stubComponents)
66
}
77

8-
addHook(_Vue.options, 'beforeMount', addStubComponentsMixin)
9-
// beforeCreate is for components created in node, which
10-
// never mount
11-
addHook(_Vue.options, 'beforeCreate', addStubComponentsMixin)
8+
_Vue.mixin({
9+
[BEFORE_RENDER_LIFECYCLE_HOOK]: addStubComponentsMixin
10+
})
1211
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @flow
2-
import { addHook } from './add-hook'
32

43
export function logEvents (
54
vm: Component,
@@ -15,10 +14,11 @@ export function logEvents (
1514
}
1615

1716
export function addEventLogger (_Vue: Component): void {
18-
addHook(_Vue.options, 'beforeCreate', function () {
19-
this.__emitted = Object.create(null)
20-
this.__emittedByOrder = []
21-
logEvents(this, this.__emitted, this.__emittedByOrder)
22-
}
23-
)
17+
_Vue.mixin({
18+
beforeCreate: function () {
19+
this.__emitted = Object.create(null)
20+
this.__emittedByOrder = []
21+
logEvents(this, this.__emitted, this.__emittedByOrder)
22+
}
23+
})
2424
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createStubFromComponent } from './create-component-stubs'
22
import { resolveComponent, semVerGreaterThan } from 'shared/util'
33
import { isReservedTag } from 'shared/validators'
4-
import { addHook } from './add-hook'
54
import Vue from 'vue'
5+
import { BEFORE_RENDER_LIFECYCLE_HOOK } from 'shared/consts'
66

77
const isWhitelisted = (el, whitelist) => resolveComponent(el, whitelist)
88
const isAlreadyStubbed = (el, stubs) => stubs.has(el)
@@ -11,9 +11,6 @@ const isDynamicComponent = cmp => typeof cmp === 'function' && !cmp.cid
1111
const CREATE_ELEMENT_ALIAS = semVerGreaterThan(Vue.version, '2.1.5')
1212
? '_c'
1313
: '_h'
14-
const LIFECYCLE_HOOK = semVerGreaterThan(Vue.version, '2.1.8')
15-
? 'beforeCreate'
16-
: 'beforeMount'
1714

1815
function shouldExtend (component, _Vue) {
1916
return (
@@ -126,5 +123,7 @@ export function patchRender (_Vue, stubs, stubAllComponents) {
126123
vm.$createElement = createElement
127124
}
128125

129-
addHook(_Vue.options, LIFECYCLE_HOOK, patchRenderMixin)
126+
_Vue.mixin({
127+
[BEFORE_RENDER_LIFECYCLE_HOOK]: patchRenderMixin
128+
})
130129
}

Diff for: packages/test-utils/src/consts.js renamed to packages/shared/consts.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { semVerGreaterThan } from './util'
23

34
export const NAME_SELECTOR = 'NAME_SELECTOR'
45
export const COMPONENT_SELECTOR = 'COMPONENT_SELECTOR'
@@ -10,3 +11,8 @@ export const VUE_VERSION = Number(
1011
)
1112
export const FUNCTIONAL_OPTIONS =
1213
VUE_VERSION >= 2.5 ? 'fnOptions' : 'functionalOptions'
14+
15+
export const BEFORE_RENDER_LIFECYCLE_HOOK =
16+
semVerGreaterThan(Vue.version, '2.1.8')
17+
? 'beforeCreate'
18+
: 'beforeMount'

Diff for: packages/test-utils/src/find.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
DOM_SELECTOR,
66
REF_SELECTOR,
77
COMPONENT_SELECTOR
8-
} from './consts'
8+
} from 'shared/consts'
99
import { throwError, vueVersion } from 'shared/util'
1010
import { matches } from './matches'
1111

Diff for: packages/test-utils/src/get-selector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
NAME_SELECTOR,
1414
DOM_SELECTOR,
1515
INVALID_SELECTOR
16-
} from './consts'
16+
} from 'shared/consts'
1717

1818
function getSelectorType (
1919
selector: Selector

Diff for: packages/test-utils/src/matches.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
DOM_SELECTOR,
33
COMPONENT_SELECTOR,
44
FUNCTIONAL_OPTIONS
5-
} from './consts'
5+
} from 'shared/consts'
66

77
export function vmMatchesName (vm, name) {
88
return !!name && (

Diff for: packages/test-utils/src/set-watchers-to-sync.js

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

3-
import { VUE_VERSION } from './consts'
3+
import { VUE_VERSION } from 'shared/consts'
44

55
function setDepsSync (dep): void {
66
dep.subs.forEach(setWatcherSync)

Diff for: packages/test-utils/src/wrapper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getSelector from './get-selector'
55
import {
66
REF_SELECTOR,
77
FUNCTIONAL_OPTIONS
8-
} from './consts'
8+
} from 'shared/consts'
99
import config from './config'
1010
import WrapperArray from './wrapper-array'
1111
import ErrorWrapper from './error-wrapper'

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isRunningPhantomJS,
55
vueVersion
66
} from '~resources/utils'
7-
import { createLocalVue } from '~vue/test-utils'
7+
import { createLocalVue, shallowMount, mount } from '~vue/test-utils'
88
import { itSkipIf, itRunIf, itDoNotRunIf } from 'conditional-specs'
99
import Vuex from 'vuex'
1010

@@ -196,4 +196,21 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
196196
}
197197
expect(wrapper.findAll(ChildComponent).length).to.equal(1)
198198
})
199+
200+
itRunIf(
201+
mountingMethod.name === 'mount',
202+
'does not affect future tests', () => {
203+
const ChildComponent = {
204+
template: '<span></span>'
205+
}
206+
const TestComponent = {
207+
template: '<child-component />',
208+
components: { ChildComponent }
209+
}
210+
const localVue = createLocalVue()
211+
localVue.use(Vuex)
212+
shallowMount(TestComponent, { localVue })
213+
const wrapper = mount(TestComponent, { localVue })
214+
expect(wrapper.html()).to.contain('span')
215+
})
199216
})

0 commit comments

Comments
 (0)