How to use the mongodb-memory-server.MongoMemoryServer function in mongodb-memory-server

To help you get started, we’ve selected a few mongodb-memory-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github typegoose / typegoose / test / utils / mongooseConnect.ts View on Github external
import { MongoMemoryServer } from 'mongodb-memory-server';
import * as mongoose from 'mongoose';
import { isNullOrUndefined } from '../../src/internal/utils';
import { config } from './config';

/** its needed in global space, because we dont want to create a new instance everytime */
let instance: MongoMemoryServer = null;

if (config.Memory) {
  // only create an instance, if it is enabled in the config, wich defaults to "true"
  instance = new MongoMemoryServer();
}

/** is it the First time connecting in this test run? */
let isFirst = true;

interface ExtraConnectionConfig {
  dbName?: string;
  createNewConnection?: boolean;
}

// to not duplicate code
const staticOptions = {
  useNewUrlParser: true,
  useFindAndModify: true,
  useCreateIndex: true,
  useUnifiedTopology: true,
github JKHeadley / rest-hapi / tests / e2e / end-to-end.tests.js View on Github external
const Logging = require('loggin')
const Q = require('q')
const Decache = require('decache')

// Import test groups
const BasicCrudTests = require('./basic-crud.tests')
const DocAuthTests = require('./doc-auth.tests')
const BasicEmbedTests = require('./basic-embed.tests')
const BasicNonEmbedTests = require('./basic-non-embed.tests')
const AuditLogTests = require('./audit-log.tests')
const AdvanceAssocTests = require('./advance-assoc.tests')
const DuplicateFieldTests = require('./duplicate-field.tests')
const MiscTests = require('./misc.tests')

const MongoMemoryServer = require('mongodb-memory-server').MongoMemoryServer
const mongoServer = new MongoMemoryServer({
  instance: {
    port: 27017,
    dbName: 'rest_hapi'
  }
})

// TODO: Possibly require this in every test and decache it to avoid unexpected
// errors between tests.
const Mongoose = require('mongoose')
Mongoose.Promise = Promise

let Log = Logging.getLogger('tests')
Log.logLevel = 'DEBUG'
Log = Log.bind('end-to-end')

const internals = {
github goemonjs / goemon / src / base / utilities / test-helper.ts View on Github external
export async function initializeDB() {
  mongoServer = new mongodbMemoryServer.MongoMemoryServer();
  const mongoUri = await mongoServer.getConnectionString();
  const mongooseOpts = { // options for mongoose 4.11.3 and above
    autoReconnect: true,
    reconnectTries: Number.MAX_VALUE,
    reconnectInterval: 1000,
  };

  await mongoose.connect(mongoUri, mongooseOpts, err => {
    if (err) {
      logger.error('Mongoose connect to MongoMemory failed!');
      logger.error(err);
    }
  });

  await Users.createUser({
    email: 'test@example.com',
github voluntarily / vly2 / server / util / test-memory-mongo.js View on Github external
constructor () {
    this.connection = null
    this.mms = new MongoMemoryServer({
      debug: false, // by default false
      binary: {
        debug: false
        // version: '4.0.5'
      }
    })
  }
github TreeHacks / root / backend / setupMongo.js View on Github external
const MongodbMemoryServer = require('mongodb-memory-server');

const mongoServer = new MongodbMemoryServer.MongoMemoryServer({
  binary: { version: "latest" },
  instance: { port: 27017, dbName: "treehacks-application-portal-local" }
});

// mongoose.Promise = Promise;
console.warn("Setting up mongodb server...");
module.exports = async function () {
  await mongoServer.getConnectionString().then((uri) => {
    process.env.MONGODB_URI = uri;
    console.log("Mongodb server running on url", uri);

    // Set reference to mongod in order to close the server during teardown.
    global.__MONGOD__ = mongoServer;
  });
};
github TreeHacks / hackpack-web-api / app.js View on Github external
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var path = require("path");
var request = require('request');
const Person = require('./models/person')

// Set up the Express app
const app = express();

const MongodbMemoryServer = require('mongodb-memory-server');

const mongoServer = new MongodbMemoryServer.MongoMemoryServer({
	binary: { version: "latest" },
	instance: { port: 65210, dbName: "test" }
});

mongoServer.getConnectionString().then((uri) => {
	// Connect to MongoDB - should be running locally
	mongoose.connect(uri);
	mongoose.Promise = global.Promise;
});



// Set up static files
app.use(express.static('public'));
app.use('/css', express.static(path.join(__dirname, 'public/styles')));
app.use('/scripts', express.static(path.join(__dirname, 'public/scripts')));
github biblibre / urungi / test / server / api / dashboards.spec.js View on Github external
beforeAll(async () => {
    mongod = new MongoMemoryServer();
    process.env.MONGODB_URI = await mongod.getConnectionString();
    app = require('../../../server/app');
});
afterAll(async () => {
github OWASP / RiskAssessmentFramework / api / src / database / mongo.js View on Github external
async function startDatabase() {
  const mongo = new MongoMemoryServer();
  const mongoDBURL = await mongo.getConnectionString();
  const connection = await MongoClient.connect(mongoDBURL, {
    useNewUrlParser: true
  });
  database = connection.db();
}
github usehenri / henri / packages / disk / index.js View on Github external
async start() {
    debug('starting %s', this.name);

    const dataPath = path.join(
      os.tmpdir(),
      `henri-mongo-${md5(process.cwd())}`
    );

    if (!fs.existsSync(dataPath)) {
      fs.mkdirSync(dataPath);
    }

    this.mongod = new MongoMemoryServer({
      instance: {
        dbName: 'henri',
        dbPath: this.henri.isTest ? null : dataPath,
        storageEngine: this.henri.isTest ? 'ephemeralForTest' : 'wiredTiger',
      },
    });

    this.config.url = await this.mongod.getConnectionString();

    return super.start();
  }

mongodb-memory-server

MongoDB Server for testing (auto-download latest version). The server will allow you to connect your favourite ODM or client library to the MongoDB Server and run parallel integration tests isolated from each other.

MIT
Latest version published 13 days ago

Package Health Score

98 / 100
Full package analysis