Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
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 = {
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',
constructor () {
this.connection = null
this.mms = new MongoMemoryServer({
debug: false, // by default false
binary: {
debug: false
// version: '4.0.5'
}
})
}
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;
});
};
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')));
beforeAll(async () => {
mongod = new MongoMemoryServer();
process.env.MONGODB_URI = await mongod.getConnectionString();
app = require('../../../server/app');
});
afterAll(async () => {
async function startDatabase() {
const mongo = new MongoMemoryServer();
const mongoDBURL = await mongo.getConnectionString();
const connection = await MongoClient.connect(mongoDBURL, {
useNewUrlParser: true
});
database = connection.db();
}
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();
}