Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class DiscoverableMetaHostCollection {
// of `wrapper.metatype` to resolve processor's class properly.
// But since calling `wrapper.instance` could degrade overall performance
// we must defer it as much we can.
instanceWrapper.metatype || instanceWrapper.inject
!instanceWrapper.metatype || instanceWrapper.inject
? (instanceWrapper.instance?.constructor ?? instanceWrapper.metatype)
: instanceWrapper.metatype,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('DiscoverableMetaHostCollection', () => {
expect(result.has(instanceWrapper)).toBe(true);
});

it('should not add provider when metatype is null and inject is not provided (useValue without inject)', () => {
it('should add provider when metatype is null and inject is not provided (useValue without inject)', () => {
const hostContainerRef = new ModulesContainer();
class ValueClass {}
const metaKey = 'value-key';
Expand All @@ -262,13 +262,12 @@ describe('DiscoverableMetaHostCollection', () => {
instanceWrapper,
);

// When metatype is null and inject is not provided, the provider cannot be resolved
// because the code uses `metatype || inject` condition for performance optimization
const result = DiscoverableMetaHostCollection.getProvidersByMetaKey(
hostContainerRef,
metaKey,
);
expect(result.size).toBe(0);
expect(result.size).toBe(1);
expect(result.has(instanceWrapper)).toBe(true);
});

it('should use instance constructor when metatype is null but inject is provided', () => {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/discovery/discovery-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ describe('DiscoveryService', () => {
const providers = discoveryService.getProviders({ include: [] });
expect(providers).toHaveLength(0);
});

it('should discover useValue providers decorated with custom discovery decorator', () => {
const TestDecorator = DiscoveryService.createDecorator();

@TestDecorator()
class TargetService {}

const module1 = new Module(class Module1 {}, modulesContainer as any);
const instance = new TargetService();
const instanceWrapper = new InstanceWrapper({
name: 'ValueProvider',
token: 'VALUE_TOKEN',
metatype: null as any,
instance,
});

DiscoverableMetaHostCollection.inspectProvider(
modulesContainer,
instanceWrapper,
);

module1.providers.set('VALUE_TOKEN', instanceWrapper);
modulesContainer.set('Module1', module1);

const providers = discoveryService.getProviders({
metadataKey: TestDecorator.KEY,
});

expect(providers).toHaveLength(1);
expect(providers).toContain(instanceWrapper);
});
});

describe('getControllers', () => {
Expand Down
Loading