Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 832
Luka Blažecki Avatar asked Jun 27 '13 11:06

Luka Blažecki


People also ask

How does MongoDB integrate with node js?

To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database if it does not exist, and make a connection to it.

How does jest connect to MongoDB?

Jest MongoDB will provide all the required configuration to run your tests using MongoDB. const {MongoClient} = require('mongodb'); describe('insert', () => { let connection; let db; beforeAll(async () => { connection = await MongoClient. connect(global.

What is integration testing in node JS?

js Guide to Actually Doing Integration Tests. Your software isn't fully tested until you write integration tests for it. While unit tests help ensure that functions are properly written, integration tests help ensure that the system is working properly as a whole.


1 Answers

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 download
jest.setTimeout(60000);

// List your collection names here
const 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());
like image 81
David Weldon Avatar answered Sep 27 '22 19:09

David Weldon