From eead379dad7082a72addeeea85ea072183d16ab7 Mon Sep 17 00:00:00 2001 From: Thiago Martins Date: Mon, 4 Dec 2023 10:02:44 -0300 Subject: [PATCH] docs: add docs for enforce-close-testing-module closes #8 --- .prettierignore | 3 +- README.md | 4 +- docs/rules/TEMPLATE.md | 26 +++++++ docs/rules/enforce-close-testing-module.md | 87 ++++++++++++++++++++++ package.json | 2 +- 5 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 docs/rules/TEMPLATE.md create mode 100644 docs/rules/enforce-close-testing-module.md diff --git a/.prettierignore b/.prettierignore index d132439..c3282a6 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,5 @@ node_modules lib .pnp.* -.vscode/ \ No newline at end of file +.vscode/ +.github/* \ No newline at end of file diff --git a/README.md b/README.md index 8588450..0fd563e 100644 --- a/README.md +++ b/README.md @@ -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. - diff --git a/docs/rules/TEMPLATE.md b/docs/rules/TEMPLATE.md new file mode 100644 index 0000000..c8f82d7 --- /dev/null +++ b/docs/rules/TEMPLATE.md @@ -0,0 +1,26 @@ +--- +description: '' +--- + +## Examples + +To fill out: tell us more about this rule. + + + +### ❌ 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. diff --git a/docs/rules/enforce-close-testing-module.md b/docs/rules/enforce-close-testing-module.md new file mode 100644 index 0000000..e551edc --- /dev/null +++ b/docs/rules/enforce-close-testing-module.md @@ -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. diff --git a/package.json b/package.json index d24c698..ea2e1bb 100644 --- a/package.json +++ b/package.json @@ -33,4 +33,4 @@ "tsx": "^4.6.2", "typescript": "^5.3.2" } -} \ No newline at end of file +}