How to unit test mongoose model? How to unit test mongoose model? mongoose mongoose

How to unit test mongoose model?


Mockgoose runs an in memory copy of MongoDB and once setup, patches mongoose so your app connections go to the test instance.

before(function() {  return mockgoose.prepareStorage().then(()=>{    return mongoose.connect('mongodb://127.0.0.1:27017/whatever')  })})after(function() {  return mockgoose.helper.reset()}}

Mockgoose saves all the "why isn't my mock like mongoose" time which only gets worse once you get past a simple example into multiple queries or more complex operations.

Although the tests aren't really unit tests at this stage, it's the best solution I've found for mongoose database interaction if you can live with the addition of mongoose and mongodb and mongodb-prebuilt to your "unit" of test.

The monbodb-prebuilt dependency is ~ 200MB so adding it can impact dev dependency install time.