diff --git a/integration/discovery/e2e/discover-by-meta.spec.ts b/integration/discovery/e2e/discover-by-meta.spec.ts index 2488c7a0cbe..679c7758430 100644 --- a/integration/discovery/e2e/discover-by-meta.spec.ts +++ b/integration/discovery/e2e/discover-by-meta.spec.ts @@ -1,15 +1,21 @@ -import { Test } from '@nestjs/testing'; +import { Test, TestingModule } from '@nestjs/testing'; +import { DiscoveryService } from '@nestjs/core'; import { expect } from 'chai'; import { AppModule } from '../src/app.module'; import { WebhooksExplorer } from '../src/webhooks.explorer'; +import { NonAppliedDecorator } from '../src/decorators/non-applied.decorator'; describe('DiscoveryModule', () => { - it('should discover all providers & handlers with corresponding annotations', async () => { - const builder = Test.createTestingModule({ + let moduleRef: TestingModule; + + beforeEach(async () => { + moduleRef = await Test.createTestingModule({ imports: [AppModule], - }); - const testingModule = await builder.compile(); - const webhooksExplorer = testingModule.get(WebhooksExplorer); + }).compile(); + }); + + it('should discover all providers & handlers with corresponding annotations', async () => { + const webhooksExplorer = moduleRef.get(WebhooksExplorer); expect(webhooksExplorer.getWebhooks()).to.be.eql([ { @@ -32,4 +38,22 @@ describe('DiscoveryModule', () => { }, ]); }); + + it('should return an empty array if no providers were found for a given discoverable decorator', () => { + const discoveryService = moduleRef.get(DiscoveryService); + + const providers = discoveryService.getProviders({ + metadataKey: NonAppliedDecorator.KEY, + }); + expect(providers).to.be.eql([]); + }); + + it('should return an empty array if no controllers were found for a given discoverable decorator', () => { + const discoveryService = moduleRef.get(DiscoveryService); + + const controllers = discoveryService.getControllers({ + metadataKey: NonAppliedDecorator.KEY, + }); + expect(controllers).to.be.eql([]); + }); }); diff --git a/integration/discovery/src/decorators/non-applied.decorator.ts b/integration/discovery/src/decorators/non-applied.decorator.ts new file mode 100644 index 00000000000..e20a5f932dc --- /dev/null +++ b/integration/discovery/src/decorators/non-applied.decorator.ts @@ -0,0 +1,9 @@ +import { DiscoveryService } from '@nestjs/core'; + +/** + * This decorator must not be used anywhere! + * + * This will be used to test the scenario where we are trying to retrieving + * metadata for a discoverable decorator that was not applied to any class. + */ +export const NonAppliedDecorator = DiscoveryService.createDecorator(); diff --git a/packages/core/discovery/discoverable-meta-host-collection.ts b/packages/core/discovery/discoverable-meta-host-collection.ts index c0642b278da..fd5c79dfaae 100644 --- a/packages/core/discovery/discoverable-meta-host-collection.ts +++ b/packages/core/discovery/discoverable-meta-host-collection.ts @@ -94,7 +94,7 @@ export class DiscoverableMetaHostCollection { metaKey: string, ): Set { const wrappersByMetaKey = this.providersByMetaKey.get(hostContainerRef); - return wrappersByMetaKey.get(metaKey); + return wrappersByMetaKey?.get(metaKey) ?? new Set(); } public static getControllersByMetaKey( @@ -102,7 +102,7 @@ export class DiscoverableMetaHostCollection { metaKey: string, ): Set { const wrappersByMetaKey = this.controllersByMetaKey.get(hostContainerRef); - return wrappersByMetaKey.get(metaKey); + return wrappersByMetaKey?.get(metaKey) ?? new Set(); } private static inspectInstanceWrapper(