failing unit test

This commit is contained in:
Matteo Pagliazzi
2016-01-29 12:07:46 +01:00
parent b960ecdd94
commit e345fa76f5
10 changed files with 34 additions and 48 deletions

View File

@@ -94,7 +94,7 @@
"npm": "^3.3.10" "npm": "^3.3.10"
}, },
"scripts": { "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:unit": "mocha test/server_side",
"test:api-v2:integration": "mocha test/api/v2 --recursive", "test:api-v2:integration": "mocha test/api/v2 --recursive",
"test:api-v3": "mocha test/api/v3 --recursive", "test:api-v3": "mocha test/api/v3 --recursive",

View File

@@ -13,6 +13,8 @@ import Q from 'q';
import runSequence from 'run-sequence'; import runSequence from 'run-sequence';
import os from 'os'; import os from 'os';
// TODO rewrite
const TEST_SERVER_PORT = 3003 const TEST_SERVER_PORT = 3003
const TEST_DB = 'habitrpg_test' const TEST_DB = 'habitrpg_test'
let server; let server;

View File

@@ -29,7 +29,7 @@ describe('analytics middleware', () => {
attachAnalytics(req, res, next); attachAnalytics(req, res, next);
expect(res.analytics).to.exist; expect(res.analytics).to.not.exist;
}); });
it('attaches stubbed methods for non-prod environments', () => { it('attaches stubbed methods for non-prod environments', () => {
@@ -53,4 +53,3 @@ describe('analytics middleware', () => {
expect(res.analytics.trackPurchase).to.eql(analyticsService.trackPurchase); expect(res.analytics.trackPurchase).to.eql(analyticsService.trackPurchase);
}); });
}); });

View File

@@ -4,7 +4,7 @@ import { requester } from './requester';
import { import {
getDocument as getDocumentFromMongo, getDocument as getDocumentFromMongo,
updateDocument as updateDocumentInMongo, updateDocument as updateDocumentInMongo,
} from './mongo'; } from '../mongo';
import { import {
assign, assign,
each, each,

View File

@@ -2,7 +2,6 @@
import superagent from 'superagent'; import superagent from 'superagent';
import nconf from 'nconf'; import nconf from 'nconf';
import app from '../../../website/src/server';
const API_TEST_SERVER_PORT = nconf.get('PORT'); const API_TEST_SERVER_PORT = nconf.get('PORT');
let apiVersion; let apiVersion;

View File

@@ -6,7 +6,7 @@ requester.setApiVersion('v3');
export { requester }; export { requester };
export { translate } from '../translate'; export { translate } from '../translate';
export { checkExistence, resetHabiticaDB } from '../mongo'; export { checkExistence, resetHabiticaDB } from '../../mongo';
export * from './object-generators'; export * from './object-generators';
export async function sleep (seconds) { export async function sleep (seconds) {

View File

@@ -4,21 +4,9 @@ import { defaultsDeep as defaults } from 'lodash';
import { model as User } from '../../website/src/models/user'; import { model as User } from '../../website/src/models/user';
import { model as Group } from '../../website/src/models/group'; 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) => { afterEach((done) => {
sandbox.restore(); sandbox.restore();
connection.db.dropDatabase(done); mongoose.connection.db.dropDatabase(done);
}); });
export function generateUser (options = {}) { export function generateUser (options = {}) {

View File

@@ -1,5 +1,4 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import Q from 'q';
import { wrap as wrapUser } from '../../common/script/index'; import { wrap as wrapUser } from '../../common/script/index';
import { model as User } from '../../website/src/models/user'; import { model as User } from '../../website/src/models/user';

View File

@@ -11,7 +11,14 @@ global.expect = chai.expect;
global.sinon = require('sinon'); global.sinon = require('sinon');
global.sandbox = sinon.sandbox.create(); global.sandbox = sinon.sandbox.create();
import nconf from 'nconf';
//------------------------------ //------------------------------
// Load nconf for unit tests // Load nconf for unit tests
//------------------------------ //------------------------------
require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); 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');

View File

@@ -1,22 +1,17 @@
/* eslint-disable no-use-before-define */ import mongoose from 'mongoose';
import nconf from 'nconf';
import { MongoClient as mongo } from 'mongodb';
// Useful for checking things that have been deleted, // Useful for checking things that have been deleted,
// but you no longer have access to, // but you no longer have access to,
// like private parties or users // like private parties or users
export async function checkExistence (collectionName, id) { export async function checkExistence (collectionName, id) {
let db = await connectToMongo();
return new Promise((resolve, reject) => { 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) => { collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
if (findError) return reject(findError); if (findError) return reject(findError);
let exists = docs.length > 0; let exists = docs.length > 0;
db.close();
resolve(exists); resolve(exists);
}); });
}); });
@@ -25,12 +20,10 @@ export async function checkExistence (collectionName, id) {
// Specifically helpful for the GET /groups tests, // Specifically helpful for the GET /groups tests,
// resets the db to an empty state and creates a tavern document // resets the db to an empty state and creates a tavern document
export async function resetHabiticaDB () { export async function resetHabiticaDB () {
let db = await connectToMongo();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.dropDatabase((dbErr) => { mongoose.connection.db.dropDatabase((dbErr) => {
if (dbErr) return reject(dbErr); if (dbErr) return reject(dbErr);
let groups = db.collection('groups'); let groups = mongoose.connection.db.collection('groups');
groups.insertOne({ groups.insertOne({
_id: 'habitrpg', _id: 'habitrpg',
@@ -42,7 +35,6 @@ export async function resetHabiticaDB () {
}, (insertErr) => { }, (insertErr) => {
if (insertErr) return reject(insertErr); if (insertErr) return reject(insertErr);
db.close();
resolve(); resolve();
}); });
}); });
@@ -50,39 +42,39 @@ export async function resetHabiticaDB () {
} }
export async function updateDocument (collectionName, doc, update) { export async function updateDocument (collectionName, doc, update) {
let db = await connectToMongo(); let collection = mongoose.connection.db.collection(collectionName);
let collection = db.collection(collectionName);
return new Promise((resolve) => { return new Promise((resolve) => {
collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => { collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => {
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`); if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
db.close();
resolve(); resolve();
}); });
}); });
} }
export async function getDocument (collectionName, doc) { export async function getDocument (collectionName, doc) {
let db = await connectToMongo(); let collection = mongoose.connection.db.collection(collectionName);
let collection = db.collection(collectionName);
return new Promise((resolve) => { return new Promise((resolve) => {
collection.findOne({ _id: doc._id }, (lookupErr, found) => { collection.findOne({ _id: doc._id }, (lookupErr, found) => {
if (lookupErr) throw new Error(`Error looking up ${collectionName}: ${lookupErr}`); if (lookupErr) throw new Error(`Error looking up ${collectionName}: ${lookupErr}`);
db.close();
resolve(found); resolve(found);
}); });
}); });
} }
export function connectToMongo () { before((done) => {
return new Promise((resolve, reject) => { mongoose.connection.on('open', (err) => {
mongo.connect(nconf.get('NODE_DB_URI'), (err, db) => { if (err) return done(err);
if (err) return reject(err); resetHabiticaDB()
.then(() => done())
resolve(db); .catch(done);
});
}); });
} });
after((done) => {
mongoose.connection.db.dropDatabase((err) => {
if (err) return done(err);
mongoose.connection.close(done);
});
});