Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,14 @@ export function defineReactive (
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (process.env.NODE_ENV !== 'production' &&
(isUndef(target) || isPrimitive(target))
) {
warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`)
if (process.env.NODE_ENV !== 'production') {
if (isUndef(target) || isPrimitive(target)) {
warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`)
} else if (Object.getOwnPropertyDescriptor(target, key) &&
(typeof (Object.getOwnPropertyDescriptor(target, key).get) === 'undefined') &&
!Array.isArray(target)) {
warn(`Cannot enable reactivity on a property that is already defined: ${(key: any)}`)
}
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.length = Math.max(target.length, key)
Expand Down
16 changes: 16 additions & 0 deletions test/unit/modules/observer/observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ describe('Observer', () => {
}).then(done)
})

it('Cannot enable reactivity on a property that is already defined', done => {
const vm = new Vue({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Vue instance must have a template prop or render fn, your test is throwing an error before getting to the checks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhensche Thanks; that was it.

template: '<div></div>',
data: {
person: {
age: 32
}
}
}).$mount()
vm.person.job = 'Programmer'
Vue.set(vm.person, 'job', 'Educator')
waitForUpdate(() => {
expect(`Cannot enable reactivity on a property that is already defined`).toHaveBeenWarned()
}).then(done)
})

it('warning set/delete on Vue instance root $data', done => {
const data = { a: 1 }
const vm = new Vue({
Expand Down