nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.9k stars 206 forks source link

Jest did not exit one second after the test run has completed #2050

Closed andkom closed 4 weeks ago

andkom commented 1 month ago

Is there an existing issue for this?

Current behavior

Jest did not exit one second after the test run has completed if there are more then one test in the file:

test.spec.ts

import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';

describe('tests', () => {
  let app: INestApplication;
  let moduleRef: TestingModule;

  beforeEach(async () => {
    moduleRef = await Test.createTestingModule({
      imports: [
        TypeOrmModule.forRoot({
          type: 'mysql',
          url: 'mysql://api:api@127.0.0.1/api',
        }),
      ],
      providers: [],
    }).compile();

    app = moduleRef.createNestApplication();

    await app.init();
  });

  afterAll(async () => {
    await moduleRef.get(DataSource).destroy();
    await moduleRef.close();
    await app.close();
  });

  it('test 1', () => {
    expect(true).toBe(true);
  });

  it('test 2', () => {
    expect(false).toBe(false);
  });

  it('test 3', () => {
    expect(1).toBe(1);
  });
});

output:

 tests
    ✓ test 1 (126 ms)
    ✓ test 2 (7 ms)
    ✓ test 3 (5 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.856 s, estimated 2 s
Ran all test suites.
Jest did not exit one second after the test run has completed.

'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

If i comment any two tests or TypeOrmModule import, test will run and exit properly.

Minimum reproduction code

https://github.com/andkom/jest-nest-typeorm/blob/main/src/test.spec.ts

Steps to reproduce

  1. git clone https://github.com/andkom/jest-nest-typeorm
  2. npm install
  3. jest

Expected behavior

there are no open handles after test

Package version

10.0.2

NestJS version

10.4.1

Node.js version

20.12.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 4 weeks ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.