Skip to content

Commit 18e5179

Browse files
authored
Merge pull request #9 from TrilonIO/docs/enforce-close-testing-module
docs: add docs for enforce-close-testing-module
2 parents b7e78e4 + eead379 commit 18e5179

5 files changed

Lines changed: 117 additions & 5 deletions

File tree

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
node_modules
33
lib
44
.pnp.*
5-
.vscode/
5+
.vscode/
6+
.github/*

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
# Trilon eslint-plugin
12

2-
# Trilon eslint-plugin
33
[![Node.js CI](https://github.com/TrilonIO/eslint-plugin/actions/workflows/node-ci.yml/badge.svg)](https://github.com/TrilonIO/eslint-plugin/actions/workflows/node-ci.yml)
44

5-
65
At Trilon, our goal is to help elevate teams - giving them the push they need to continuously succeed in today's ever-changing tech world.
76

87
As part of that, we focus on developing tools that make **your** dev experience easier, enjoyable, and safer.
98

109
The official Trilon Eslint Plugin is part of that toolbelt to help your team to thrive, applying best practices for NestJS, curated by our key contributors and core team.
11-

docs/rules/TEMPLATE.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
description: '<Description from rule metadata here>'
3+
---
4+
5+
## Examples
6+
7+
To fill out: tell us more about this rule.
8+
9+
<!--tabs-->
10+
11+
### ❌ Incorrect
12+
13+
```ts
14+
// To fill out: incorrect code
15+
```
16+
17+
### ✅ Correct
18+
19+
```ts
20+
// To fill out: correct code
21+
```
22+
23+
## When Not To Use It
24+
25+
To fill out: why wouldn't you want to use this rule?
26+
For example if this rule requires a feature released in a certain TS version.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
description: 'Ensure NestJS testing modules are closed properly'
3+
---
4+
5+
[Testing modules](https://docs.nestjs.com/fundamentals/testing#testing-utilities) are generally used to mimic the behavior of underlying services and modules, allowing the developer to override and configure them for testing purposes. However, if the testing module is not closed properly, it can cause many issues, such as memory leaks, hanging processes and open database connections. This rule ensures that all testing modules are closed properly - and also closed in the correct hook.
6+
7+
## Examples
8+
9+
### ❌ Incorrect
10+
11+
```ts
12+
describe('Creates a testingModule in the "beforeEach" hook but does not close it', () => {
13+
let testingModule: TestingModule;
14+
beforeEach(async () => {
15+
testingModule = await Test.createTestingModule({
16+
imports: [AppModule],
17+
}).compile();
18+
});
19+
20+
it('should be defined', () => {
21+
expect(testingModule).toBeDefined();
22+
});
23+
});
24+
25+
describe('Creates a testingModule in the "beforeEach" hook but closes it in the "afterAll"', () => {
26+
let testingModule: TestingModule;
27+
beforeEach(async () => {
28+
testingModule = await Test.createTestingModule({
29+
imports: [AppModule],
30+
}).compile();
31+
});
32+
33+
it('should be defined', () => {
34+
expect(testingModule).toBeDefined();
35+
});
36+
37+
afterAll(async () => {
38+
await testingModule.close();
39+
});
40+
});
41+
```
42+
43+
### ✅ Correct
44+
45+
```ts
46+
describe('Closes the testingModule in the "afterEach" hook', () => {
47+
let testingModule: TestingModule;
48+
49+
beforeEach(async () => {
50+
testingModule = await Test.createTestingModule({
51+
imports: [AppModule],
52+
}).compile();
53+
});
54+
55+
afterEach(async () => {
56+
await testingModule.close();
57+
});
58+
59+
it('should be defined', () => {
60+
expect(testingModule).toBeDefined();
61+
});
62+
});
63+
64+
describe('Closes the appModule created from the testingModule', () => {
65+
let app: INestApplication;
66+
beforeEach(async () => {
67+
const testingModule = await Test.createTestingModule({
68+
imports: [AppModule],
69+
}).compile();
70+
app = testingModule.createNestApplication();
71+
});
72+
73+
it('should be defined', () => {
74+
expect(testingModule).toBeDefined();
75+
});
76+
77+
afterEach(async () => {
78+
await app.close();
79+
});
80+
});
81+
```
82+
83+
## When Not To Use It
84+
85+
If you don't use testing modules, you can disable this rule. Moreover, you can also enable this rule
86+
only for files ending with `.spec.ts` or `.e2e-spec.ts`, so you can create utility functions in other files
87+
that create testing modules without closing them.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
"tsx": "^4.6.2",
3434
"typescript": "^5.3.2"
3535
}
36-
}
36+
}

0 commit comments

Comments
 (0)