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): Do not call onModuleInit if is null #2790

Merged
merged 1 commit into from
Aug 24, 2019
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
22 changes: 22 additions & 0 deletions integration/hooks/e2e/on-app-boostrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ describe('OnApplicationBootstrap', () => {
const instance = module.get(TestInjectable);
expect(instance.onApplicationBootstrap.called).to.be.true;
});

it('should not throw an error when onApplicationBootstrap is null', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onApplicationBootstrap: null } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});

it('should not throw an error when onApplicationBootstrap is undefined', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onApplicationBootstrap: undefined } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});
});
22 changes: 22 additions & 0 deletions integration/hooks/e2e/on-module-destroy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ describe('OnModuleDestroy', () => {
const instance = module.get(TestInjectable);
expect(instance.onModuleDestroy.called).to.be.true;
});

it('should not throw an error when onModuleDestroy is null', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onModuleDestroy: null } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});

it('should not throw an error when onModuleDestroy is undefined', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onModuleDestroy: undefined } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});
});
22 changes: 22 additions & 0 deletions integration/hooks/e2e/on-module-init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ describe('OnModuleInit', () => {
const instance = module.get(TestInjectable);
expect(instance.onModuleInit.called).to.be.true;
});

it('should not throw an error when onModuleInit is null', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onModuleInit: null } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});

it('should not throw an error when onModuleInit is undefined', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onModuleInit: undefined } }
],
}).compile();

const app = module.createNestApplication();
await app.init().then((obj) => expect(obj).to.not.be.undefined);
});
});
4 changes: 2 additions & 2 deletions packages/core/hooks/on-app-bootstrap.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnApplicationBootstrap } from '@nestjs/common';
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { isNil } from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { InstanceWrapper } from '../injector/instance-wrapper';
import { Module } from '../injector/module';
Expand All @@ -16,7 +16,7 @@ import {
function hasOnAppBootstrapHook(
instance: unknown,
): instance is OnApplicationBootstrap {
return !isUndefined(
return !isNil(
(instance as OnApplicationBootstrap).onApplicationBootstrap,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/hooks/on-app-shutdown.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnApplicationShutdown } from '@nestjs/common';
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { isNil } from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { InstanceWrapper } from '../injector/instance-wrapper';
import { Module } from '../injector/module';
Expand All @@ -16,7 +16,7 @@ import {
function hasOnAppBootstrapHook(
instance: unknown,
): instance is OnApplicationShutdown {
return !isUndefined(
return !isNil(
(instance as OnApplicationShutdown).onApplicationShutdown,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/hooks/on-module-destroy.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnModuleDestroy } from '@nestjs/common';
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { isNil } from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { InstanceWrapper } from '../injector/instance-wrapper';
import { Module } from '../injector/module';
Expand All @@ -16,7 +16,7 @@ import {
function hasOnModuleDestroyHook(
instance: unknown,
): instance is OnModuleDestroy {
return !isUndefined((instance as OnModuleDestroy).onModuleDestroy);
return !isNil((instance as OnModuleDestroy).onModuleDestroy);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/hooks/on-module-init.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnModuleInit } from '@nestjs/common';
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { isNil } from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { InstanceWrapper } from '../injector/instance-wrapper';
import { Module } from '../injector/module';
Expand All @@ -14,7 +14,7 @@ import {
* @param instance The instance which should be checked
*/
function hasOnModuleInitHook(instance: unknown): instance is OnModuleInit {
return !isUndefined((instance as OnModuleInit).onModuleInit);
return !isNil((instance as OnModuleInit).onModuleInit);
}

/**
Expand Down