Webeleon / unit-testing-nestjs-using-typeorm-in-memory

a dev.to article on unit testing nestjs project using typeorm and in memory db
5 stars 1 forks source link

The connection remains open #22

Open Edo78 opened 2 years ago

Edo78 commented 2 years ago

If I add even another simple test like

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

to src/spaceships/spaceships.service.spec.ts then I start to get an error

[Nest] 559866  - 20/11/2021, 23:13:03   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
    at AlreadyHasActiveConnectionError.TypeORMError [as constructor] (/tmp/unit-testing-nestjs-using-typeorm-in-memory/src/error/TypeORMError.ts:7:9)
    at new AlreadyHasActiveConnectionError (/tmp/unit-testing-nestjs-using-typeorm-in-memory/src/error/AlreadyHasActiveConnectionError.ts:8:9)
    at ConnectionManager.Object.<anonymous>.ConnectionManager.create (/tmp/unit-testing-nestjs-using-typeorm-in-memory/src/connection/ConnectionManager.ts:57:23)
    at /tmp/unit-testing-nestjs-using-typeorm-in-memory/src/globals.ts:77:35
    at step (/tmp/unit-testing-nestjs-using-typeorm-in-memory/node_modules/tslib/tslib.js:143:27)
    at Object.next (/tmp/unit-testing-nestjs-using-typeorm-in-memory/node_modules/tslib/tslib.js:124:57)
    at /tmp/unit-testing-nestjs-using-typeorm-in-memory/node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/tmp/unit-testing-nestjs-using-typeorm-in-memory/node_modules/tslib/tslib.js:113:16)
    at createConnection (/tmp/unit-testing-nestjs-using-typeorm-in-memory/node_modules/typeorm/globals.js:55:20)

until the test goes in timeout.

You should add an afterEach to close the module.

Let me know if you'd like that I write a PR

theshadowsvk commented 2 years ago

@Edo78 I also have this problem, can you assist me how to properly close the module as you mentioned ?

Edo78 commented 2 years ago

Sure thing.

import { Test, TestingModule } from '@nestjs/testing';
import { SpaceshipsService } from './spaceships.service';
import { TypeOrmSQLITETestingModule } from '../test-utils/TypeOrmSQLITETestingModule';
import { testDatasetSeed } from '../test-utils/testDataset.seed';

describe('SpaceshipsService', () => {
  let service: SpaceshipsService;
  // first you move the declaration of module
  let module: TestingModule;

  beforeEach(async () => {
    module = await Test.createTestingModule({
      imports: [...TypeOrmSQLITETestingModule()],
      providers: [SpaceshipsService],
    }).compile();

    service = module.get<SpaceshipsService>(SpaceshipsService);
    await testDatasetSeed();
  });

  // then you close the module after each test
  afterEach(() => {
    module.close();
  });

  it('listSpaceships', async () => {
    const spaceships = await service.listSpaceships();
    expect(spaceships).toHaveLength(3);
  });
});

I'm gonna create a PR just in case someone is interested

theshadowsvk commented 2 years ago

@Edo78 thank you for this, helped. Only one thing. I got 14tests and sometimes, randomly a random test fail due QueryFailedError: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed Do you have any idea how to handle it ? it looks like it got some problem creating entities, or schema or something. It happens very random.

Edo78 commented 2 years ago

I faced the same issue on the project I'm working on just this weekend. In my case it was a problem of async functions not correctly managed and the data in the DB aren't there when you are trying to do something with them. Try to wrap each call in a try/catch to detect where the error arise.

theshadowsvk commented 2 years ago

@Edo78 yep same problem here, did you find proper solution ?

Edo78 commented 2 years ago

The problem itself is given from the DB ... the try/catch suggestion wasn't mean to solve it, just to provide you an insight of what was it's root cause. You should debug (or log) the error and proced from there.

In any way it's not something related to this issue.