From 396f92c78f121301fef1af25197a374443e7da63 Mon Sep 17 00:00:00 2001 From: webfansplz <> Date: Thu, 15 Jul 2021 00:35:58 +0800 Subject: [PATCH 1/6] fix(runtime-core): warning should be triggered when the injectionKey is undefined --- src/apis/inject.ts | 9 +++++---- test/v3/runtime-core/apiInject.spec.ts | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/apis/inject.ts b/src/apis/inject.ts index 44183798..12131eb2 100644 --- a/src/apis/inject.ts +++ b/src/apis/inject.ts @@ -48,10 +48,6 @@ export function inject( defaultValue?: unknown, treatDefaultAsFactory = false ) { - if (!key) { - return defaultValue - } - const vm = getCurrentInstance()?.proxy if (!vm) { __DEV__ && @@ -64,6 +60,11 @@ export function inject( return val } + if (key === undefined) { + __DEV__ && warn(`Injection "${String(key)}" not found`, vm) + return + } + if (defaultValue === undefined && __DEV__) { warn(`Injection "${String(key)}" not found`, vm) } diff --git a/test/v3/runtime-core/apiInject.spec.ts b/test/v3/runtime-core/apiInject.spec.ts index f4593eae..1d45b6ed 100644 --- a/test/v3/runtime-core/apiInject.spec.ts +++ b/test/v3/runtime-core/apiInject.spec.ts @@ -242,6 +242,28 @@ describe('api: provide/inject', () => { expect(`[Vue warn]: Injection "foo" not found`).toHaveBeenWarned() }) + it('should warn unfound w/ injectionKey is undefined', () => { + const Provider = { + setup() { + return () => h(Consumer) + }, + } + + const Consumer = { + setup() { + // Not using TypeScript + const foo = inject(undefined as unknown as string) + expect(foo).toBeUndefined() + return () => h('div', foo as unknown as string) + }, + } + + const root = document.createElement('div') + const vm = createApp(Provider).mount(root) + expect(vm.$el.outerHTML).toBe(`
`) + expect(`[Vue warn]: Injection "undefined" not found`).toHaveBeenWarned() + }) + it('should not self-inject', () => { const Comp = { setup() { From c80b97421779acafcbb8a74a4658cbf7e4976aec Mon Sep 17 00:00:00 2001 From: webfansplz <> Date: Thu, 15 Jul 2021 00:58:52 +0800 Subject: [PATCH 2/6] test(runtime-core): update test case comments --- test/v3/runtime-core/apiInject.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/v3/runtime-core/apiInject.spec.ts b/test/v3/runtime-core/apiInject.spec.ts index 1d45b6ed..f1a72674 100644 --- a/test/v3/runtime-core/apiInject.spec.ts +++ b/test/v3/runtime-core/apiInject.spec.ts @@ -251,7 +251,7 @@ describe('api: provide/inject', () => { const Consumer = { setup() { - // Not using TypeScript + // The emulation does not use TypeScript const foo = inject(undefined as unknown as string) expect(foo).toBeUndefined() return () => h('div', foo as unknown as string) From 80d3fe7f56bdd8299f8dd46eb840de2e9ea6f02a Mon Sep 17 00:00:00 2001 From: webfansplz <> Date: Thu, 15 Jul 2021 01:23:59 +0800 Subject: [PATCH 3/6] fix(runtime-core): warning should be triggered when the injectionKey is undefined --- src/apis/inject.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/apis/inject.ts b/src/apis/inject.ts index 12131eb2..28e013a5 100644 --- a/src/apis/inject.ts +++ b/src/apis/inject.ts @@ -55,16 +55,16 @@ export function inject( return } + if (!key) { + __DEV__ && warn(`Injection "${String(key)}" not found`, vm) + return defaultValue + } + const val = resolveInject(key, vm) if (val !== NOT_FOUND) { return val } - if (key === undefined) { - __DEV__ && warn(`Injection "${String(key)}" not found`, vm) - return - } - if (defaultValue === undefined && __DEV__) { warn(`Injection "${String(key)}" not found`, vm) } From 2c1a3ef055b644a4f7209e9eaedfb62117c65f8a Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 15 Jul 2021 13:54:05 +0800 Subject: [PATCH 4/6] Update src/apis/inject.ts --- src/apis/inject.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apis/inject.ts b/src/apis/inject.ts index 28e013a5..75b53013 100644 --- a/src/apis/inject.ts +++ b/src/apis/inject.ts @@ -56,7 +56,7 @@ export function inject( } if (!key) { - __DEV__ && warn(`Injection "${String(key)}" not found`, vm) + __DEV__ && warn(`injection "${String(key)}" not found.`, vm) return defaultValue } From 4177865109d89fb6d60ced0c543aa562a9c23cfe Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 15 Jul 2021 13:54:42 +0800 Subject: [PATCH 5/6] Update test/v3/runtime-core/apiInject.spec.ts --- test/v3/runtime-core/apiInject.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/v3/runtime-core/apiInject.spec.ts b/test/v3/runtime-core/apiInject.spec.ts index f1a72674..22903ade 100644 --- a/test/v3/runtime-core/apiInject.spec.ts +++ b/test/v3/runtime-core/apiInject.spec.ts @@ -261,7 +261,7 @@ describe('api: provide/inject', () => { const root = document.createElement('div') const vm = createApp(Provider).mount(root) expect(vm.$el.outerHTML).toBe(`
`) - expect(`[Vue warn]: Injection "undefined" not found`).toHaveBeenWarned() + expect(`[Vue warn]: injection "undefined" not found,`).toHaveBeenWarned() }) it('should not self-inject', () => { From 8f6f345044e937bad07bc79999521b83911dcbe1 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 15 Jul 2021 13:58:18 +0800 Subject: [PATCH 6/6] Update test/v3/runtime-core/apiInject.spec.ts --- test/v3/runtime-core/apiInject.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/v3/runtime-core/apiInject.spec.ts b/test/v3/runtime-core/apiInject.spec.ts index 22903ade..1ed7d5cb 100644 --- a/test/v3/runtime-core/apiInject.spec.ts +++ b/test/v3/runtime-core/apiInject.spec.ts @@ -261,7 +261,7 @@ describe('api: provide/inject', () => { const root = document.createElement('div') const vm = createApp(Provider).mount(root) expect(vm.$el.outerHTML).toBe(`
`) - expect(`[Vue warn]: injection "undefined" not found,`).toHaveBeenWarned() + expect(`[Vue warn]: injection "undefined" not found.`).toHaveBeenWarned() }) it('should not self-inject', () => {