In-memory MongoDB for test?

前端 未结 2 744
野趣味
野趣味 2021-02-05 08:26

I am writing some integration and system tests for my NodeJS application using a MongoDB database. The test framework I use is Mocha and Supertest. Is it possible to setup Mongo

2条回答
  •  清歌不尽
    2021-02-05 08:28

    I recommend using mongodb-memory-server.

    I have a DB file that is shared over the whole project, so I have to have it testable as well.

    //db.js
    
    import mongoose from 'mongoose';
    import bluebird from 'bluebird';
    
    module.exports = {
      mongoose,
      init: () => {
          mongoose.Promise = bluebird;
      },
      connect: async database => {
          try {
              const conn =  await mongoose.connect(
                  database,
                  { useNewUrlParser: true }
              );
    
              //eslint-disable-next-line
              console.log(`MongoDb Connected on: ${database}`);
    
              return conn;
          } catch (err) {
              //eslint-disable-next-line
              console.log('Error to connect on mongo', err);
          }
      },
      disconnect: async () => await mongoose.connection.close()
    };
    

    To be used (db.js) on my tests I've created a test-helper.js file.

    'use strict';
    import MongodbMemoryServer from 'mongodb-memory-server';
    import db from '../src/db';
    
    const server = new MongodbMemoryServer();
    
    const createDB = async () => {
        try {
           const url = await server.getConnectionString();
           db.connect(url);
        } catch (err) {
          throw err;
        }
     };
    
     const destroyDB = () => {
       db.disconnect();
    
     };
    
     module.exports = {
        createDB,
        destroyDB
     }; 
    }
    

    So, my tests always have before and after (to create and destroy de DB), something like:

    import { createDB, destroyDB } from '../test-helper';
    before(() => {
      createDB();
    });
    after(() => {
      destroyDB();
    });
    

    Hope it is helpful.

    The project I'm using it: https://github.com/abdalla/node-auth

提交回复
热议问题