Skip to content

Instantly share code, notes, and snippets.

@JCMais
Created May 13, 2018 18:59
Show Gist options
  • Save JCMais/56b532d97f232e9e65285f87045a4f51 to your computer and use it in GitHub Desktop.
Save JCMais/56b532d97f232e9e65285f87045a4f51 to your computer and use it in GitHub Desktop.
// eslint-disable-next-line import/no-extraneous-dependencies
const NodeEnvironment = require('jest-environment-node');
const MongodbMemoryServer = require('mongodb-memory-server');
class MongoDbEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
// eslint-disable-next-line new-cap
this.mongod = new MongodbMemoryServer.default({
instance: {
// settings here
// dbName is null, so it's random
// dbName: MONGO_DB_NAME,
},
binary: {
version: '3.6.1',
},
// debug: true,
});
}
async setup() {
await super.setup();
// console.log('\n# MongoDB Environment Setup #');
this.global.__MONGO_URI__ = await this.mongod.getConnectionString();
this.global.__MONGO_DB_NAME__ = await this.mongod.getDbName();
// this is used to have different names for documents created while testing
this.global.__COUNTERS__ = {
user: 0,
};
}
async teardown() {
// console.log('\n# MongoDB Environment Teardown #');
await super.teardown();
await this.mongod.stop();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = MongoDbEnvironment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment