From 7cec79d339b874ec41f35891c891dfd27460c1d3 Mon Sep 17 00:00:00 2001 From: Kia King Ishii Date: Mon, 29 Jun 2020 23:53:53 +0900 Subject: [PATCH] fix: warn when unregistering non existing module (#1786) * fix: warn when unregistering non existing module * refactor: store child as a variable and reuse it --- src/module/module-collection.js | 16 +++++++++++++++- test/unit/module/module-collection.spec.js | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index ee1a887ba..095872882 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -49,7 +49,21 @@ export default class ModuleCollection { unregister (path) { const parent = this.get(path.slice(0, -1)) const key = path[path.length - 1] - if (!parent.getChild(key).runtime) return + const child = parent.getChild(key) + + if (!child) { + if (__DEV__) { + console.warn( + `[vuex] trying to unregister module '${key}', which is ` + + `not registered` + ) + } + return + } + + if (!child.runtime) { + return + } parent.removeChild(key) } diff --git a/test/unit/module/module-collection.spec.js b/test/unit/module/module-collection.spec.js index 007989af2..3e8081ce3 100644 --- a/test/unit/module/module-collection.spec.js +++ b/test/unit/module/module-collection.spec.js @@ -92,4 +92,12 @@ describe('ModuleCollection', () => { collection.unregister(['a']) expect(collection.get(['a']).state.value).toBe(true) }) + + it('warns when unregistering non existing module', () => { + const spy = jest.spyOn(console, 'warn').mockImplementation() + + const collection = new ModuleCollection({}) + collection.unregister(['a']) + expect(spy).toHaveBeenCalled() + }) })