diff --git a/packages/core/errors/messages.ts b/packages/core/errors/messages.ts index 20926035929..b0a26e8d1d3 100644 --- a/packages/core/errors/messages.ts +++ b/packages/core/errors/messages.ts @@ -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)}] `; diff --git a/packages/core/scanner.ts b/packages/core/scanner.ts index 63d02a6c25a..1e2cb009bb1 100644 --- a/packages/core/scanner.ts +++ b/packages/core/scanner.ts @@ -2,7 +2,6 @@ import { DynamicModule, flatten, ForwardReference, - Logger, Provider, } from '@nestjs/common'; import { @@ -59,7 +58,6 @@ interface ApplicationProviderWrapper { } export class DependenciesScanner { - private readonly logger = new Logger(DependenciesScanner.name); private readonly applicationProvidersApplyMap: ApplicationProviderWrapper[] = []; @@ -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); diff --git a/packages/core/test/scanner.spec.ts b/packages/core/test/scanner.spec.ts index 2fdad2cc297..f2ef6b2acc0 100644 --- a/packages/core/test/scanner.spec.ts +++ b/packages/core/test/scanner.spec.ts @@ -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'; @@ -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); @@ -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); }); });