Skip to content

Commit

Permalink
Merge branch 'BrunnerLivio-fix/unknown-dependency-ex-symbol'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 21, 2019
2 parents 6cea592 + 40fb55a commit f95b5ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 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
18 changes: 13 additions & 5 deletions packages/core/errors/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type } from '@nestjs/common';
import { isNil } from '@nestjs/common/utils/shared.utils';
import { isNil, isSymbol } from '@nestjs/common/utils/shared.utils';
import {
InjectorDependency,
InjectorDependencyContext,
Expand All @@ -21,7 +21,15 @@ 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 (symbol)
(isSymbol(dependency) && dependency.toString()) ||
// use string directly
dependency ||
// otherwise
'+';

/**
* Returns the name of the module
* Tries to get the class name. As fallback it returns 'current'.
Expand All @@ -31,15 +39,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 f95b5ae

Please sign in to comment.