Skip to content

Commit

Permalink
fix(context): allow optional for a binding without value getter
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 26, 2019
1 parent 1143b5d commit e211a71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/context/src/__tests__/unit/binding.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ describe('Binding', () => {
);
});

it('allows optional if binding does not have a value getter', () => {
// This can happen for `@inject.binding`
ctx.bind('child.options');
const childOptions = ctx.getSync('child.options', {optional: true});
expect(childOptions).to.be.undefined();
});

it('allows optional if alias binding cannot be resolved', () => {
ctx.bind('child.options').toAlias('parent.options#child');
const childOptions = ctx.getSync('child.options', {optional: true});
Expand Down
2 changes: 2 additions & 0 deletions packages/context/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export class Binding<T = BoundValue> {
);
return this._cacheValue(ctx, result);
}
// `@inject.binding` adds a binding without _getValue
if (options.optional) return undefined;
return Promise.reject(
new Error(`No value was configured for binding ${this.key}.`),
);
Expand Down

0 comments on commit e211a71

Please sign in to comment.