process env NODE_ENV test process env DEBUG false process env TCHOP_MY

 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
process.env.NODE_ENV = 'test';
process.env.DEBUG = false;
process.env.TCHOP_MYSQL_DATABASE = `mixedzone-test-${Date.now()}`;
const { spawn } = require('child-process-promise');
const migrator = require('../migration/migrate');
const _ = require('lodash');
const { createDatabase, dropDatabase } = require('./helpers');
/**
* convert args to simple format
* @example
* command: "node runner.js modeName" args: { modeName: true }
* command: "node runner.js argX='my first test'" args: { argX: "my first test" }
*/
const args = process.argv.slice(2).reduce((acc, arg) => {
let [k, v = true] = arg.split('=');
acc[k] = v;
return acc;
}, {});
/**
* @params {boolean} [watch] - Watch files in the current working directory for changes
* @params {string} [grep] - Only run tests matching this string or regexp
* @return {Promise<void>}
*/
async function spawnTest ({ watch, grep }) {
const commandName = watch ? 'test:watch' : 'test';
const commandArgs = [];
if (_.isString(grep)) {
commandArgs.push([ '--grep', grep ]);
}
await spawn('npm', [ 'run', commandName, '--' ].concat(...commandArgs), { stdio: 'inherit' });
}
async function main () {
console.info('Preparing to launch tests in "%s" environment...', process.env.NODE_ENV);
try {
await createDatabase(process.env.TCHOP_MYSQL_DATABASE);
// INFO: Creates only database structure
await migrator('production', true);
await spawnTest({
grep: args.grep,
watch: args.watch,
});
} catch (err) {
console.error(err);
throw err;
} finally {
await dropDatabase(process.env.TCHOP_MYSQL_DATABASE).catch(() => { /* ignore */ });
}
}
main();