Skip to content

feat: add warning in dev mode when method is not function (#… #8022

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

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ function initMethods (vm: Component, methods: Object) {
`Did you reference the function correctly?`,
vm
)
} else if (typeof methods[key] !== 'function') {
warn(
`Method "${key}" should be a function.`
)
methods[key] = null
}
if (props && hasOwn(props, key)) {
warn(
Expand Down
13 changes: 11 additions & 2 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Options methods', () => {
foo () {}
}
})
expect(`Method "foo" has already been defined as a data property`).toHaveBeenWarned()
expect('Method "foo" has already been defined as a data property').toHaveBeenWarned()
})

it('should warn methods conflicting with internal methods', () => {
Expand All @@ -46,6 +46,15 @@ describe('Options methods', () => {
_update () {}
}
})
expect(`Method "_update" conflicts with an existing Vue instance method`).toHaveBeenWarned()
expect('Method "_update" conflicts with an existing Vue instance method').toHaveBeenWarned()
})

it('should warn if method is not a function', () => {
new Vue({
methods: {
notAFunction: {}
}
})
expect('Method "notAFunction" should be a function').toHaveBeenWarned()
})
})