Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: In development mode, warns if user tries to Vue.set a property t… #8138

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,15 @@ 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)}`)
}
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
15 changes: 15 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,21 @@ 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.

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()
}).end(done)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be then rather than end.

Copy link
Author

Choose a reason for hiding this comment

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

I've tried that as well, but the result is the same.

})

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