2
votes

I'm trying to do a simple test of my mongoose model with mocha and chai

// Mock user
  var testUser = new User({
    companyID: "1",
    username: "mockUser",
    password: "password",
    email: "[email protected]",
  });

// Create new user
it('Should add a new user with a hashed password to DB' , (done) => {
  User.addUser(testUser, (err, user) => {
    if(err) console.log(err);
    else {
      assert.typeOf(user, 'Object');
      assert.equal(user.username, "mockUser");
      expect(user.password).to.not.equal("password");
    }
    done();
  });
});

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves

I assume mocha changed since this has been working with previous projects. What am i missing here ?

the model method:

module.exports.addUser = function(newUser, callback) {
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if (err) throw err;
      newUser.password = hash;
      newUser.save(callback);
    });
  });
}
2

2 Answers

0
votes

Sometimes, when a unit test implies to create an object in database it takes more than the 2 seconds of the default timeout. Try launching mocha increasing timeout and see that it works.

mocha --timeout 10000
0
votes

forgot to require the app itself.

const app = require('../app');