diff --git a/package.json b/package.json index b7de9c1f5a..11258f9f00 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "npm": "^3.3.10" }, "scripts": { - "test": "gulp lint && (gulp test:nodemon & (sleep 20; mocha test/api/v3 --recursive; killall gulp; killall node;))", + "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", diff --git a/tasks/gulp-tests.js b/tasks/gulp-tests.js index 5fbd32cac8..a963c6781d 100644 --- a/tasks/gulp-tests.js +++ b/tasks/gulp-tests.js @@ -13,6 +13,8 @@ import Q from 'q'; import runSequence from 'run-sequence'; import os from 'os'; +// TODO rewrite + const TEST_SERVER_PORT = 3003 const TEST_DB = 'habitrpg_test' let server; diff --git a/test/api/v3/unit/middlewares/analytics.test.js b/test/api/v3/unit/middlewares/analytics.test.js index bcaa7898e2..a56666b7a1 100644 --- a/test/api/v3/unit/middlewares/analytics.test.js +++ b/test/api/v3/unit/middlewares/analytics.test.js @@ -29,7 +29,7 @@ describe('analytics middleware', () => { attachAnalytics(req, res, next); - expect(res.analytics).to.exist; + expect(res.analytics).to.not.exist; }); it('attaches stubbed methods for non-prod environments', () => { @@ -53,4 +53,3 @@ describe('analytics middleware', () => { expect(res.analytics.trackPurchase).to.eql(analyticsService.trackPurchase); }); }); - diff --git a/test/helpers/api-integration/api-classes.js b/test/helpers/api-integration/api-classes.js index 097c359427..f584826520 100644 --- a/test/helpers/api-integration/api-classes.js +++ b/test/helpers/api-integration/api-classes.js @@ -4,7 +4,7 @@ import { requester } from './requester'; import { getDocument as getDocumentFromMongo, updateDocument as updateDocumentInMongo, -} from './mongo'; +} from '../mongo'; import { assign, each, diff --git a/test/helpers/api-integration/requester.js b/test/helpers/api-integration/requester.js index be2ea5c817..bcf580e653 100644 --- a/test/helpers/api-integration/requester.js +++ b/test/helpers/api-integration/requester.js @@ -2,7 +2,6 @@ import superagent from 'superagent'; import nconf from 'nconf'; -import app from '../../../website/src/server'; const API_TEST_SERVER_PORT = nconf.get('PORT'); let apiVersion; diff --git a/test/helpers/api-integration/v3/index.js b/test/helpers/api-integration/v3/index.js index 880f88f41a..4ee2e6cfbe 100644 --- a/test/helpers/api-integration/v3/index.js +++ b/test/helpers/api-integration/v3/index.js @@ -6,7 +6,7 @@ requester.setApiVersion('v3'); export { requester }; export { translate } from '../translate'; -export { checkExistence, resetHabiticaDB } from '../mongo'; +export { checkExistence, resetHabiticaDB } from '../../mongo'; export * from './object-generators'; export async function sleep (seconds) { diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js index 8988ca684a..ffef97ab82 100644 --- a/test/helpers/api-unit.helper.js +++ b/test/helpers/api-unit.helper.js @@ -4,21 +4,9 @@ import { defaultsDeep as defaults } from 'lodash'; import { model as User } from '../../website/src/models/user'; import { model as Group } from '../../website/src/models/group'; -let connection = mongoose.connection; - -before((done) => { - connection.on('open', () => { - connection.db.dropDatabase(done); - }); -}); - -after((done) => { - connection.close(done); -}); - afterEach((done) => { sandbox.restore(); - connection.db.dropDatabase(done); + mongoose.connection.db.dropDatabase(done); }); export function generateUser (options = {}) { diff --git a/test/helpers/common.helper.js b/test/helpers/common.helper.js index 5e0660d3c4..96064b8142 100644 --- a/test/helpers/common.helper.js +++ b/test/helpers/common.helper.js @@ -1,5 +1,4 @@ import mongoose from 'mongoose'; -import Q from 'q'; import { wrap as wrapUser } from '../../common/script/index'; import { model as User } from '../../website/src/models/user'; diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index f08df055fd..7b4a96070e 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -11,7 +11,14 @@ global.expect = chai.expect; global.sinon = require('sinon'); global.sandbox = sinon.sandbox.create(); +import nconf from 'nconf'; + //------------------------------ // Load nconf for unit tests //------------------------------ require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); +nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_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'); diff --git a/test/helpers/api-integration/mongo.js b/test/helpers/mongo.js similarity index 67% rename from test/helpers/api-integration/mongo.js rename to test/helpers/mongo.js index e3db242fd3..17d9c17a2a 100644 --- a/test/helpers/api-integration/mongo.js +++ b/test/helpers/mongo.js @@ -1,22 +1,17 @@ -/* eslint-disable no-use-before-define */ -import nconf from 'nconf'; -import { MongoClient as mongo } from 'mongodb'; +import mongoose from 'mongoose'; // Useful for checking things that have been deleted, // but you no longer have access to, // like private parties or users export async function checkExistence (collectionName, id) { - let db = await connectToMongo(); - return new Promise((resolve, reject) => { - let collection = db.collection(collectionName); + let collection = mongoose.connection.db.collection(collectionName); collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => { if (findError) return reject(findError); let exists = docs.length > 0; - db.close(); resolve(exists); }); }); @@ -25,12 +20,10 @@ export async function checkExistence (collectionName, id) { // Specifically helpful for the GET /groups tests, // resets the db to an empty state and creates a tavern document export async function resetHabiticaDB () { - let db = await connectToMongo(); - return new Promise((resolve, reject) => { - db.dropDatabase((dbErr) => { + mongoose.connection.db.dropDatabase((dbErr) => { if (dbErr) return reject(dbErr); - let groups = db.collection('groups'); + let groups = mongoose.connection.db.collection('groups'); groups.insertOne({ _id: 'habitrpg', @@ -42,7 +35,6 @@ export async function resetHabiticaDB () { }, (insertErr) => { if (insertErr) return reject(insertErr); - db.close(); resolve(); }); }); @@ -50,39 +42,39 @@ export async function resetHabiticaDB () { } export async function updateDocument (collectionName, doc, update) { - let db = await connectToMongo(); - - let collection = db.collection(collectionName); + let collection = mongoose.connection.db.collection(collectionName); return new Promise((resolve) => { collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => { if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`); - db.close(); resolve(); }); }); } export async function getDocument (collectionName, doc) { - let db = await connectToMongo(); - - let collection = db.collection(collectionName); + let collection = mongoose.connection.db.collection(collectionName); return new Promise((resolve) => { collection.findOne({ _id: doc._id }, (lookupErr, found) => { if (lookupErr) throw new Error(`Error looking up ${collectionName}: ${lookupErr}`); - db.close(); resolve(found); }); }); } -export function connectToMongo () { - return new Promise((resolve, reject) => { - mongo.connect(nconf.get('NODE_DB_URI'), (err, db) => { - if (err) return reject(err); - - resolve(db); - }); +before((done) => { + mongoose.connection.on('open', (err) => { + if (err) return done(err); + resetHabiticaDB() + .then(() => done()) + .catch(done); }); -} +}); + +after((done) => { + mongoose.connection.db.dropDatabase((err) => { + if (err) return done(err); + mongoose.connection.close(done); + }); +});