Skip to content

Commit

Permalink
docs: update enforce-close-testing-module docs to include options
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomini committed Dec 15, 2023
1 parent dcdd13b commit ee88e5f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions docs/rules/enforce-close-testing-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ 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.

## Options

This rule accepts an object with two properties: `createAliases` and `closeAliases`. Each property is an array of objects, where each object specifies a `kind` (either 'function' or 'method') and a `name` (the name of the function or method).

- `createAliases`: Defines functions or methods that behave similarly to `Test.createTestingModule()`.
- `closeAliases`: Defines functions or methods that are equivalent to `TestingModule.close()`.

### Example of Options

```json
{
"nestjs/close-testing-modules": ["error", {
"createAliases": [
{ "kind": "function", "name": "customCreateTestingModule" },
{ "kind": "method", "name": "alternativeCreateMethod" }
],
"closeAliases": [
{ "kind": "method", "name": "customCloseMethod" }
]
}]
}
```

## Examples

### ❌ Incorrect
Expand Down Expand Up @@ -78,6 +101,42 @@ describe('Closes the appModule created from the testingModule', () => {
await app.close();
});
});

describe('Creates and closes the appModule using custom functions', () => {
let app: INestApplication;
beforeEach(async () => {
// defined via the "createAliases" option as { kind: 'function', name: 'createTestingModule' }
const testingModule = await createTestingModule();
app = testingModule.createNestApplication();
});

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

afterEach(async () => {
// defined via the "closeAliases" option as { kind: 'function', name: 'closeTestingModule' }
await closeTestingModule(testingModule);
});
});

describe('Creates and closes the appModule using custom methods', () => {
let app: INestApplication;
beforeEach(async () => {
// defined via the "createAliases" option as { kind: 'method', name: 'createTestingModule' }
const testingModule = await testUtils.createTestingModule();
app = testingModule.createNestApplication();
});

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

afterEach(async () => {
// defined via the "closeAliases" option as { kind: 'method', name: 'close' }
await testUtils.close(testingModule);
});
});
```

## When Not To Use It
Expand Down
2 changes: 1 addition & 1 deletion src/rules/enforce-close-testing-module.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default createRule<Options, MessageIds>({
},
},
},
], // no options
],
messages: {
testModuleNotClosed:
'A Testing Module was created but not closed, which can cause memory leaks',
Expand Down

0 comments on commit ee88e5f

Please sign in to comment.