mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
fix(@nestjs/core): Display Symbol in UnknownDependencyException
Fixes #1551
This commit is contained in:
@@ -5,7 +5,7 @@ import { Module } from '../../injector/module';
|
||||
|
||||
export class UnknownDependenciesException extends RuntimeException {
|
||||
constructor(
|
||||
type: string,
|
||||
type: string | symbol,
|
||||
unknownDependencyContext: InjectorDependencyContext,
|
||||
module?: Module,
|
||||
) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user