Skip to content

Commit

Permalink
feat(core): throw an exception instead of logging due to module misusage
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed May 18, 2022
1 parent 79cccd0 commit 1cb7e37
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
3 changes: 1 addition & 2 deletions packages/core/errors/messages.ts
Expand Up @@ -123,8 +123,7 @@ export const USING_INVALID_CLASS_AS_A_MODULE_MESSAGE = (
) => {
const metatypeName = getInstanceName(metatypeUsedAsAModule) || 'found';

// 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.
return `Classes annotated with @Injectable(), @Catch(), and @Controller() decorators must not appear in the "imports" array of a module.
Please remove "${metatypeName}" (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

0 comments on commit 1cb7e37

Please sign in to comment.