mardi 7 mai 2019

Waiting for database configuration when MongoUnit starts

I want to run a test with the MongoUnit, but I received the error message “ReferenceError: regenerator not defined”. What have I done wrong?

I know that MongoUnit.start() returns a Promise, and I make a callback in .then() to work with url, but then I want to set up my database with that url. My configuration of database is async function and I don't understand how I may to create the waiting process inside .then() and go over to the next .then() only after configuration of database. Please help...)

1. configuration of database:

import Mongoose from 'mongoose';
import BlueBird from 'bluebird';
import config from 'config';
// ----------------------------
import logger from './src/utilities/logger'; 

const db = config.get('db');

export default async (url = `mongodb+srv://${db.username}:${db.password}@${db.host}/${db.name}`) => {
  Mongoose.Promise = BlueBird;
  Mongoose.connect(url, config.get('db.mongoose'), err => {
    if(err) {
      logger.error('+++ DB Error', err);
    } else {
      logger.info('+++ MongoDB connected');
    }
  });
};

2. UserTest

import MongoUnit from 'mongo-unit';
import {expect} from 'chai';
// -------------------------------
import TestData from './UserData';
import User from './../../src/collections/user';
import configureDB from './../../db';

describe('User Model', () => {

  before(() => MongoUnit.start()
    .then(url => configureDB(url)) // that moment I don't understand
    .then(() => MongoUnit.load(TestData)));

  after(() => MongoUnit.drop());

  it('should find all users', async () => {
    const users = await User.find();
    expect(users.length).to.equal(1);
    expect(users[0].username).to.equal('artem');
  });
});

Aucun commentaire:

Enregistrer un commentaire