jeudi 11 octobre 2018

Jest Test result empty

I have the following method to return my alerts:

const findAlerts = async () => {
  const alerts = Alert;
  const allAlerts = await alerts.find({ status: 'A' }, (err, activeAlerts) => {
    if (err) console.log(err);
    return activeAlerts;
  });

  return allAlerts;
};

on development and production, this return all alerts with status 'A', but with the following test:

test('Should create and find all active alerts', async () => {
    const currencyAlert = {
      name: 'VTC',
      pair: 'BTC',
      price: 0.00009239,
      condition: 'H',
      status: 'A',
      exchange: 'BITTREX',
    };

    const currencyAlert2 = {
      name: 'ETH',
      pair: 'BTC',
      price: 0.00009240,
      condition: 'H',
      status: 'A',
      exchange: 'BITTREX',
    };

    const currencyAlert3 = {
      name: 'EOS',
      pair: 'BTC',
      price: 0.00009241,
      condition: 'H',
      status: 'A',
      exchange: 'BITTREX',
    };

    await createAlert(currencyAlert);
    await createAlert(currencyAlert2);
    await createAlert(currencyAlert3);

    const alerts = await findAlerts();
    expect(alerts).toHaveLength(3);
  });

sometimes does no create the alerts, sometimes create the alerts, sometimes return 1 alert, sometimes return 2 and couple of time return the three alerts, I don't understand what is happening. This my setup.js file in case you need to watch my mongoose configuration for jest

import mongoose from 'mongoose';
import config from './config';

const options = {
  useNewUrlParser: true,
};

beforeEach(async (done) => {
  const clearDB = () => {
    for (let i in mongoose.connection.collections) {
      mongoose.connection.collections[i].remove(function() {});
    }
    done();
  }

  if (mongoose.connection.readyState === 0) {
    mongoose.connect(
      config.mongo.local,
      options,
      (err) => {
        if (err) {
          throw err;
        }
        return clearDB();
      }
    );
  } else {
    return clearDB();
  }

  clearDB();
});

afterEach(async (done) => {
  await Promise.all(mongoose.modelNames().map(model => mongoose.model(model).ensureIndexes()));
  mongoose.disconnect();
  return done();
});

afterAll(done => {
  return done();
});

Aucun commentaire:

Enregistrer un commentaire