Skip to content

Commit 7fa2fb3

Browse files
cdbkreddyerburgh
authored andcommitted
feat: silence warnings when updating prop (#688)
1 parent 5072274 commit 7fa2fb3

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

docs/api/config.md

+14
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,17 @@ import VueTestUtils from '@vue/test-utils'
9494

9595
VueTestUtils.config.logModifiedComponents = false
9696
```
97+
98+
### `silentWarnings`
99+
100+
- type: `Boolean`
101+
- default: `true`
102+
103+
It suppresses warnings triggered by Vue while mutating component's observables (e.g. props). When set to `false`, all warnings are visible in the console. This is a configurable way which relies on `Vue.config.silent`.
104+
Example:
105+
106+
```js
107+
import VueTestUtils from '@vue/test-utils'
108+
109+
VueTestUtils.config.silentWarnings = false
110+
```

packages/test-utils/src/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export default {
99
mocks: {},
1010
methods: {},
1111
provide: {},
12-
logModifiedComponents: true
12+
logModifiedComponents: true,
13+
silentWarnings: true
1314
}

packages/test-utils/src/wrapper.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
NAME_SELECTOR,
1010
FUNCTIONAL_OPTIONS
1111
} from './consts'
12+
import config from './config'
1213
import {
1314
vmCtorMatchesName,
1415
vmCtorMatchesSelector,
@@ -512,6 +513,8 @@ export default class Wrapper implements BaseWrapper {
512513
* Sets vm props
513514
*/
514515
setProps (data: Object) {
516+
const originalConfig = Vue.config.silent
517+
Vue.config.silent = config.silentWarnings
515518
if (this.isFunctionalComponent) {
516519
throwError('wrapper.setProps() cannot be called on a functional component')
517520
}
@@ -546,6 +549,7 @@ export default class Wrapper implements BaseWrapper {
546549
// $FlowIgnore : Problem with possibly null this.vm
547550
this.vnode = this.vm._vnode
548551
orderWatchers(this.vm || this.vnode.context.$root)
552+
Vue.config.silent = originalConfig
549553
}
550554

551555
/**

test/specs/config.spec.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
describeWithShallowAndMount,
33
vueVersion
44
} from '~resources/utils'
5+
import ComponentWithProps from '~resources/components/component-with-props.vue'
56
import {
67
itDoNotRunIf,
78
itSkipIf
@@ -10,15 +11,17 @@ import { config, TransitionStub, TransitionGroupStub, createLocalVue } from '~vu
1011
import Vue from 'vue'
1112

1213
describeWithShallowAndMount('config', (mountingMethod) => {
13-
let configStubsSave
14-
let consoleError
15-
let configLogSave
14+
let configStubsSave,
15+
consoleError,
16+
configLogSave,
17+
configSilentWarningsSave
1618

1719
beforeEach(() => {
1820
TransitionGroupStub.name = 'another-temp-name'
1921
TransitionStub.name = 'a-temp-name'
2022
configStubsSave = config.stubs
2123
configLogSave = config.logModifiedComponents
24+
configSilentWarningsSave = config.silentWarnings
2225
consoleError = sinon.stub(console, 'error')
2326
})
2427

@@ -27,6 +30,7 @@ describeWithShallowAndMount('config', (mountingMethod) => {
2730
TransitionStub.name = 'transition'
2831
config.stubs = configStubsSave
2932
config.logModifiedComponents = configLogSave
33+
config.silentWarnings = configSilentWarningsSave
3034
consoleError.restore()
3135
})
3236

@@ -137,6 +141,38 @@ describeWithShallowAndMount('config', (mountingMethod) => {
137141
expect(wrapper.contains(TransitionStub)).to.equal(false)
138142
})
139143

144+
it('doesn\'t throw Vue warning when silentWarnings is set to true', () => {
145+
config.silentWarnings = true
146+
const localVue = createLocalVue()
147+
const wrapper = mountingMethod(ComponentWithProps, {
148+
propsData: {
149+
prop1: 'example'
150+
},
151+
localVue
152+
})
153+
expect(wrapper.vm.prop1).to.equal('example')
154+
wrapper.setProps({
155+
prop1: 'new value'
156+
})
157+
expect(consoleError.called).to.equal(false)
158+
})
159+
160+
it('does throw Vue warning when silentWarnings is set to false', () => {
161+
config.silentWarnings = false
162+
const localVue = createLocalVue()
163+
const wrapper = mountingMethod(ComponentWithProps, {
164+
propsData: {
165+
prop1: 'example'
166+
},
167+
localVue
168+
})
169+
expect(wrapper.vm.prop1).to.equal('example')
170+
wrapper.setProps({
171+
prop1: 'new value'
172+
})
173+
expect(consoleError.called).to.equal(true)
174+
})
175+
140176
itSkipIf(
141177
vueVersion < 2.3,
142178
'does not log when component is extended if logModifiedComponents is false', () => {

0 commit comments

Comments
 (0)