Skip to content

Commit ad6dd43

Browse files
Merge pull request nestjs#1416 from BrunnerLivio/refactor/lifecycle-hooks
refactor(core) extract lifecycle hooks
2 parents e4179eb + c735dc6 commit ad6dd43

21 files changed

Lines changed: 595 additions & 202 deletions

integration/hooks/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# dependencies
2+
/node_modules
3+
4+
# IDE
5+
/.idea
6+
/.awcache
7+
/.vscode
8+
9+
# misc
10+
npm-debug.log
11+
12+
# example
13+
/quick-start
14+
15+
# tests
16+
/test
17+
/coverage
18+
/.nyc_output
19+
20+
# dist
21+
/dist
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test } from '@nestjs/testing';
2+
import { expect } from 'chai';
3+
import * as Sinon from 'sinon';
4+
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
5+
6+
@Injectable()
7+
class TestInjectable implements OnApplicationBootstrap {
8+
onApplicationBootstrap = Sinon.spy();
9+
}
10+
11+
describe('OnApplicationBootstrap', () => {
12+
it('should call onApplicationBootstrap when application starts', async () => {
13+
const module = await Test.createTestingModule({
14+
providers: [TestInjectable],
15+
}).compile();
16+
17+
const app = module.createNestApplication();
18+
await app.init();
19+
const instance = module.get(TestInjectable);
20+
expect(instance.onApplicationBootstrap.called).to.be.true;
21+
});
22+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Test } from '@nestjs/testing';
2+
import { expect } from 'chai';
3+
import * as Sinon from 'sinon';
4+
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
5+
import { spawn } from 'child_process';
6+
7+
@Injectable()
8+
class TestInjectable implements OnApplicationShutdown {
9+
onApplicationShutdown = Sinon.spy();
10+
}
11+
12+
describe('OnApplicationShutdown', () => {
13+
it('should call onApplicationShutdown when application closes', async () => {
14+
const module = await Test.createTestingModule({
15+
providers: [TestInjectable],
16+
}).compile();
17+
18+
const app = module.createNestApplication();
19+
await app.close();
20+
const instance = module.get(TestInjectable);
21+
expect(instance.onApplicationShutdown.called).to.be.true;
22+
});
23+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test } from '@nestjs/testing';
2+
import { expect } from 'chai';
3+
import * as Sinon from 'sinon';
4+
import { Injectable, OnModuleDestroy } from '@nestjs/common';
5+
6+
@Injectable()
7+
class TestInjectable implements OnModuleDestroy {
8+
onModuleDestroy = Sinon.spy();
9+
}
10+
11+
describe('OnModuleDestroy', () => {
12+
it('should call onModuleDestroy when application closes', async () => {
13+
const module = await Test.createTestingModule({
14+
providers: [TestInjectable],
15+
}).compile();
16+
17+
const app = module.createNestApplication();
18+
await app.close();
19+
const instance = module.get(TestInjectable);
20+
expect(instance.onModuleDestroy.called).to.be.true;
21+
});
22+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test } from '@nestjs/testing';
2+
import { expect } from 'chai';
3+
import * as Sinon from 'sinon';
4+
import { Injectable, OnModuleInit } from '@nestjs/common';
5+
6+
@Injectable()
7+
class TestInjectable implements OnModuleInit {
8+
onModuleInit = Sinon.spy();
9+
}
10+
11+
describe('OnModuleInit', () => {
12+
it('should call onModuleInit when application starts', async () => {
13+
const module = await Test.createTestingModule({
14+
providers: [TestInjectable],
15+
}).compile();
16+
17+
const app = module.createNestApplication();
18+
await app.init();
19+
const instance = module.get(TestInjectable);
20+
expect(instance.onModuleInit.called).to.be.true;
21+
});
22+
});

integration/hooks/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "nest-typescript-starter",
3+
"version": "1.0.0",
4+
"description": "Nest TypeScript starter repository",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "ts-node src/main"
8+
},
9+
"dependencies": {
10+
"@nestjs/common": "^5.0.0",
11+
"@nestjs/core": "^5.0.0",
12+
"class-transformer": "^0.1.7",
13+
"class-validator": "^0.7.2",
14+
"reflect-metadata": "^0.1.12",
15+
"rxjs": "^6.0.0",
16+
"typescript": "^3.1.0"
17+
},
18+
"devDependencies": {
19+
"@types/node": "^7.0.41",
20+
"supertest": "^3.0.0",
21+
"ts-node": "^6.0.0"
22+
}
23+
}

integration/hooks/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": false,
5+
"noImplicitAny": false,
6+
"removeComments": true,
7+
"noLib": false,
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"target": "es6",
11+
"sourceMap": true,
12+
"allowJs": true,
13+
"outDir": "./dist"
14+
},
15+
"include": [
16+
"src/**/*",
17+
"e2e/**/*"
18+
],
19+
"exclude": [
20+
"node_modules",
21+
]
22+
}

integration/hooks/tslint.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"defaultSeverity": "error",
3+
"extends": [
4+
"tslint:recommended"
5+
],
6+
"jsRules": {
7+
"no-unused-expression": true
8+
},
9+
"rules": {
10+
"eofline": false,
11+
"quotemark": [
12+
true,
13+
"single"
14+
],
15+
"ordered-imports": [
16+
false
17+
],
18+
"max-line-length": [
19+
150
20+
],
21+
"member-ordering": [
22+
false
23+
],
24+
"curly": false,
25+
"interface-name": [
26+
false
27+
],
28+
"array-type": [
29+
false
30+
],
31+
"member-access": [
32+
false
33+
],
34+
"no-empty-interface": false,
35+
"no-empty": false,
36+
"arrow-parens": false,
37+
"object-literal-sort-keys": false,
38+
"no-unused-expression": false,
39+
"max-classes-per-file": [
40+
false
41+
],
42+
"variable-name": [
43+
false
44+
],
45+
"one-line": [
46+
false
47+
],
48+
"one-variable-per-declaration": [
49+
false
50+
]
51+
},
52+
"rulesDirectory": []
53+
}

0 commit comments

Comments
 (0)