Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(config) {
// console.error('\n# MongoDB Environment Constructor #\n');
super(config);
this.mongod = new MongodbMemoryServer.default({
instance: {
// settings here
// dbName is null, so it's random
// dbName: MONGO_DB_NAME,
},
binary: {
version: '4.0.0',
},
// debug: true,
autoStart: false,
});
}
async function getMongoMemoryServerConfig() {
mongoServer = mongoServer || new MongoDBMemoryServer();
mongoServerReferences++;
// Passing `true` here generates a new, random DB name for us
const mongoUri = await mongoServer.getConnectionString(true);
// In theory the dbName can contain query params so lets parse it then extract the db name
const dbName = url
.parse(mongoUri)
.pathname.split('/')
.pop();
return { mongoUri, dbName };
}
beforeAll(async () => {
// DEV NOTE: do NOT resetModules() here, as it'll impact Mongoose’s
// internal driver initialization, leading to weird "Decimal128 of null"
// errors -- this one was tough to hunt down…
jest.dontMock('./util/ciphers')
server = new MongoDBMemoryServer()
const url = await server.getConnectionString()
connection = await mongoose.createConnection(url, {
autoReconnect: true,
connectTimeoutMS: 1000,
reconnectInterval: 100,
reconnectTries: Number.MAX_VALUE,
useNewUrlParser: true,
})
})
module.exports = async () => {
const mongoServer = new MongoDBMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
const mongoConnection = await MongoClient.connect(mongoUri, { useNewUrlParser: true });
const mongoDb = mongoConnection.db(await mongoServer.getDbName());
// Only item 2 & 3 have stock. Item 2 only has stock in warehouse A.Item 3 used
// to have stock in warehouse B, but now only has stock in warehouse A.
const { insertedIds: insertedStock } = await mongoDb
.collection('warehouses')
.insertMany([
{ warehouse: 'A', instock: 0 },
{ warehouse: 'A', instock: 80 },
{ warehouse: 'B', instock: 0 },
{ warehouse: 'B', instock: 0 },
{ warehouse: 'A', instock: 40 },
]);
const fs = require('fs');
const {resolve, join} = require('path');
const cwd = require('cwd');
const MongodbMemoryServer = require('mongodb-memory-server');
const globalConfigPath = join(__dirname, 'globalConfig.json');
const debug = require('debug')('jest-mongodb:setup');
const mongod = new MongodbMemoryServer.default(getMongodbMemoryOptions());
module.exports = async () => {
if (!mongod.isRunning) {
await mongod.start();
}
const mongoConfig = {
mongoDBName: getMongodbMemoryOptions().instance.dbName,
mongoUri: await mongod.getConnectionString()
};
// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
debug('Config is written');
// Set reference to mongod in order to close the server during teardown.
constructor(config) {
super(config);
this.mongod = new MongodbMemoryServer.default({
instance: {
dbName: this.global.dbName
},
binary: {
version: "3.4.4"
}
});
}
const MongodbMemoryServer = require('mongodb-memory-server').default
const NodeEnvironment = require('jest-environment-node')
const mongod = new MongodbMemoryServer()
class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config)
}
async setup() {
console.log('Setup MongoDB Test Environment')
this.global.__MONGO_URI__ = `mongodb://localhost:${await mongod.getPort()}/`
await super.setup()
}
async teardown() {
console.log('Teardown MongoDB Test Environment')
const MongodbMemoryServer = require('mongodb-memory-server').default
const mongod = new MongodbMemoryServer()
module.exports = () => {
global.__MONGOD__ = mongod
}
const { default: MongoMemoryServer } = require('mongodb-memory-server');
const mongod = new MongoMemoryServer({ autoStart: false });
const globalSetup = async () => {
await mongod.start();
process.env.MONGO_URI = await mongod.getConnectionString();
global.__MONGOD__ = mongod;
};
module.exports = globalSetup;
constructor(config) {
super(config)
this.mongod = new MongodbMemoryServer()
}