Skip to content

Commit

Permalink
feat(context): reports the resolution path for circular deps
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 6, 2018
1 parent 8fae758 commit bc4ce20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/context/src/resolution-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ export class ResolutionSession {
debugSession('Enter binding:', binding.toJSON());
}
if (this.stack.find(i => i.type === 'binding' && i.value === binding)) {
throw new Error(
`Circular dependency detected on path '${this.getBindingPath()} --> ${
binding.key
}'`,
);
const msg =
`Circular dependency detected: ` +
`${this.getResolutionPath()} --> ${binding.key}`;
debugSession(msg);
throw new Error(msg);
}
this.stack.push({type: 'binding', value: binding});
/* istanbul ignore if */
Expand Down
15 changes: 10 additions & 5 deletions packages/context/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ describe('constructor injection', () => {
context.bind('x').toClass(XClass);
context.bind('y').toClass(YClass);
expect(() => context.getSync('x')).to.throw(
"Circular dependency detected on path 'x --> y --> x'",
'Circular dependency detected: x --> @XClass.prototype.y ' +
'--> y --> @YClass.prototype.x --> x',
);
expect(() => context.getSync('y')).to.throw(
"Circular dependency detected on path 'y --> x --> y'",
'Circular dependency detected: y --> @YClass.prototype.x ' +
'--> x --> @XClass.prototype.y --> y',
);
});

Expand Down Expand Up @@ -138,13 +140,16 @@ describe('constructor injection', () => {
context.bind('y').toClass(YClass);
context.bind('z').toClass(ZClass);
expect(() => context.getSync('x')).to.throw(
"Circular dependency detected on path 'x --> y --> z --> x'",
'Circular dependency detected: x --> @XClass.constructor[0] --> y ' +
'--> @YClass.constructor[0] --> z --> @ZClass.constructor[0] --> x',
);
expect(() => context.getSync('y')).to.throw(
"Circular dependency detected on path 'y --> z --> x --> y'",
'Circular dependency detected: y --> @YClass.constructor[0] --> z ' +
'--> @ZClass.constructor[0] --> x --> @XClass.constructor[0] --> y',
);
expect(() => context.getSync('z')).to.throw(
"Circular dependency detected on path 'z --> x --> y --> z'",
'Circular dependency detected: z --> @ZClass.constructor[0] --> x ' +
'--> @XClass.constructor[0] --> y --> @YClass.constructor[0] --> z',
);
});

Expand Down

0 comments on commit bc4ce20

Please sign in to comment.