0

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

maoooricio
  • 2,249
  • 3
  • 15
  • 19
  • There are multiple errors in your code that all boil down to incorrectly trying to get values from asynchronous code. Even the code that you think is right is incorrect. The fact that you see "Hi world" is not demonstrating correctness. You need to start with the basics and learn how to correctly obtain values from asynchronous code. – Louis Mar 01 '17 at 17:38
  • Hi Louis, thanks for your support. Can you give me some readings in order to start to understand? In advance thanks – maoooricio Mar 01 '17 at 17:43
  • The question against which your question was closed is the one resource we point people to for learning how it all works. I don't know of a better resource I'm afraid. – Louis Mar 01 '17 at 17:45
  • I found my answer, I understand better what is a promise. You were all right, I was in a very bad mistake. Thanks! – maoooricio Mar 02 '17 at 16:29

0 Answers0