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

docs: add docs for enforce-close-testing-module #9

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -2,4 +2,5 @@
node_modules
lib
.pnp.*
.vscode/
.vscode/
.github/*
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Trilon eslint-plugin

# Trilon eslint-plugin
[![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)


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.

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

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.

26 changes: 26 additions & 0 deletions docs/rules/TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
description: '<Description from rule metadata here>'
---

## Examples

To fill out: tell us more about this rule.

<!--tabs-->

### ❌ Incorrect

```ts
// To fill out: incorrect code
```

### ✅ Correct

```ts
// To fill out: correct code
```

## When Not To Use It

To fill out: why wouldn't you want to use this rule?
For example if this rule requires a feature released in a certain TS version.
87 changes: 87 additions & 0 deletions docs/rules/enforce-close-testing-module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
description: 'Ensure NestJS testing modules are closed properly'
---

[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.

## Examples

### ❌ Incorrect

```ts
describe('Creates a testingModule in the "beforeEach" hook but does not close it', () => {
let testingModule: TestingModule;
beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
});

it('should be defined', () => {
expect(testingModule).toBeDefined();
});
});

describe('Creates a testingModule in the "beforeEach" hook but closes it in the "afterAll"', () => {
let testingModule: TestingModule;
beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
});

it('should be defined', () => {
expect(testingModule).toBeDefined();
});

afterAll(async () => {
await testingModule.close();
});
});
```

### ✅ Correct

```ts
describe('Closes the testingModule in the "afterEach" hook', () => {
let testingModule: TestingModule;

beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
});

afterEach(async () => {
await testingModule.close();
});

it('should be defined', () => {
expect(testingModule).toBeDefined();
});
});

describe('Closes the appModule created from the testingModule', () => {
let app: INestApplication;
beforeEach(async () => {
const testingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = testingModule.createNestApplication();
});

it('should be defined', () => {
expect(testingModule).toBeDefined();
});

afterEach(async () => {
await app.close();
});
});
```

## When Not To Use It

If you don't use testing modules, you can disable this rule. Moreover, you can also enable this rule
only for files ending with `.spec.ts` or `.e2e-spec.ts`, so you can create utility functions in other files
that create testing modules without closing them.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -33,4 +33,4 @@
"tsx": "^4.6.2",
"typescript": "^5.3.2"
}
}
}