Skip to content

Commit 7cec79d

Browse files
authored
fix: warn when unregistering non existing module (#1786)
* fix: warn when unregistering non existing module * refactor: store child as a variable and reuse it
1 parent bab3155 commit 7cec79d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/module/module-collection.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,21 @@ export default class ModuleCollection {
4949
unregister (path) {
5050
const parent = this.get(path.slice(0, -1))
5151
const key = path[path.length - 1]
52-
if (!parent.getChild(key).runtime) return
52+
const child = parent.getChild(key)
53+
54+
if (!child) {
55+
if (__DEV__) {
56+
console.warn(
57+
`[vuex] trying to unregister module '${key}', which is ` +
58+
`not registered`
59+
)
60+
}
61+
return
62+
}
63+
64+
if (!child.runtime) {
65+
return
66+
}
5367

5468
parent.removeChild(key)
5569
}

test/unit/module/module-collection.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,12 @@ describe('ModuleCollection', () => {
9292
collection.unregister(['a'])
9393
expect(collection.get(['a']).state.value).toBe(true)
9494
})
95+
96+
it('warns when unregistering non existing module', () => {
97+
const spy = jest.spyOn(console, 'warn').mockImplementation()
98+
99+
const collection = new ModuleCollection({})
100+
collection.unregister(['a'])
101+
expect(spy).toHaveBeenCalled()
102+
})
95103
})

0 commit comments

Comments
 (0)