Skip to content

Commit 4c65dbd

Browse files
authoredFeb 2, 2019
fix: use Vue async option for sync mode (#1062)
1 parent 32ce160 commit 4c65dbd

21 files changed

+2380
-1305
lines changed
 

Diff for: ‎.circleci/config.yml

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ restore_node_modules: &restore_node_modules
99
name: Restore node_modules cache
1010
keys:
1111
- v1-dependencies-{{ .Branch }}-{{ checksum "yarn.lock" }}
12-
- v1-dependencies-{{ .Branch }}-
13-
- v1-dependencies-
1412
jobs:
1513
install:
1614
<<: *defaults

Diff for: ‎flow/config.flow.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ declare type Config = {
33
mocks?: Object,
44
methods?: { [name: string]: Function },
55
provide?: Object,
6-
logModifiedComponents?: boolean,
76
silent?: boolean
87
}

Diff for: ‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@
6868
"sinon-chai": "^2.10.0",
6969
"typescript": "^3.0.1",
7070
"vee-validate": "^2.1.3",
71-
"vue": "2.5.16",
71+
"vue": "2.5.21",
7272
"vue-class-component": "^6.1.2",
7373
"vue-loader": "^13.6.2",
7474
"vue-router": "^3.0.1",
75-
"vue-server-renderer": "2.5.16",
76-
"vue-template-compiler": "2.5.16",
75+
"vue-server-renderer": "2.5.21",
76+
"vue-template-compiler": "2.5.21",
7777
"vuepress": "^0.14.2",
7878
"vuepress-theme-vue": "^1.0.3",
7979
"vuex": "^3.0.1",

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ export default function createInstance(
7272
componentOptions.$_vueTestUtils_original = component
7373

7474
// make sure all extends are based on this instance
75-
componentOptions._base = _Vue
7675

7776
const Constructor = _Vue.extend(componentOptions).extend(instanceOptions)
7877

78+
Constructor.options._base = _Vue
79+
7980
const scopedSlots = createScopedSlots(options.scopedSlots, _Vue)
8081

8182
const parentComponentOptions = options.parentComponent || {}

Diff for: ‎packages/create-instance/extract-instance-options.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const MOUNTING_OPTIONS = [
1111
'attrs',
1212
'listeners',
1313
'propsData',
14-
'logModifiedComponents',
1514
'sync',
1615
'shouldProxy'
1716
]

Diff for: ‎packages/server-test-utils/dist/vue-server-test-utils.js

+293-340
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎packages/server-test-utils/types/index.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ interface VueTestUtilsConfigOptions {
5050
mocks?: object
5151
methods?: Record<string, Function>
5252
provide?: object,
53-
logModifiedComponents?: Boolean
5453
silent?: Boolean
5554
}
5655

Diff for: ‎packages/server-test-utils/types/test/renderToString.ts

-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,4 @@ config.methods = {
6262
config.provide = {
6363
foo: {}
6464
}
65-
config.logModifiedComponents = true
6665
config.silent = true

Diff for: ‎packages/shared/consts.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const COMPONENT_SELECTOR = 'COMPONENT_SELECTOR'
66
export const REF_SELECTOR = 'REF_SELECTOR'
77
export const DOM_SELECTOR = 'DOM_SELECTOR'
88
export const INVALID_SELECTOR = 'INVALID_SELECTOR'
9+
export const COMPAT_SYNC_MODE = 'COMPAT_SYNC_MODE'
910

1011
export const VUE_VERSION = Number(
1112
`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`

Diff for: ‎packages/shared/merge-options.js

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export function mergeOptions(options: Options, config: Config): Options {
2828
return {
2929
...options,
3030
provide: normalizeProvide(provide),
31-
logModifiedComponents: config.logModifiedComponents,
3231
stubs: getOption(normalizeStubs(options.stubs), config.stubs),
3332
mocks,
3433
methods,

Diff for: ‎packages/test-utils/dist/vue-test-utils.js

+1,921-896
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎packages/test-utils/src/mount.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,41 @@ import config from './config'
1010
import warnIfNoWindow from './warn-if-no-window'
1111
import createWrapper from './create-wrapper'
1212
import createLocalVue from './create-local-vue'
13+
import { warn } from 'shared/util'
14+
import semver from 'semver'
15+
import { COMPAT_SYNC_MODE } from 'shared/consts'
1316
import { validateOptions } from 'shared/validate-options'
1417

1518
Vue.config.productionTip = false
1619
Vue.config.devtools = false
1720

21+
function getSyncOption(syncOption) {
22+
if (syncOption === false) {
23+
Vue.config.async = true
24+
return false
25+
}
26+
if (semver.lt(Vue.version, '2.5.18')) {
27+
warn(
28+
`Vue Test Utils runs in sync mode by default. Due to bugs, sync mode ` +
29+
`requires Vue > 2.5.18. In Vue Test Utils 1.0 sync mode will only be ` +
30+
`supported with Vue 2.5.18+ running in development mode. If you are ` +
31+
`unable to upgrade, you should rewrite your tests to run asynchronously` +
32+
`you can do this by setting the sync mounting option to false.`
33+
)
34+
return COMPAT_SYNC_MODE
35+
}
36+
37+
if (typeof Vue.config.async === 'undefined') {
38+
warn(
39+
`Sync mode only works when Vue runs in dev mode. ` +
40+
`Please set Vue to run in dev mode, or set sync to false`
41+
)
42+
}
43+
44+
Vue.config.async = false
45+
return true
46+
}
47+
1848
export default function mount(
1949
component: Component,
2050
options: Options = {}
@@ -37,10 +67,11 @@ export default function mount(
3767
component._Ctor = {}
3868

3969
throwIfInstancesThrew(vm)
70+
const sync = getSyncOption(mergedOptions.sync)
4071

4172
const wrapperOptions = {
4273
attachedToDocument: !!mergedOptions.attachToDocument,
43-
sync: mergedOptions.sync
74+
sync
4475
}
4576

4677
const root = parentVm.$options._isFunctionalContainer

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Wrapper from './wrapper'
44
import { throwError } from 'shared/util'
55
import { setWatchersToSync } from './set-watchers-to-sync'
66
import { orderWatchers } from './order-watchers'
7+
import { COMPAT_SYNC_MODE } from 'shared/consts'
78

89
export default class VueWrapper extends Wrapper implements BaseWrapper {
910
constructor(vm: Component, options: WrapperOptions) {
@@ -28,7 +29,7 @@ export default class VueWrapper extends Wrapper implements BaseWrapper {
2829
get: () => vm,
2930
set: () => throwError('wrapper.vm is read-only')
3031
})
31-
if (options.sync) {
32+
if (options.sync === COMPAT_SYNC_MODE) {
3233
setWatchersToSync(vm)
3334
orderWatchers(vm)
3435
}

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import Vue from 'vue'
44
import getSelector from './get-selector'
5-
import { REF_SELECTOR, FUNCTIONAL_OPTIONS, VUE_VERSION } from 'shared/consts'
5+
import {
6+
REF_SELECTOR,
7+
FUNCTIONAL_OPTIONS,
8+
VUE_VERSION,
9+
COMPAT_SYNC_MODE
10+
} from 'shared/consts'
611
import config from './config'
712
import WrapperArray from './wrapper-array'
813
import ErrorWrapper from './error-wrapper'
@@ -505,8 +510,10 @@ export default class Wrapper implements BaseWrapper {
505510
})
506511
// $FlowIgnore : Problem with possibly null this.vm
507512
this.vm.$forceUpdate()
508-
// $FlowIgnore : Problem with possibly null this.vm
509-
orderWatchers(this.vm || this.vnode.context.$root)
513+
if (this.options.sync === COMPAT_SYNC_MODE) {
514+
// $FlowIgnore : Problem with possibly null this.vm
515+
orderWatchers(this.vm || this.vnode.context.$root)
516+
}
510517
Vue.config.silent = originalConfig
511518
}
512519

@@ -578,7 +585,7 @@ export default class Wrapper implements BaseWrapper {
578585
const event = createDOMEvent(type, options)
579586
this.element.dispatchEvent(event)
580587

581-
if (this.vnode) {
588+
if (this.vnode && this.options.sync === COMPAT_SYNC_MODE) {
582589
orderWatchers(this.vm || this.vnode.context.$root)
583590
}
584591
}

Diff for: ‎scripts/test-compat-all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ scripts/test-compat.sh "2.1.10"
77
scripts/test-compat.sh "2.2.6"
88
scripts/test-compat.sh "2.3.4"
99
scripts/test-compat.sh "2.4.2"
10-
scripts/test-compat.sh "2.5.16"
10+
scripts/test-compat.sh "2.5.20"

Diff for: ‎test/specs/config.spec.js

+7-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
1+
import { describeWithShallowAndMount } from '~resources/utils'
22
import ComponentWithProps from '~resources/components/component-with-props.vue'
3-
import { itDoNotRunIf, itSkipIf } from 'conditional-specs'
3+
import { itDoNotRunIf } from 'conditional-specs'
44
import {
55
config,
66
TransitionStub,
77
TransitionGroupStub,
88
createLocalVue
99
} from '~vue/test-utils'
10-
import Vue from 'vue'
1110

1211
describeWithShallowAndMount('config', mountingMethod => {
13-
let configStubsSave, consoleError, configLogSave, configSilentSave
12+
let configStubsSave, configLogSave, configSilentSave
1413

1514
beforeEach(() => {
1615
configStubsSave = config.stubs
1716
configLogSave = config.logModifiedComponents
1817
configSilentSave = config.silent
19-
consoleError = sinon.stub(console, 'error')
18+
sinon.stub(console, 'error').callThrough()
2019
})
2120

2221
afterEach(() => {
2322
config.stubs = configStubsSave
2423
config.logModifiedComponents = configLogSave
2524
config.silent = configSilentSave
26-
consoleError.restore()
25+
console.error.restore()
2726
})
2827

2928
itDoNotRunIf(
@@ -150,7 +149,7 @@ describeWithShallowAndMount('config', mountingMethod => {
150149
wrapper.setProps({
151150
prop1: 'new value'
152151
})
153-
expect(consoleError.called).to.equal(false)
152+
expect(console.error).not.calledWith(sinon.match('[Vue warn]'))
154153
})
155154

156155
it('does throw Vue warning when silent is set to false', () => {
@@ -166,25 +165,6 @@ describeWithShallowAndMount('config', mountingMethod => {
166165
wrapper.setProps({
167166
prop1: 'new value'
168167
})
169-
expect(consoleError.called).to.equal(true)
168+
expect(console.error).calledWith(sinon.match('[Vue warn]'))
170169
})
171-
172-
itSkipIf(
173-
vueVersion < 2.3,
174-
'does not log when component is extended if logModifiedComponents is false',
175-
() => {
176-
const ChildComponent = Vue.extend({
177-
template: '<span />'
178-
})
179-
const TestComponent = {
180-
template: '<child-component />',
181-
components: {
182-
ChildComponent
183-
}
184-
}
185-
config.logModifiedComponents = false
186-
mountingMethod(TestComponent)
187-
expect(consoleError.called).to.equal(false)
188-
}
189-
)
190170
})

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

+20-16
Original file line numberDiff line numberDiff line change
@@ -412,21 +412,25 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
412412
})
413413
})
414414

415-
it('throws if component throws during update', () => {
416-
const TestComponent = {
417-
template: '<div :p="a" />',
418-
updated() {
419-
throw new Error('err')
420-
},
421-
data: () => ({
422-
a: 1
423-
})
424-
}
425-
const wrapper = mount(TestComponent)
426-
const fn = () => {
427-
wrapper.vm.a = 2
415+
itDoNotRunIf(
416+
vueVersion >= 2.5,
417+
'throws if component throws during update',
418+
() => {
419+
const TestComponent = {
420+
template: '<div :p="a" />',
421+
updated() {
422+
throw new Error('err')
423+
},
424+
data: () => ({
425+
a: 1
426+
})
427+
}
428+
const wrapper = mount(TestComponent)
429+
const fn = () => {
430+
wrapper.vm.a = 2
431+
}
432+
expect(fn).to.throw()
433+
wrapper.destroy()
428434
}
429-
expect(fn).to.throw()
430-
wrapper.destroy()
431-
})
435+
)
432436
})

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

+27
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
231231
}
232232
)
233233

234+
itDoNotRunIf(
235+
vueVersion < 2.5,
236+
'renders scoped slots in sync mode by default',
237+
() => {
238+
const TestComponent = {
239+
data() {
240+
return {
241+
val: null
242+
}
243+
},
244+
mounted() {
245+
this.val = 123
246+
},
247+
render() {
248+
return this.$scopedSlots.default(this.val)
249+
}
250+
}
251+
const stub = sinon.stub()
252+
mountingMethod(TestComponent, {
253+
scopedSlots: {
254+
default: stub
255+
}
256+
})
257+
expect(stub).calledWith(123)
258+
}
259+
)
260+
234261
itDoNotRunIf(
235262
vueVersion < 2.5 || mountingMethod.name !== 'mount',
236263
'renders using localVue constructor',

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import sinon from 'sinon'
2-
import { describeWithShallowAndMount } from '~resources/utils'
2+
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
3+
import { itDoNotRunIf } from 'conditional-specs'
34

45
describeWithShallowAndMount('options.sync', mountingMethod => {
6+
beforeEach(() => {
7+
sinon.stub(console, 'error').callThrough()
8+
})
9+
10+
afterEach(() => {
11+
console.error.restore()
12+
})
13+
514
it('sets watchers to sync if set to true', () => {
615
const TestComponent = {
716
template: '<div>{{someData}}</div>',
@@ -12,11 +21,12 @@ describeWithShallowAndMount('options.sync', mountingMethod => {
1221
const wrapper = mountingMethod(TestComponent, {
1322
sync: true
1423
})
24+
const syncValue = vueVersion < 2.5 ? 'COMPAT_SYNC_MODE' : true
1525

1626
expect(wrapper.text()).to.equal('hello')
1727
wrapper.vm.someData = 'world'
1828
expect(wrapper.text()).to.equal('world')
19-
expect(wrapper.options.sync).to.equal(true)
29+
expect(wrapper.options.sync).to.equal(syncValue)
2030
})
2131

2232
it('sets watchers to sync if undefined', () => {
@@ -27,11 +37,12 @@ describeWithShallowAndMount('options.sync', mountingMethod => {
2737
})
2838
}
2939
const wrapper = mountingMethod(TestComponent)
40+
const syncValue = vueVersion < 2.5 ? 'COMPAT_SYNC_MODE' : true
3041

3142
expect(wrapper.text()).to.equal('hello')
3243
wrapper.vm.someData = 'world'
3344
expect(wrapper.text()).to.equal('world')
34-
expect(wrapper.options.sync).to.equal(true)
45+
expect(wrapper.options.sync).to.equal(syncValue)
3546
})
3647

3748
it('handles methods that update watchers', () => {
@@ -145,4 +156,18 @@ describeWithShallowAndMount('options.sync', mountingMethod => {
145156
expect(childComponentSpy.calledOnce).to.equal(true)
146157
expect(wrapper.html()).to.equal('<div>bar<div>bar</div></div>')
147158
})
159+
160+
itDoNotRunIf(
161+
vueVersion > 2.4,
162+
'warns if Vue version is less than 2.5.18',
163+
() => {
164+
const TestComponent = {
165+
template: '<div />'
166+
}
167+
mountingMethod(TestComponent)
168+
expect(console.error).calledWith(
169+
sinon.match('Vue Test Utils runs in sync mode by default')
170+
)
171+
}
172+
)
148173
})

Diff for: ‎test/specs/shallow-mount.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
149149
}
150150
}
151151
shallowMount(TestComponent)
152-
expect(console.error).not.called
152+
expect(console.error).not.calledWith(sinon.match('[Vue warn]'))
153153
}
154154
)
155155

@@ -254,7 +254,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
254254
}
255255
const wrapper = shallowMount(TestComponent)
256256
expect(wrapper.html()).to.contain('<test-component-stub>')
257-
expect(console.error).not.called
257+
expect(console.error).not.calledWith('[Vue warn]')
258258
})
259259

260260
it('does not call stubbed children lifecycle hooks', () => {

Diff for: ‎yarn.lock

+30-3
Original file line numberDiff line numberDiff line change
@@ -9823,7 +9823,21 @@ vue-router@^3.0.1:
98239823
version "3.0.1"
98249824
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.0.1.tgz#d9b05ad9c7420ba0f626d6500d693e60092cc1e9"
98259825

9826-
vue-server-renderer@2.5.16, vue-server-renderer@^2.5.16:
9826+
vue-server-renderer@2.5.21:
9827+
version "2.5.21"
9828+
resolved "https://registry.yarnpkg.com/vue-server-renderer/-/vue-server-renderer-2.5.21.tgz#7c2b611d2e8558359cdb84dcd3080845c07121b4"
9829+
integrity sha512-bAkOYZ5DnmQKv3RboNbmCB4LReF2tIE20EgUeWrz/87aVkpMihf3FVK+8DT45nyN4k9iSQOhsyem49fq++cF8w==
9830+
dependencies:
9831+
chalk "^1.1.3"
9832+
hash-sum "^1.0.2"
9833+
he "^1.1.0"
9834+
lodash.template "^4.4.0"
9835+
lodash.uniq "^4.5.0"
9836+
resolve "^1.2.0"
9837+
serialize-javascript "^1.3.0"
9838+
source-map "0.5.6"
9839+
9840+
vue-server-renderer@^2.5.16:
98279841
version "2.5.16"
98289842
resolved "https://registry.yarnpkg.com/vue-server-renderer/-/vue-server-renderer-2.5.16.tgz#279ef8e37e502a0de3a9ae30758cc04a472eaac0"
98299843
dependencies:
@@ -9850,7 +9864,15 @@ vue-style-loader@^4.1.0:
98509864
hash-sum "^1.0.2"
98519865
loader-utils "^1.0.2"
98529866

9853-
vue-template-compiler@2.5.16, vue-template-compiler@^2.5.16:
9867+
vue-template-compiler@2.5.21:
9868+
version "2.5.21"
9869+
resolved "https://registry.yarnpkg.com/vue-template-compiler/-/vue-template-compiler-2.5.21.tgz#a57ceb903177e8f643560a8d639a0f8db647054a"
9870+
integrity sha512-Vmk5Cv7UcmI99B9nXJEkaK262IQNnHp5rJYo+EwYpe2epTAXqcVyExhV6pk8jTkxQK2vRc8v8KmZBAwdmUZvvw==
9871+
dependencies:
9872+
de-indent "^1.0.2"
9873+
he "^1.1.0"
9874+
9875+
vue-template-compiler@^2.5.16:
98549876
version "2.5.16"
98559877
resolved "https://registry.yarnpkg.com/vue-template-compiler/-/vue-template-compiler-2.5.16.tgz#93b48570e56c720cdf3f051cc15287c26fbd04cb"
98569878
dependencies:
@@ -9861,7 +9883,12 @@ vue-template-es2015-compiler@^1.6.0:
98619883
version "1.6.0"
98629884
resolved "https://registry.yarnpkg.com/vue-template-es2015-compiler/-/vue-template-es2015-compiler-1.6.0.tgz#dc42697133302ce3017524356a6c61b7b69b4a18"
98639885

9864-
vue@2.5.16, vue@^2.5.16:
9886+
vue@2.5.21:
9887+
version "2.5.21"
9888+
resolved "https://registry.yarnpkg.com/vue/-/vue-2.5.21.tgz#3d33dcd03bb813912ce894a8303ab553699c4a85"
9889+
integrity sha512-Aejvyyfhn0zjVeLvXd70h4hrE4zZDx1wfZqia6ekkobLmUZ+vNFQer53B4fu0EjWBSiqApxPejzkO1Znt3joxQ==
9890+
9891+
vue@^2.5.16:
98659892
version "2.5.16"
98669893
resolved "https://registry.yarnpkg.com/vue/-/vue-2.5.16.tgz#07edb75e8412aaeed871ebafa99f4672584a0085"
98679894

0 commit comments

Comments
 (0)
Please sign in to comment.