Use mongodb in memory for unit tests
parent
37860fd115
commit
b09222bd73
|
@ -15,9 +15,14 @@ module.exports = {
|
|||
},
|
||||
|
||||
test: {
|
||||
port: 3001,
|
||||
db: 'cc-test',
|
||||
env: 'test',
|
||||
},
|
||||
|
||||
dredd: {
|
||||
env: 'dreddTest',
|
||||
port: 3001
|
||||
},
|
||||
unit: {
|
||||
env: 'unitTest',
|
||||
port: 3010
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@ dry-run: null
|
|||
hookfiles: null
|
||||
language: nodejs
|
||||
sandbox: false
|
||||
server: npm run start-test
|
||||
server: npm run start-api-test
|
||||
server-wait: 3
|
||||
init: false
|
||||
custom: {}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -11,7 +11,8 @@
|
|||
"eslint": "$(npm bin)/eslint",
|
||||
"lint": "eslint '**/*.js'",
|
||||
"test": "mocha --require ./test/config/spec_helper.js",
|
||||
"start-test": "DEBUG='cc:*' NODE_ENV=test node server.js",
|
||||
"start-test": "DEBUG='cc:*' NODE_ENV=unitTest node server.js",
|
||||
"start-api-test": "DEBUG='cc:*' NODE_ENV=dreddTest node server.js",
|
||||
"api:compile-docs": "$(npm bin)/hercule apib/dev-doc.apib -o apib/documentation.apib",
|
||||
"api:publish-docs": "$(npm bin)/aglio --theme-variables slate -i ./apib/documentation.apib -o ../public/api-docs.html",
|
||||
"api:test-docs": "npm run api:docs && ./apib/dredd/populate-data.sh && $(npm bin)/dredd",
|
||||
|
@ -48,6 +49,7 @@
|
|||
"eslint-config-google": "^0.9.1",
|
||||
"hercule": "^4.1.1",
|
||||
"mocha": "^3.5.3",
|
||||
"mongodb-memory-server": "^1.7.3",
|
||||
"nodemon": "^1.14.12"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ app.use(urls.signatures, signatureRouter);
|
|||
if (process.env.NODE_ENV === config.dev.env) {
|
||||
// development logging
|
||||
app.use(requestLogger('dev'));
|
||||
} else if (process.env.NODE_ENV !== config.test.env) {
|
||||
} else if (process.env.NODE_ENV !== config.test.dredd.env && process.env.NODE_ENV !== config.test.unit.env) {
|
||||
// production logging, apache style
|
||||
app.use(requestLogger(':date[clf] :method :url :response-time ms :status'));
|
||||
}
|
||||
|
@ -102,25 +102,59 @@ app.use((req, res) => {
|
|||
errorResponseWare(app);
|
||||
|
||||
// Start the server
|
||||
if (process.env.NODE_ENV !== config.test.env) {
|
||||
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db);
|
||||
mongoosePromise.then((db) => {
|
||||
app.listen(config.port, (err) => {
|
||||
if (err !== undefined) {
|
||||
error('Error on startup, ', err);
|
||||
} else {
|
||||
logger('Listening on port ' + config.port);
|
||||
signatureCronJob.start();
|
||||
backupCronJob.start();
|
||||
}
|
||||
|
||||
switch (process.env.NODE_ENV) {
|
||||
case config.test.dredd.env: {
|
||||
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
|
||||
mongoosePromise.then((db) => {
|
||||
app.listen(config.test.dredd.port);
|
||||
logger('Listening on port ' + config.test.dredd.port);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
|
||||
mongoosePromise.then((db) => {
|
||||
app.listen(config.test.port);
|
||||
logger('Listening on port ' + config.test.port);
|
||||
});
|
||||
break;
|
||||
}
|
||||
case config.test.unit.env: {
|
||||
const MongodbMemoryServer = require('mongodb-memory-server').default;
|
||||
const mongoServer = new MongodbMemoryServer();
|
||||
mongoose.Promise = Promise;
|
||||
mongoServer.getConnectionString().then((mongoUri) => {
|
||||
const mongooseOpts = { // options for mongoose 4.11.3 and above
|
||||
autoReconnect: true,
|
||||
reconnectTries: Number.MAX_VALUE,
|
||||
reconnectInterval: 1000,
|
||||
useMongoClient: true, // remove this line if you use mongoose 5 and above
|
||||
};
|
||||
|
||||
mongoose.connect(mongoUri, mongooseOpts);
|
||||
|
||||
mongoose.connection.on('error', (e) => {
|
||||
if (e.message.code === 'ETIMEDOUT') {
|
||||
console.log(e);
|
||||
mongoose.connect(mongoUri, mongooseOpts);
|
||||
}
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
mongoose.connection.once('open', () => {
|
||||
console.log(`MongoDB successfully connected to ${mongoUri}`);
|
||||
});
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db);
|
||||
mongoosePromise.then((db) => {
|
||||
app.listen(config.port, (err) => {
|
||||
if (err !== undefined) {
|
||||
error('Error on startup, ', err);
|
||||
} else {
|
||||
logger('Listening on port ' + config.port);
|
||||
signatureCronJob.start();
|
||||
backupCronJob.start();
|
||||
}
|
||||
});
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = app;
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
const config = require('../../config/config');
|
||||
|
||||
process.env.NODE_ENV = config.test.env;
|
||||
process.env.NODE_ENV = config.test.unit.env;
|
||||
|
|
Loading…
Reference in New Issue