#Unit tests never end jest + nest + mongo

1 messages · Page 1 of 1 (latest)

fair nebula
#

Hello, i have an problem with my tests units.

I wan't to develop unit tests to my project, and it don't work.

I h've adding mongoDB, and now, tests units don't pass.

I look some forums and i don't found the solution.

Il my new service spec.ts, it work when i'hve only one "it", but if i have 2: "it", the code lock, afterAll timeout (5 seconds / 20 seconds, is the same), and jest say me:

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.```


My Test: 
https://github.com/ScyLabs/auth.service.spec.ts/blob/main/auth.service.spec.ts
GitHub

Contribute to ScyLabs/auth.service.spec.ts development by creating an account on GitHub.

fair nebula
#

No one have an solution or i'hve giving bad explication ?

hearty cargo
#

Are your unit tests connecting to an actual database? That doesn't sound good. Or are these e2e tests?

fair nebula
#

I use MongoMemoryServer.
It i understand correctly It is an mongodb simulation.
Thé code is on e2e context i suppose because my code work with db

#

I need to test the complete success of the app. How can i test my functionnalities if It dépends of db ?

hearty cargo
#

The error implies that you cannot connect to the database multiple times. Unit tests running in parallel might do that. Also, after executing any test you should gracefully disconnect from the server. Perhaps you can do that in a beforeAll/afterAll block.

fair nebula
#
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from 'auth/auth.service';
import { UsersService } from 'users/users.service';
import { AuthModule, JwtModuleConfig } from 'auth/auth.module';
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose, { Connection, Model, connect } from 'mongoose';
import { getModelToken } from '@nestjs/mongoose';

import { User, UserSchema } from 'users/user.model';
import { UsersModule } from 'users/users.module';

import { AuthController } from 'auth/auth.controller';
import { Module } from '@nestjs/common';
#
describe('AuthService', () => {
  let module: TestingModule;

  let mongod: MongoMemoryServer;
  let mongoConnection: Connection;
  let userModel: Model<User>;

  const TEST_USER = {
    username: 'test',
    password: 'test',
  };

  beforeEach(async () => {
    mongod = await MongoMemoryServer.create();
    mongoConnection = (
      await mongoose.connect(mongod.getUri(), {
        socketTimeoutMS: 5000,
      })
    ).connection;

    userModel = mongoConnection.model(User.name, UserSchema);

    @Module({
      providers: [
        UsersService,
        { provide: getModelToken(User.name), useValue: userModel },
      ],
      exports: [UsersService],
    })
    class UsersModule {}

    module = await Test.createTestingModule({
      controllers: [AuthController],
      imports: [UsersModule, JwtModuleConfig],
      providers: [AuthService],
    }).compile();

    //service = module.get<AuthService>(AuthService);
  });

  afterAll(async () => {
    await mongoConnection.dropDatabase();
    await mongoConnection.close();
    await mongod.stop();
  }, 20000);
  afterEach(async () => {
    const collections = mongoConnection.collections;
    for (const key in collections) {
      const collection = collections[key];
      await collection.deleteMany({});
    }
  });

  describe('login', () => {
    it('controller should be defined', () => {
      expect(true).toBe(true);
    });
    it('Create account with model', () => {
      expect(false).toBe(false);
    });
  });
fair nebula
#

I can add you on my repository if you need, you can try, it is very unintelligible

fair nebula
#

You dont see thé problem ? 🥲

rapid holly
#

You use beforeEach to create the connections but afterAll to close everything. That's a bit of a smell if you ask me

fair nebula
#

Hoo ok..

#

I just understand now

#

Thanks you guy. I need to use beforeAll

#

beforeEach, is before all it 🤣 🤣 🤣