How to setup MongoDB for integration tests in NodeJS? How to setup MongoDB for integration tests in NodeJS? mongodb mongodb

How to setup MongoDB for integration tests in NodeJS?


As of this writing, I'd recommend using mongodb-memory-server. The package downloads a mongod binary to your home directory and instantiates a new memory-backed MondoDB instance as needed. This should work well for your CI setup because you can spin up a new server for each set of tests, which means you can run them in parallel.

See the documentation for details on how to use it with mongoose.


For readers using jest and the native mongodb driver, you may find this class useful:

const { MongoClient } = require('mongodb');const { MongoMemoryServer } = require('mongodb-memory-server');// Extend the default timeout so MongoDB binaries can downloadjest.setTimeout(60000);// List your collection names hereconst COLLECTIONS = [];class DBManager {  constructor() {    this.db = null;    this.server = new MongoMemoryServer();    this.connection = null;  }  async start() {    const url = await this.server.getUri();    this.connection = await MongoClient.connect(url, { useNewUrlParser: true });    this.db = this.connection.db(await this.server.getDbName());  }  stop() {    this.connection.close();    return this.server.stop();  }  cleanup() {    return Promise.all(COLLECTIONS.map(c => this.db.collection(c).remove({})));  }}module.exports = DBManager;

Then in each test file you can do the following:

const dbman = new DBManager();afterAll(() => dbman.stop());beforeAll(() => dbman.start());afterEach(() => dbman.cleanup());


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.


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(){} }; } });