forked from GetStream/mongodb-activity-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-tests.js
89 lines (81 loc) · 2.34 KB
/
setup-tests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Mocha from 'mocha'
import config from './src/config'
import logger from './src/utils/logger'
import { dropDBs } from './test/utils'
/*
api.use((err, req, res, next) => {
if (err) {
logger.error(err.stack);
if (err.request) {
logger.error(`REQUEST = ${stringify(err.request)}`);
}
if (err.response) {
logger.error(`RESPONSE = ${stringify(err.response)}`);
}
}
next(err, req, res);
});
chai.use(chaiHttp);*/
function wrapMocha(onPrepare, onUnprepare) {
// Monkey-patch run method
const run = Mocha.prototype.run
//XXX: using function syntax instead of fat-arrow syntax
// to avoid implicit binding of 'this'
Mocha.prototype.run = function(done) {
const self = this
onPrepare()
.then(() => {
//XXX: ditto
run.call(self, function() {
if (typeof onUnprepare === 'function') {
onUnprepare.apply(this, arguments)
}
done.apply(this, arguments)
})
})
.catch(err => {
if (err instanceof Error) {
console.error(err.stack)
}
process.exit(1)
})
}
}
wrapMocha(
async () => {
if (!config.database.uri)
throw new Error('Missing MongoDB connection string. Check config')
if (!config.redis.uri)
throw new Error('Missing Redis connection string. Check config')
if (
!config.database.uri.includes('localhost') &&
!config.database.uri.includes('127.0.0.1')
)
throw new Error(
'MongoDB connection string contains non-local address. For safety reasons test suite can only connect to local databases. Check config',
)
if (
!config.redis.uri.includes('localhost') &&
!config.redis.uri.includes('127.0.0.1')
)
throw new Error(
'Redis connection string contains non-local address. For safety reasons test suite can only connect to local databases. Check config',
)
//XXX: drop all data before running tests
await dropDBs()
/*
fs.readdirSync(path.join(__dirname, 'src', 'routes')).forEach(file => {
if (file.endsWith('.js')) {
require(`./src/routes/${file}`)(api);
}
});*/
},
failures => {
//XXX: it seems Travis-ci is having trouble with process wrap-up procedures so lets
// allocate more time before shutting down
const timeout = process.env.TRAVIS ? 10000 : 1500
logger.info(`Terminating in ${timeout / 1000} seconds`)
//XXX: don't care about open connections
setTimeout(() => process.exit(failures ? 1 : 0), timeout)
},
)