Skip to content

Commit a165ddb

Browse files
committed
fix(core): Do not call onModuleInit if is null
Fixes nestjs#1252
1 parent baa2000 commit a165ddb

7 files changed

Lines changed: 74 additions & 8 deletions

File tree

integration/hooks/e2e/on-app-boostrap.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ describe('OnApplicationBootstrap', () => {
1919
const instance = module.get(TestInjectable);
2020
expect(instance.onApplicationBootstrap.called).to.be.true;
2121
});
22+
23+
it('should not throw an error when onApplicationBootstrap is null', async () => {
24+
const module = await Test.createTestingModule({
25+
providers: [
26+
{ provide: 'TEST', useValue: { onApplicationBootstrap: null } }
27+
],
28+
}).compile();
29+
30+
const app = module.createNestApplication();
31+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
32+
});
33+
34+
it('should not throw an error when onApplicationBootstrap is undefined', async () => {
35+
const module = await Test.createTestingModule({
36+
providers: [
37+
{ provide: 'TEST', useValue: { onApplicationBootstrap: undefined } }
38+
],
39+
}).compile();
40+
41+
const app = module.createNestApplication();
42+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
43+
});
2244
});

integration/hooks/e2e/on-module-destroy.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ describe('OnModuleDestroy', () => {
1919
const instance = module.get(TestInjectable);
2020
expect(instance.onModuleDestroy.called).to.be.true;
2121
});
22+
23+
it('should not throw an error when onModuleDestroy is null', async () => {
24+
const module = await Test.createTestingModule({
25+
providers: [
26+
{ provide: 'TEST', useValue: { onModuleDestroy: null } }
27+
],
28+
}).compile();
29+
30+
const app = module.createNestApplication();
31+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
32+
});
33+
34+
it('should not throw an error when onModuleDestroy is undefined', async () => {
35+
const module = await Test.createTestingModule({
36+
providers: [
37+
{ provide: 'TEST', useValue: { onModuleDestroy: undefined } }
38+
],
39+
}).compile();
40+
41+
const app = module.createNestApplication();
42+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
43+
});
2244
});

integration/hooks/e2e/on-module-init.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ describe('OnModuleInit', () => {
1919
const instance = module.get(TestInjectable);
2020
expect(instance.onModuleInit.called).to.be.true;
2121
});
22+
23+
it('should not throw an error when onModuleInit is null', async () => {
24+
const module = await Test.createTestingModule({
25+
providers: [
26+
{ provide: 'TEST', useValue: { onModuleInit: null } }
27+
],
28+
}).compile();
29+
30+
const app = module.createNestApplication();
31+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
32+
});
33+
34+
it('should not throw an error when onModuleInit is undefined', async () => {
35+
const module = await Test.createTestingModule({
36+
providers: [
37+
{ provide: 'TEST', useValue: { onModuleInit: undefined } }
38+
],
39+
}).compile();
40+
41+
const app = module.createNestApplication();
42+
await app.init().then((obj) => expect(obj).to.not.be.undefined);
43+
});
2244
});

packages/core/hooks/on-app-bootstrap.hook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OnApplicationBootstrap } from '@nestjs/common';
2-
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
2+
import { isNil } from '@nestjs/common/utils/shared.utils';
33
import iterate from 'iterare';
44
import { InstanceWrapper } from '../injector/instance-wrapper';
55
import { Module } from '../injector/module';
@@ -16,7 +16,7 @@ import {
1616
function hasOnAppBootstrapHook(
1717
instance: unknown,
1818
): instance is OnApplicationBootstrap {
19-
return !isUndefined(
19+
return !isNil(
2020
(instance as OnApplicationBootstrap).onApplicationBootstrap,
2121
);
2222
}

packages/core/hooks/on-app-shutdown.hook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OnApplicationShutdown } from '@nestjs/common';
2-
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
2+
import { isNil } from '@nestjs/common/utils/shared.utils';
33
import iterate from 'iterare';
44
import { InstanceWrapper } from '../injector/instance-wrapper';
55
import { Module } from '../injector/module';
@@ -16,7 +16,7 @@ import {
1616
function hasOnAppBootstrapHook(
1717
instance: unknown,
1818
): instance is OnApplicationShutdown {
19-
return !isUndefined(
19+
return !isNil(
2020
(instance as OnApplicationShutdown).onApplicationShutdown,
2121
);
2222
}

packages/core/hooks/on-module-destroy.hook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OnModuleDestroy } from '@nestjs/common';
2-
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
2+
import { isNil } from '@nestjs/common/utils/shared.utils';
33
import iterate from 'iterare';
44
import { InstanceWrapper } from '../injector/instance-wrapper';
55
import { Module } from '../injector/module';
@@ -16,7 +16,7 @@ import {
1616
function hasOnModuleDestroyHook(
1717
instance: unknown,
1818
): instance is OnModuleDestroy {
19-
return !isUndefined((instance as OnModuleDestroy).onModuleDestroy);
19+
return !isNil((instance as OnModuleDestroy).onModuleDestroy);
2020
}
2121

2222
/**

packages/core/hooks/on-module-init.hook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OnModuleInit } from '@nestjs/common';
2-
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
2+
import { isNil } from '@nestjs/common/utils/shared.utils';
33
import iterate from 'iterare';
44
import { InstanceWrapper } from '../injector/instance-wrapper';
55
import { Module } from '../injector/module';
@@ -14,7 +14,7 @@ import {
1414
* @param instance The instance which should be checked
1515
*/
1616
function hasOnModuleInitHook(instance: unknown): instance is OnModuleInit {
17-
return !isUndefined((instance as OnModuleInit).onModuleInit);
17+
return !isNil((instance as OnModuleInit).onModuleInit);
1818
}
1919

2020
/**

0 commit comments

Comments
 (0)