Skip to content

Commit

Permalink
feat: silence warnings when updating prop (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdbkr authored and eddyerburgh committed Jun 14, 2018
1 parent 5072274 commit 7fa2fb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ import VueTestUtils from '@vue/test-utils'

VueTestUtils.config.logModifiedComponents = false
```

### `silentWarnings`

- type: `Boolean`
- default: `true`

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`.
Example:

```js
import VueTestUtils from '@vue/test-utils'

VueTestUtils.config.silentWarnings = false
```
3 changes: 2 additions & 1 deletion packages/test-utils/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export default {
mocks: {},
methods: {},
provide: {},
logModifiedComponents: true
logModifiedComponents: true,
silentWarnings: true
}
4 changes: 4 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NAME_SELECTOR,
FUNCTIONAL_OPTIONS
} from './consts'
import config from './config'
import {
vmCtorMatchesName,
vmCtorMatchesSelector,
Expand Down Expand Up @@ -512,6 +513,8 @@ export default class Wrapper implements BaseWrapper {
* Sets vm props
*/
setProps (data: Object) {
const originalConfig = Vue.config.silent
Vue.config.silent = config.silentWarnings
if (this.isFunctionalComponent) {
throwError('wrapper.setProps() cannot be called on a functional component')
}
Expand Down Expand Up @@ -546,6 +549,7 @@ export default class Wrapper implements BaseWrapper {
// $FlowIgnore : Problem with possibly null this.vm
this.vnode = this.vm._vnode
orderWatchers(this.vm || this.vnode.context.$root)
Vue.config.silent = originalConfig
}

/**
Expand Down
42 changes: 39 additions & 3 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
describeWithShallowAndMount,
vueVersion
} from '~resources/utils'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import {
itDoNotRunIf,
itSkipIf
Expand All @@ -10,15 +11,17 @@ import { config, TransitionStub, TransitionGroupStub, createLocalVue } from '~vu
import Vue from 'vue'

describeWithShallowAndMount('config', (mountingMethod) => {
let configStubsSave
let consoleError
let configLogSave
let configStubsSave,
consoleError,
configLogSave,
configSilentWarningsSave

beforeEach(() => {
TransitionGroupStub.name = 'another-temp-name'
TransitionStub.name = 'a-temp-name'
configStubsSave = config.stubs
configLogSave = config.logModifiedComponents
configSilentWarningsSave = config.silentWarnings
consoleError = sinon.stub(console, 'error')
})

Expand All @@ -27,6 +30,7 @@ describeWithShallowAndMount('config', (mountingMethod) => {
TransitionStub.name = 'transition'
config.stubs = configStubsSave
config.logModifiedComponents = configLogSave
config.silentWarnings = configSilentWarningsSave
consoleError.restore()
})

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

it('doesn\'t throw Vue warning when silentWarnings is set to true', () => {
config.silentWarnings = true
const localVue = createLocalVue()
const wrapper = mountingMethod(ComponentWithProps, {
propsData: {
prop1: 'example'
},
localVue
})
expect(wrapper.vm.prop1).to.equal('example')
wrapper.setProps({
prop1: 'new value'
})
expect(consoleError.called).to.equal(false)
})

it('does throw Vue warning when silentWarnings is set to false', () => {
config.silentWarnings = false
const localVue = createLocalVue()
const wrapper = mountingMethod(ComponentWithProps, {
propsData: {
prop1: 'example'
},
localVue
})
expect(wrapper.vm.prop1).to.equal('example')
wrapper.setProps({
prop1: 'new value'
})
expect(consoleError.called).to.equal(true)
})

itSkipIf(
vueVersion < 2.3,
'does not log when component is extended if logModifiedComponents is false', () => {
Expand Down

0 comments on commit 7fa2fb3

Please sign in to comment.