How to setup MongoDB for integration tests in NodeJS?

I am writing integration tests for application written in NodeJS with MongoDB.

On CI server I would like to have some sort of embedded MongoDB for faster performance and easier control. Currently I have MongoDB on other server, but tests are slow. Before each test I need to drop all collections. I am using mongoose as ORM.

So far I have only found embedded MongoDB for Java.

Our team has been stubbing out the mongo skin calls. Depending on your testing packages you can do the same thing. It takes a little bit of work but it is worth it. Create a stub function and then just declare what you need in your test.

   // Object based stubbing
    function createObjStub(obj) {
      return {
        getDb: function() {
         return {
            collection: function() {
              var coll = {};

              for (var name in obj) {
                var func = obj[name];

                if (typeof func === 'object') {
                  coll = func;
                } else {
                  coll[name] = func;
                }
              }

              return coll;
            }
          };
        }
      }
   }; 
    // Stubbed mongodb call
      var moduleSvc = new ModulesService(createObjStub({
        findById: function(query, options, cb) {
          return cb({
             'name': 'test'
           }, null);
           //return cb(null, null);
        }
      }),{getProperties: function(){return{get: function(){} }; } });

Following the "don’t use test doubles for types you don’t own" principle, consider continue using a real MongoDB instance for your integration test. Look at this nice article for details.