-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
86 lines (71 loc) · 2.23 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*!
* Boilerplate Node.js Express
* https://github.com/tendadigital
* Copyright (c) 2016-2018 Tenda Digital
* Released under the MIT license
*/
const glob = require('glob')
const chalk = require('chalk')
const Mocha = require('mocha')
const app = require('./app')
// this function receives another function and test it to see if it's throws the expected error
global.assertFuncThrows = async (expectedError, fun, ...context) => {
try {
await fun(...context)
throw new Error(`Should have thrown ${expectedError}, but succeded`)
} catch (e) {
if (e.name != expectedError) {
throw e
}
}
}
async function test() {
console.info()
console.info('Bootstrapping basic components...')
console.info()
await app.bootstrap(['config', 'i18n', 'helpers', 'mongo', 'models', 'redirect'])
// Security Checks before starting tests
if (app.config.MONGO_URI && !app.config.MONGO_URI.includes('localhost') && app.config.isProduction) {
throw new Error(chalk.red('You cannot test on a non local MongoDB.\n'
+ 'Change MONGO_URI to default localhost before testing')
)
}
let mocha = new Mocha({ timeout: 20000 })
// Find files for testing
let testFiles = glob.sync('**/*spec.js', {
ignore: ['node_modules/**']
})
// Add files to mocha
testFiles.forEach(mocha.addFile.bind(mocha))
// Default passes to 0, in case there is only syncronous tests running
let stats = {passes: 0}
// Run tests and save stats
stats = mocha.run(function(failures) {
// Exits if less then 10 tests are being run (in case .only is used)
if (!failures && stats.passes < 9) {
console.error(chalk.red('Did not performed full testing:'))
console.error(chalk.red(' - Remove `describe.only` on tests'))
console.log()
return process.exit(1)
}
process.exit(!!failures)
}).stats
// creating some helpers
Object.defineProperty(Array.prototype, 'elToString', {
enumerable: false,
value: function() { return this.sort().map(s => s.toString()) }
})
}
process.on('unhandledRejection', (e) => {
console.error('Unhandled Rejection')
console.error(e.stack)
process.exit(1)
})
;(async function (){
try {
await test()
} catch (e) {
console.error(e)
process.exit(1)
}
})()