samedi 27 avril 2019

Jest and mongodb-memory-server : Unhandled errors

I'm testing typescript express-mongoose app with jest and MongoDB-memory-server.All my tests are running properly, but im getting jasmine unhandled errors and I cant find the problem.

let mongoServer: any;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
jest.mock('../../services/sendEmailConfirmation');
describe('/auth', (): void => {
  let mongoServer: any;
  const opts = {}; // remove this option if you use mongoose 5 and above

  beforeAll(async () => {
    mongoServer = new MongoMemoryServer();
    const mongoUri = await mongoServer.getConnectionString();
    await mongoose.connect(mongoUri, opts, err => {});
  });

  afterAll(async () => {
    mongoose.disconnect();
    await mongoServer.stop();
  });

  describe('auth', (): void => {
    describe('/sign-up', (): void => {
      it('signing up new user', async (): Promise<void> => {
        const response = await request(app)
          .post('/auth/sign-up')
          .send({
            email: 'test@mail.com',
            username: 'testUserName',
            password: '123456789123',
            matchPassword: '123456789123',
          });
        expect(response.status).toEqual(200);
        expect(sendEmailConfirmation).toHaveBeenCalledTimes(1);
        expect(response.body).toEqual({ message: 'User created!' });
      });
    });
  });
});

And the error that this code produces

console.error node_modules/jest-jasmine2/build/jasmine/Env.js:289 Unhandled error console.error node_modules/jest-jasmine2/build/jasmine/Env.js:290 Error [ERR_UNHANDLED_ERROR]: Unhandled error. (MongoNetworkError: read ECONNRESET) at Connection.emit (events.js:178:17) at Socket. (C:\Users\PC\Desktop\typescript-mern-budget-app\server\node_modules\mongodb-core\lib\connection\connection.js:321:10) at Object.onceWrapper (events.js:277:13) at Socket.emit (events.js:189:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process._tickCallback (internal/process/next_tick.js:63:19) console.error node_modules/jest-jasmine2/build/jasmine/Env.js:289 Unhandled error console.error node_modules/jest-jasmine2/build/jasmine/Env.js:290 Error [ERR_UNHANDLED_ERROR]: Unhandled error. (MongoNetworkError: read ECONNRESET) at Connection.emit (events.js:178:17) at Socket. (C:\Users\PC\Desktop\typescript-mern-budget-app\server\node_modules\mongodb-core\lib\connection\connection.js:321:10) at Object.onceWrapper (events.js:277:13) at Socket.emit (events.js:189:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process._tickCallback (internal/process/next_tick.js:63:19)

Aucun commentaire:

Enregistrer un commentaire