Skip to content

Commit

Permalink
fix(@nestjs/core): Display Symbol in UnknownDependencyException
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Feb 16, 2019
1 parent 1714b51 commit 3261bed
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Module } from '../../injector/module';

export class UnknownDependenciesException extends RuntimeException {
constructor(
type: string,
type: string | symbol,
unknownDependencyContext: InjectorDependencyContext,
module?: Module,
) {
Expand Down
14 changes: 10 additions & 4 deletions packages/core/errors/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const getInstanceName = (instance: any) =>
* @param dependency The dependency whichs name should get displayed
*/
const getDependencyName = (dependency: InjectorDependency) =>
getInstanceName(dependency) || dependency || '+';
// Use class name
getInstanceName(dependency) ||
// Use injection token
dependency && dependency.toString() ||
// Don't know, don't care
'+';

/**
* Returns the name of the module
* Tries to get the class name. As fallback it returns 'current'.
Expand All @@ -31,15 +37,15 @@ const getModuleName = (module: Module) =>
(module && getInstanceName(module.metatype)) || 'current';

export const UNKNOWN_DEPENDENCIES_MESSAGE = (
type: string,
type: string | symbol,
unknownDependencyContext: InjectorDependencyContext,
module: Module,
) => {
const { index, dependencies, key } = unknownDependencyContext;
let message = `Nest can't resolve dependencies of the ${type}`;
let message = `Nest can't resolve dependencies of the ${type.toString()}`;

if (isNil(index)) {
message += `. Please make sure that the "${key}" property is available in the current context.`;
message += `. Please make sure that the "${key.toString()}" property is available in the current context.`;
return message;
}
const dependenciesName = (dependencies || []).map(getDependencyName);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Module } from './module';
/**
* The type of an injectable dependency
*/
export type InjectorDependency = Type<any> | Function | string;
export type InjectorDependency = Type<any> | Function | string | symbol;

/**
* The property-based dependency
Expand All @@ -44,7 +44,7 @@ export interface InjectorDependencyContext {
/**
* The name of the property key (property-based injection)
*/
key?: string;
key?: string | symbol;
/**
* The name of the function or injection token
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/errors/test/messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ describe('UnknownDependenciesMessage', () => {
myModule.metatype = myMetaType;
expect(new UnknownDependenciesException('CatService', { index, dependencies: ['', 'MY_TOKEN'] }, myModule as Module).message).to.equal(expectedResult);
});
it('should display the symbol name of the provider', () => {
const expectedResult = 'Nest can\'t resolve dependencies of the Symbol(CatProvider) (?). ' +
'Please make sure that the argument at index [0] is available in the current context.';
expect(new UnknownDependenciesException(Symbol('CatProvider'), { index, dependencies: [''] }).message).to.equal(expectedResult);
});
it('should display the symbol dependency of the provider', () => {
const expectedResult = 'Nest can\'t resolve dependencies of the CatProvider (?, Symbol(DogProvider)). ' +
'Please make sure that the argument at index [0] is available in the current context.';
expect(new UnknownDependenciesException('CatProvider', { index, dependencies: ['', Symbol('DogProvider')] }).message).to.equal(expectedResult);
});
});

0 comments on commit 3261bed

Please sign in to comment.