Search code examples
node.jsmongoosejasminebddjasmine-node

jasmine test case does not return from mongo save


I am using https://github.com/mhevery/jasmine-node to test my nodejs server routes. My mongoose model has a working pre function as below

userSchema.pre('save', function(next) {
  var user = this;
  if (!user.isModified('password')) return next();
      bcrypt.genSalt(10, function(err, salt) {
         if (err) return next(err);

             logger.info('Hashing password!!!');

             bcrypt.hash(user.password, salt, null, function(err, hash) {
                  if (err) return next(err);
                  user.password = hash;
                  next();
             });
      });
});

Now I need to write a test case in jasmine that creates a userSchema object and saves it to mongodb to use it further in my test case.

var User = require("../../../../models/User");

            it("{postLogin - invalid}", function(done) {
                var user = new User({email: "[email protected]", password: "a"});

                user.save(function(err) {
                if (err){
                    console.log(err);
                    return next(err);
                }
                console.log('user saved to db: ' +user.email);  
                request.post(invalidLoginParams, function(error, response, body) {
                    expect(response.statusCode).toBe(400);
                    done();
                });              
            });
    },15000);

I run the above test case using jasmine-node and i get the log 'Hashing password!!!' which means it is calling this function. But after this it never returns to my test case. It should print the log 'user saved to db: ' But it is never returning back from .pre('save') function. Any idea where and what am i missing. Could not find any answer on google. Hope to get it here! Thanks


Solution

  • please make sure to call the callback "done()" properly.