forked from adnanrahic/boilerplate-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
67 lines (56 loc) · 1.46 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = (testDir) => {
require('dotenv').config();
const Mocha = require('mocha');
const async = require('async');
const mocha = new Mocha();
//// Add test files
var files = Mocha.utils.lookupFiles(testDir, ['js'], true);
files.forEach(function (file) {
mocha.addFile(file);
});
global.__root = __dirname + '/';
//// Global export of mutil
global.mutil = {
getApp: getApp,
clearDB: clearDB,
getModel: getModel,
parseJSON: parseJSON
};
//// Chai - Configure
const chai = require('chai');
chai.use(require('chai-http'));
chai.use(require('chai-as-promised'));
//// run the server and get the app object
const server = require('./lib/server');
let appToReturn;
server.serve('test')
.then(function (app) {
appToReturn = app;
mocha.ui('bdd').run(code => {
process.exit(code)
}); // exit the node process on test end
})
.catch('Failed to start test server.');
///////////////////////////
//// MUTIL functions
///////////////////////////
function getApp() {
return appToReturn;
}
function clearDB(done) {
let mongoose = require('mongoose');
async.each(mongoose.models, function (model, next) {
model.remove(next);
}, done);
}
function getModel(model_name) {
try {
return require('mongoose').model(model_name);
} catch (err) {
return null;
}
}
function parseJSON(obj) {
return JSON.parse(JSON.stringify(obj));
}
}