From b1848da8ded07a5b3dbc8f985e034360739066b1 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 29 Jan 2016 14:43:14 +0100 Subject: [PATCH] do not output server logs when testing, ability to run tests and server separately --- package.json | 3 ++- test/helpers/globals.helper.js | 26 +++++++++++++++++++------- website/src/libs/api-v3/logger.js | 3 +++ website/src/libs/api-v3/setupNconf.js | 1 + website/src/models/group.js | 2 +- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 11258f9f00..6d787f5bc9 100644 --- a/package.json +++ b/package.json @@ -97,9 +97,10 @@ "test": "gulp lint && npm run test:api-v3:unit && npm run test:api-v3:integration", "test:api-v2:unit": "mocha test/server_side", "test:api-v2:integration": "mocha test/api/v2 --recursive", - "test:api-v3": "mocha test/api/v3 --recursive", + "test:api-v3": "npm run test:api-v3:unit && npm run test:api-v3:integration", "test:api-v3:unit": "mocha test/api/v3/unit --recursive", "test:api-v3:integration": "mocha test/api/v3/integration --recursive", + "test:api-v3:integration:separate-server": "LOAD_SERVER=0 mocha test/api/v3/integration --recursive", "test:api-legacy": "istanbul cover -i \"website/src/**\" --dir coverage/api ./node_modules/mocha/bin/_mocha test/api-legacy", "test:common": "mocha test/common", "test:content": "mocha test/content", diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index 2bfe3c8a56..9e92a85de9 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -1,4 +1,6 @@ /* eslint-disable no-undef */ +/* eslint-disable global-require */ +/* eslint-disable no-process-env */ //------------------------------ // Global modules //------------------------------ @@ -12,14 +14,24 @@ global.sinon = require('sinon'); global.sandbox = sinon.sandbox.create(); import nconf from 'nconf'; +import mongoose from 'mongoose'; +import Q from 'q'; //------------------------------ // Load nconf for unit tests //------------------------------ -require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); -nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test'); -nconf.set('NODE_ENV', 'test'); -// We require src/server and npt src/index because -// 1. nconf is already setup -// 2. we don't need clustering -require('../../website/src/server'); +if (process.env.LOAD_SERVER === '0') { // when the server is in a different process we simply connect to mongoose + require('../../website/src/libs/api-v3/setupNconf')('./config.json'); + // Use Q promises instead of mpromise in mongoose + mongoose.Promise = Q.Promise; + mongoose.connect(nconf.get('NODE_DB_URI')); +} else { // When running tests and the server in the same process + require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); + nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test'); + nconf.set('NODE_ENV', 'test'); + nconf.set('IS_TEST', true); + // We require src/server and npt src/index because + // 1. nconf is already setup + // 2. we don't need clustering + require('../../website/src/server'); +} diff --git a/website/src/libs/api-v3/logger.js b/website/src/libs/api-v3/logger.js index 0d00ca7f4b..3b259eda6b 100644 --- a/website/src/libs/api-v3/logger.js +++ b/website/src/libs/api-v3/logger.js @@ -4,12 +4,15 @@ import winston from 'winston'; import nconf from 'nconf'; const IS_PROD = nconf.get('IS_PROD'); +const IS_TEST = nconf.get('IS_TEST'); let logger = new winston.Logger(); if (IS_PROD) { // TODO production logging, use loggly // log errors to console too +} else if (IS_TEST) { + // Do not log anything when testing } else { logger .add(winston.transports.Console, { diff --git a/website/src/libs/api-v3/setupNconf.js b/website/src/libs/api-v3/setupNconf.js index e5a49bc3e9..f55f593bad 100644 --- a/website/src/libs/api-v3/setupNconf.js +++ b/website/src/libs/api-v3/setupNconf.js @@ -13,4 +13,5 @@ export default function setupNconf (file) { nconf.set('IS_PROD', nconf.get('NODE_ENV') === 'production'); nconf.set('IS_DEV', nconf.get('NODE_ENV') === 'development'); + nconf.set('IS_TEST', nconf.get('NODE_ENV') === 'test'); } diff --git a/website/src/models/group.js b/website/src/models/group.js index 2d2b4e3e71..718df557c9 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -515,7 +515,7 @@ export let model = mongoose.model('Group', schema); // initialize tavern if !exists (fresh installs) // do not run when testing as it's handled by the tests and can easily cause a race condition -if (nconf.get('NODE_ENV') !== 'test') { +if (nconf.get('IS_TEST')) { model.count({_id: 'habitrpg'}, (err, ct) => { if (err) throw err; if (ct > 0) return;