Skip to content

Commit

Permalink
Warn when a inject has been not provided (#5681)
Browse files Browse the repository at this point in the history
* warn when a inject has been not provided

* typo

* typo

* fix when undefined is provided

* use util hasOwn

* refactor

* test case

* Revert "test case"

This reverts commit 08f0a8b.

* test case
  • Loading branch information
maciej-ka authored and yyx990803 committed May 29, 2017
1 parent e274c96 commit b182ac4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/instance/inject.js
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
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()
})
})

0 comments on commit b182ac4

Please sign in to comment.