Created
March 2, 2019 03:00
-
-
Save grantcarthew/75eaa0881ccf13350551549aea7b18f2 to your computer and use it in GitHub Desktop.
medium-db-setup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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