I am using Mocha in order to make my tests and async module to work with mongoose. When I make a simple test to get an information from my production code, it works. The following code WORKS FINE:
/lib/main.js
  saveClient () {
    let user
    async.waterfall([
      function (callback) {
        let firstFunction = 'First'
        callback(null)
      },
      function (callback) {
        let userId
        let secondFunction = 'second'
        async.waterfall([
          function (callback) {
            callback(null, 'Hi world')
          }
        ], function (err, results) {
          if (err) return console.log(err)
          userId = results
        })
        callback(null, userId)
      }
    ], function (err, results) {
      if (err) return console.log(err)
      user = results
    })
    return user
  }
/test/test-main.js
const main = require('./lib/main')
  it('create client', function (done) {
    let promiseClient = new Promise(function (resolve, reject) {
      resolve(main.saveClient())
    })
    promiseClient.then(function (result) {
      console.log(result)
      assert.typeOf(db.saveClient, 'function', 'saveClient is a function')
      done()
    })
  })
Here I am getting the result that is 'Hi world'
But, when I implement this same code with mongoose and its routines, I don't get the result that I want. Here is the code:
/lib/main.js
  saveClient () {
    let user
    async.waterfall([
      function (callback) {
        let conn = LisaClient.connection
        conn.on('error', console.error.bind(console, 'connection error...'))
        conn.once('open', function (err, db) {
          if (err) return console.log('error')
          callback(null)
        })
      },
      function (callback) {
        let userId
        let ClientSchema = new LisaSchema({ })
        let Client = LisaClient.model('Client', ClientSchema)
        let client = new Client({ })
        async.waterfall([
          function (callback) {
            client.save().then(callback(null, client._id))
          }
        ], function (err, results) {
          if (err) return console.log(err)
          userId = results
        })
        callback(null, userId)
      }
    ], function (err, results) {
      if (err) return console.log(err)
      user = results
    })
    return user
  }
/test/test-main.js
const main = require('./lib/main')
  it('create client', function (done) {
    let promiseClient = new Promise(function (resolve, reject) {
      resolve(main.saveClient())
    })
    promiseClient.then(function (result) {
      console.log(result)
      assert.typeOf(db.saveClient, 'function', 'saveClient is a function')
      done()
    })
  })
With the last test, I am always getting 'undefined'. I think is because mocha doesn't wait to resolve main.saveClient(). What can I do?. I used async/await and I am getting the same result
