|
| 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. |
0 commit comments