Use mongodb in memory for unit tests
parent
37860fd115
commit
b09222bd73
|
@ -15,9 +15,14 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
test: {
|
test: {
|
||||||
port: 3001,
|
|
||||||
db: 'cc-test',
|
db: 'cc-test',
|
||||||
env: 'test',
|
dredd: {
|
||||||
},
|
env: 'dreddTest',
|
||||||
|
port: 3001
|
||||||
|
},
|
||||||
|
unit: {
|
||||||
|
env: 'unitTest',
|
||||||
|
port: 3010
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ dry-run: null
|
||||||
hookfiles: null
|
hookfiles: null
|
||||||
language: nodejs
|
language: nodejs
|
||||||
sandbox: false
|
sandbox: false
|
||||||
server: npm run start-test
|
server: npm run start-api-test
|
||||||
server-wait: 3
|
server-wait: 3
|
||||||
init: false
|
init: false
|
||||||
custom: {}
|
custom: {}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -11,7 +11,8 @@
|
||||||
"eslint": "$(npm bin)/eslint",
|
"eslint": "$(npm bin)/eslint",
|
||||||
"lint": "eslint '**/*.js'",
|
"lint": "eslint '**/*.js'",
|
||||||
"test": "mocha --require ./test/config/spec_helper.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: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: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",
|
"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",
|
"eslint-config-google": "^0.9.1",
|
||||||
"hercule": "^4.1.1",
|
"hercule": "^4.1.1",
|
||||||
"mocha": "^3.5.3",
|
"mocha": "^3.5.3",
|
||||||
|
"mongodb-memory-server": "^1.7.3",
|
||||||
"nodemon": "^1.14.12"
|
"nodemon": "^1.14.12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,7 @@ app.use(urls.signatures, signatureRouter);
|
||||||
if (process.env.NODE_ENV === config.dev.env) {
|
if (process.env.NODE_ENV === config.dev.env) {
|
||||||
// development logging
|
// development logging
|
||||||
app.use(requestLogger('dev'));
|
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
|
// production logging, apache style
|
||||||
app.use(requestLogger(':date[clf] :method :url :response-time ms :status'));
|
app.use(requestLogger(':date[clf] :method :url :response-time ms :status'));
|
||||||
}
|
}
|
||||||
|
@ -102,25 +102,59 @@ app.use((req, res) => {
|
||||||
errorResponseWare(app);
|
errorResponseWare(app);
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
if (process.env.NODE_ENV !== config.test.env) {
|
|
||||||
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db);
|
switch (process.env.NODE_ENV) {
|
||||||
mongoosePromise.then((db) => {
|
case config.test.dredd.env: {
|
||||||
app.listen(config.port, (err) => {
|
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
|
||||||
if (err !== undefined) {
|
mongoosePromise.then((db) => {
|
||||||
error('Error on startup, ', err);
|
app.listen(config.test.dredd.port);
|
||||||
} else {
|
logger('Listening on port ' + config.test.dredd.port);
|
||||||
logger('Listening on port ' + config.port);
|
|
||||||
signatureCronJob.start();
|
|
||||||
backupCronJob.start();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
break;
|
||||||
} else {
|
}
|
||||||
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
|
case config.test.unit.env: {
|
||||||
mongoosePromise.then((db) => {
|
const MongodbMemoryServer = require('mongodb-memory-server').default;
|
||||||
app.listen(config.test.port);
|
const mongoServer = new MongodbMemoryServer();
|
||||||
logger('Listening on port ' + config.test.port);
|
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;
|
module.exports = app;
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
const config = require('../../config/config');
|
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