Skip to content

Instantly share code, notes, and snippets.

@grantcarthew
Created March 2, 2019 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantcarthew/75eaa0881ccf13350551549aea7b18f2 to your computer and use it in GitHub Desktop.
Save grantcarthew/75eaa0881ccf13350551549aea7b18f2 to your computer and use it in GitHub Desktop.
medium-db-setup.js
// db-setup.js
const path = require('path')
const fs = require('fs')
const { MongoMemoryServer } = require('mongodb-memory-server')
const driver = require('../../src/store/driver')
const initializeDb = require('../initialize-db')
const globalConfigPath = path.join(__dirname, 'globalConfig.json')
const mongod = new MongoMemoryServer({
autoStart: false
})
module.exports = async function dbSetup () {
if (!mongod.isRunning) {
await mongod.start()
}
const mongoConfig = {
mongoDBName: 'jest',
mongoUri: await mongod.getConnectionString()
}
await driver.connect(mongoConfig.mongoUri)
await initializeDb()
// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig))
// Set reference to mongod in order to close the server during teardown.
global.__MONGOD__ = mongod
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment