Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): false-positive value provider not registered error when the value is undefined #11129

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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