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): when the discoverable decorator was not used on calling getProviders/getControllers #12519

Merged
merged 2 commits into from
Oct 23, 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
36 changes: 30 additions & 6 deletions integration/discovery/e2e/discover-by-meta.spec.ts
Original file line number Diff line number Diff line change
@@ -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([
{
Expand All @@ -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([]);
});
});
9 changes: 9 additions & 0 deletions integration/discovery/src/decorators/non-applied.decorator.ts
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 2 additions & 2 deletions packages/core/discovery/discoverable-meta-host-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ export class DiscoverableMetaHostCollection {
metaKey: string,
): Set<InstanceWrapper> {
const wrappersByMetaKey = this.providersByMetaKey.get(hostContainerRef);
return wrappersByMetaKey.get(metaKey);
return wrappersByMetaKey?.get(metaKey) ?? new Set<InstanceWrapper>();
}

public static getControllersByMetaKey(
hostContainerRef: ModulesContainer,
metaKey: string,
): Set<InstanceWrapper> {
const wrappersByMetaKey = this.controllersByMetaKey.get(hostContainerRef);
return wrappersByMetaKey.get(metaKey);
return wrappersByMetaKey?.get(metaKey) ?? new Set<InstanceWrapper>();
}

private static inspectInstanceWrapper(
Expand Down