diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index b68ded58090..38fa6b108aa 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -3,6 +3,7 @@ import { hasSymbol } from 'core/util/env' import { warn } from '../util/index' import { defineReactive } from '../observer/index' +import { hasOwn } from 'shared/util' export function initProvide (vm: Component) { const provide = vm.$options.provide @@ -57,6 +58,9 @@ export function resolveInject (inject: any, vm: Component): ?Object { } source = source.$parent } + if (process.env.NODE_ENV !== 'production' && !hasOwn(result, key)) { + warn(`Injection "${key}" not found`, vm) + } } return result } diff --git a/test/unit/features/options/inject.spec.js b/test/unit/features/options/inject.spec.js index fdb69ea1c34..3181a19fcdc 100644 --- a/test/unit/features/options/inject.spec.js +++ b/test/unit/features/options/inject.spec.js @@ -240,4 +240,33 @@ describe('Options provide/inject', () => { `overwritten whenever the provided component re-renders. ` + `injection being mutated: "${key}"`).toHaveBeenWarned() }) + + it('should warn when injections cannot be found', () => { + const vm = new Vue({}) + new Vue({ + parent: vm, + inject: ['foo', 'bar'], + created () {} + }) + expect(`Injection "foo" not found`).toHaveBeenWarned() + expect(`Injection "bar" not found`).toHaveBeenWarned() + }) + + it('should not warn when injections can be found', () => { + const vm = new Vue({ + provide: { + foo: 1, + bar: false, + baz: undefined + } + }) + new Vue({ + parent: vm, + inject: ['foo', 'bar', 'baz'], + created () {} + }) + expect(`Injection "foo" not found`).not.toHaveBeenWarned() + expect(`Injection "bar" not found`).not.toHaveBeenWarned() + expect(`Injection "baz" not found`).not.toHaveBeenWarned() + }) })