diff --git a/packages/core/errors/exceptions/unknown-dependencies.exception.ts b/packages/core/errors/exceptions/unknown-dependencies.exception.ts index 3d8eafb5549..d350952cb4a 100644 --- a/packages/core/errors/exceptions/unknown-dependencies.exception.ts +++ b/packages/core/errors/exceptions/unknown-dependencies.exception.ts @@ -5,7 +5,7 @@ import { Module } from '../../injector/module'; export class UnknownDependenciesException extends RuntimeException { constructor( - type: string, + type: string | symbol, unknownDependencyContext: InjectorDependencyContext, module?: Module, ) { diff --git a/packages/core/errors/messages.ts b/packages/core/errors/messages.ts index b1d07552804..a870db7c7d9 100644 --- a/packages/core/errors/messages.ts +++ b/packages/core/errors/messages.ts @@ -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'. @@ -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); diff --git a/packages/core/injector/injector.ts b/packages/core/injector/injector.ts index dc06ab6c97a..5b670d87102 100644 --- a/packages/core/injector/injector.ts +++ b/packages/core/injector/injector.ts @@ -24,7 +24,7 @@ import { Module } from './module'; /** * The type of an injectable dependency */ -export type InjectorDependency = Type | Function | string; +export type InjectorDependency = Type | Function | string | symbol; /** * The property-based dependency @@ -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 */ diff --git a/packages/core/test/errors/test/messages.spec.ts b/packages/core/test/errors/test/messages.spec.ts index fbb6fce3f07..161b035dc8c 100644 --- a/packages/core/test/errors/test/messages.spec.ts +++ b/packages/core/test/errors/test/messages.spec.ts @@ -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); + }); });