Skip to content

Commit

Permalink
Merge pull request #11129 from micalevisk/patch-1
Browse files Browse the repository at this point in the history
fix(core): false-positive value provider not registered error when the value is `undefined`
  • Loading branch information
kamilmysliwiec authored Mar 22, 2023
2 parents 92d5415 + 2bcf434 commit 536d65f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isString,
isSymbol,
isUndefined,
isObject,
} from '@nestjs/common/utils/shared.utils';
import { iterate } from 'iterare';
import { ApplicationConfig } from '../application-config';
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/injector/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 536d65f

Please sign in to comment.