Skip to content

Commit f065523

Browse files
holmesconanyyx990803
authored andcommitted
console.warn when getter is not a function (#151)
1 parent bd17dd1 commit f065523

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/override.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ export default function (Vue) {
7474
*/
7575

7676
function defineVuexGetter (vm, key, getter) {
77-
Object.defineProperty(vm, key, {
78-
enumerable: true,
79-
configurable: true,
80-
get: makeComputedGetter(vm.$store, getter),
81-
set: setter
82-
})
77+
if (typeof getter !== 'function') {
78+
console.warn(`[vuex] Getter bound to key 'vuex.getters.${key}' is not a function.`)
79+
} else {
80+
Object.defineProperty(vm, key, {
81+
enumerable: true,
82+
configurable: true,
83+
get: makeComputedGetter(vm.$store, getter),
84+
set: setter
85+
})
86+
}
8387
}
8488

8589
/**
@@ -95,6 +99,7 @@ export default function (Vue) {
9599

96100
function makeComputedGetter (store, getter) {
97101
const id = store._getterCacheId
102+
98103
// cached
99104
if (getter[id]) {
100105
return getter[id]

test/unit/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,30 @@ describe('Vuex', () => {
464464
expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.')
465465
console.warn.restore()
466466
})
467+
468+
it('console.warn when getter is not a function', function () {
469+
const store = new Vuex.Store({
470+
state: {
471+
a: 1
472+
},
473+
mutations: {
474+
[TEST] (state, amount) {
475+
state.a += amount
476+
}
477+
}
478+
})
479+
sinon.spy(console, 'warn')
480+
481+
new Vue({
482+
store,
483+
vuex: {
484+
getters: {
485+
test: undefined
486+
}
487+
}
488+
})
489+
490+
expect(console.warn).to.have.been.calledWith('[vuex] Getter bound to key \'vuex.getters.test\' is not a function.')
491+
console.warn.restore()
492+
})
467493
})

0 commit comments

Comments
 (0)