Open Edo78 opened 2 years ago
@Edo78 I also have this problem, can you assist me how to properly close the module as you mentioned ?
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
@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.
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.
@Edo78 yep same problem here, did you find proper solution ?
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.
If I add even another simple test like
to
src/spaceships/spaceships.service.spec.ts
then I start to get an erroruntil 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