Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/instance/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})