diff --git a/packages/core/injector/module.ts b/packages/core/injector/module.ts index 974fd50c336..8d04bb452c3 100644 --- a/packages/core/injector/module.ts +++ b/packages/core/injector/module.ts @@ -22,6 +22,7 @@ import { isString, isSymbol, isUndefined, + isObject, } from '@nestjs/common/utils/shared.utils'; import { iterate } from 'iterare'; import { ApplicationConfig } from '../application-config'; @@ -344,7 +345,10 @@ export class Module { } public isCustomValue(provider: any): provider is ValueProvider { - return !isUndefined((provider as ValueProvider).useValue); + return ( + isObject(provider) && + Object.prototype.hasOwnProperty.call(provider, 'useValue') + ); } public isCustomFactory(provider: any): provider is FactoryProvider { diff --git a/packages/core/test/injector/module.spec.ts b/packages/core/test/injector/module.spec.ts index 6cc06aae223..fb8665d1590 100644 --- a/packages/core/test/injector/module.spec.ts +++ b/packages/core/test/injector/module.spec.ts @@ -137,6 +137,16 @@ describe('Module', () => { expect((addCustomValue as sinon.SinonSpy).called).to.be.true; }); + it('should call "addCustomValue" when "useValue" property exists but its value is `undefined`', () => { + const addCustomValue = sinon.spy(); + module.addCustomValue = addCustomValue; + + const provider = { provide: 'test', useValue: undefined }; + + module.addCustomProvider(provider as any, new Map()); + expect((addCustomValue as sinon.SinonSpy).called).to.be.true; + }); + it('should call "addCustomFactory" when "useFactory" property exists', () => { const addCustomFactory = sinon.spy(); module.addCustomFactory = addCustomFactory;