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

feat(core): throw an exception instead of logging due to module import misusage #9596

Merged
merged 1 commit into from May 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/errors/messages.ts
Expand Up @@ -121,11 +121,11 @@ export const USING_INVALID_CLASS_AS_A_MODULE_MESSAGE = (
metatypeUsedAsAModule: Type | ForwardReference,
scope: any[],
) => {
const metatypeName = getInstanceName(metatypeUsedAsAModule) || 'found';
const metatypeNameQuote =
`"${getInstanceName(metatypeUsedAsAModule)}"` || 'that class';

// TODO(v9): Edit the message below:
return `In the next major version, Nest will not allow classes annotated with @Injectable(), @Catch(), and @Controller() decorators to appear in the "imports" array of a module.
Please remove "${metatypeName}" (including forwarded occurrences, if any) from all of the "imports" arrays.
return `Classes annotated with @Injectable(), @Catch(), and @Controller() decorators must not appear in the "imports" array of a module.
Please remove ${metatypeNameQuote} (including forwarded occurrences, if any) from all of the "imports" arrays.

Scope [${stringifyScope(scope)}]
`;
Expand Down
7 changes: 1 addition & 6 deletions packages/core/scanner.ts
Expand Up @@ -2,7 +2,6 @@ import {
DynamicModule,
flatten,
ForwardReference,
Logger,
Provider,
} from '@nestjs/common';
import {
Expand Down Expand Up @@ -59,7 +58,6 @@ interface ApplicationProviderWrapper {
}

export class DependenciesScanner {
private readonly logger = new Logger(DependenciesScanner.name);
private readonly applicationProvidersApplyMap: ApplicationProviderWrapper[] =
[];

Expand Down Expand Up @@ -151,10 +149,7 @@ export class DependenciesScanner {
this.isController(moduleToAdd) ||
this.isExceptionFilter(moduleToAdd)
) {
// TODO(v9): Throw the exception instead of printing a warning
this.logger.warn(
new InvalidClassModuleException(moduleDefinition, scope).message,
);
throw new InvalidClassModuleException(moduleDefinition, scope);
}

return this.container.addModule(moduleToAdd, scope);
Expand Down
35 changes: 13 additions & 22 deletions packages/core/test/scanner.spec.ts
Expand Up @@ -9,6 +9,7 @@ import { Scope } from '../../common/interfaces';
import { ApplicationConfig } from '../application-config';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '../constants';
import { InvalidModuleException } from '../errors/exceptions/invalid-module.exception';
import { InvalidClassModuleException } from '../errors/exceptions/invalid-class-module.exception';
import { UndefinedModuleException } from '../errors/exceptions/undefined-module.exception';
import { NestContainer } from '../injector/container';
import { InstanceWrapper } from '../injector/instance-wrapper';
Expand Down Expand Up @@ -165,16 +166,6 @@ describe('DependenciesScanner', () => {
});

describe('insertModule', () => {
let LoggerWarnSpy: sinon.SinonSpy;

beforeEach(() => {
LoggerWarnSpy = sinon.stub(Logger.prototype, 'warn');
});

afterEach(() => {
LoggerWarnSpy.restore();
});

it('should call forwardRef() when forwardRef property exists', () => {
sinon.stub(container, 'addModule').returns({} as any);

Expand All @@ -183,26 +174,26 @@ describe('DependenciesScanner', () => {

expect(module.forwardRef.called).to.be.true;
});
it('should logs an warning when passing a class annotated with `@Injectable()` decorator', () => {
it('should throw "InvalidClassModuleException" exception when suppling a class annotated with `@Injectable()` decorator', () => {
sinon.stub(container, 'addModule').returns({} as any);

scanner.insertModule(TestComponent, []);

expect(LoggerWarnSpy.calledOnce).to.be.true;
expect(scanner.insertModule(TestComponent, [])).to.be.rejectedWith(
InvalidClassModuleException,
);
});
it('should logs an warning when passing a class annotated with `@Controller()` decorator', () => {
it('should throw "InvalidClassModuleException" exception when suppling a class annotated with `@Controller()` decorator', () => {
sinon.stub(container, 'addModule').returns({} as any);

scanner.insertModule(TestController, []);

expect(LoggerWarnSpy.calledOnce).to.be.true;
expect(scanner.insertModule(TestController, [])).to.be.rejectedWith(
InvalidClassModuleException,
);
});
it('should logs an warning when passing a class annotated with `@Catch()` (only) decorator', () => {
it('should throw "InvalidClassModuleException" exception when suppling a class annotated with (only) `@Catch()` decorator', () => {
sinon.stub(container, 'addModule').returns({} as any);

scanner.insertModule(TestExceptionFilterWithoutInjectable, []);

expect(LoggerWarnSpy.calledOnce).to.be.true;
expect(
scanner.insertModule(TestExceptionFilterWithoutInjectable, []),
).to.be.rejectedWith(InvalidClassModuleException);
});
});

Expand Down