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
23 changes: 23 additions & 0 deletions packages/runtime-core/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,27 @@ describe('api: provide/inject', () => {
expect(serialize(root)).toBe(`<div><!----></div>`)
expect(`injection "foo" not found.`).toHaveBeenWarned()
})

it('should not warn when default value is undefined', () => {
const Provider = {
setup() {
return () => h(Middle)
}
}

const Middle = {
render: () => h(Consumer)
}

const Consumer = {
setup() {
const foo = inject('foo', undefined)
return () => foo
}
}

const root = nodeOps.createElement('div')
render(h(Provider), root)
expect(`injection "foo" not found.`).not.toHaveBeenWarned()
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function inject(
if (key in provides) {
// TS doesn't allow symbol as index type
return provides[key as string]
} else if (defaultValue !== undefined) {
} else if (arguments.length > 1) {
return defaultValue
} else if (__DEV__) {
warn(`injection "${String(key)}" not found.`)
Expand Down