-
Notifications
You must be signed in to change notification settings - Fork 23
/
mocha.js
176 lines (157 loc) · 5.13 KB
/
mocha.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const NemoCore = require('nemo-core');
const cJSON = require('flatted');
const Mocha = require('mocha');
const debug = require('debug');
const log = debug('nemo:mocha:log');
function MochaRunner(runnerConfig) {
let testResults = [];
let mochaCompleteResolvers = null;
let runner;
let modulesToRequire = [];
let instanceConfig = runnerConfig.instanceConfig;
let mocha = new Mocha(instanceConfig.profile.conf.mocha);
let requireFromConfig = instanceConfig.profile.conf.mocha.require;
let mochaCompletePromise = new Promise(function (resolve, reject) {
mochaCompleteResolvers = {resolve, reject};
});
function createNemo() {
let driverConf;
if (typeof instanceConfig.profile.conf.driver === 'string') {
driverConf = `require:${instanceConfig.profile.conf.driver}`;
} else {
driverConf = Object.assign({}, instanceConfig.profile.conf.driver || {});
}
let dataConf = Object.assign({}, instanceConfig.profile.conf.data || {});
let NemoBaseConfig;
return NemoCore.Configure(instanceConfig.profile.conf.baseConfigPath)
.then(function (nemoBaseConfig) {
NemoBaseConfig = nemoBaseConfig;
if (instanceConfig.basedir) {
// confit use case
return NemoCore.Configure(instanceConfig.basedir, {
driver: driverConf,
data: dataConf
});
}
// plain JS config use case
return NemoCore.Configure({
driver: driverConf,
data: dataConf
});
})
.then((nemoCoreConfig) => {
NemoBaseConfig.merge(nemoCoreConfig);
return NemoCore(NemoBaseConfig);
});
}
function destroyNemo() {
log('destroyNemo: called');
if (this.nemo) {
return this.nemo && this.nemo.driver && this.nemo.driver.quit()
.then(function () {
log('destroyNemo: Quit driver');
return Promise.resolve();
});
}
}
function bindNemo(nemoCoreObject) {
nemoCoreObject.mocha = this;
nemoCoreObject.runner = {
reportPath: instanceConfig.profile.conf.reports,
emit: function (eventName, event = {}) {
runnerConfig.emitter.emit('custom', {
eventName,
event: cJSON.stringify(event)
});
}
};
this.nemo = nemoCoreObject;
return Promise.resolve();
}
function beforeSuite(Suite) {
log('suite event, suite %s, root: %s', Suite.title, Suite.root);
if (instanceConfig.profile.conf.driverPerTest) {
log('driverPerTest %s', instanceConfig.profile.conf.driverPerTest);
Suite.beforeEach(function () {
return createNemo()
.then(bindNemo.bind(this));
});
Suite._beforeEach.unshift(Suite._beforeEach.pop());
Suite.afterEach(destroyNemo);
return;
}
// createNemo beforeAll (one nemo per suite)
Suite.beforeAll(function () {
return createNemo()
.then(bindNemo.bind(this));
});
// add nemo's beforeAll to the FRONT of the beforeAll array
Suite._beforeAll.unshift(Suite._beforeAll.pop());
// afterAll, kill nemo
Suite.afterAll(function () {
destroyNemo.call(this);
});
}
function afterSuite(Evt) {
log('suite end called for %s which is root: %s', Evt.title, Evt.root);
}
function afterMocha(failures) {
log(`exit instance with failures ${!!failures}`);
mochaCompleteResolvers.resolve(testResults);
}
function prepareTestResult(test) {
if (test.err) {
test.errSafe = {
name: test.err.name || 'test err.name DNE',
stack: test.err.stack || 'test err.stack DNE'
};
}
test.fullTitleString = test.fullTitle();
return cJSON.stringify(test);
}
function afterEachTest(test, err) {
if (err) {
console.error(err);
}
let testCircularSafe = prepareTestResult(test);
runnerConfig.emitter.emit('test', testCircularSafe);
testResults.push(testCircularSafe);
}
// if an array was already declared, use it
if (requireFromConfig && Array.isArray(requireFromConfig)) {
modulesToRequire = requireFromConfig;
// else if it was a single module declared, format to array
} else if (requireFromConfig && typeof requireFromConfig === 'string') {
modulesToRequire = [requireFromConfig];
}
modulesToRequire.forEach(function (module) {
try {
require(module);
} catch (err) {
console.error(err);
}
});
// add files
instanceConfig.profile.conf.tests.forEach(function (file) {
log('%s: add file %s', instanceConfig.profile.tags.uid, file);
mocha.addFile(file);
});
mocha.loadFiles();
// make sure we really need to run()
const throwawayRunner = new Mocha.Runner(mocha.suite);
const matchingTests = (mocha.options.grep) ? throwawayRunner.grep(mocha.options.grep).total : throwawayRunner.total;
if (!matchingTests) {
log('no tests. exiting');
mochaCompleteResolvers.resolve(testResults);
return mochaCompletePromise;
}
log('start');
runner = mocha.run(afterMocha);
runner.on('pass', afterEachTest);
runner.on('fail', afterEachTest);
runner.on('suite', beforeSuite);
runner.on('suite end', afterSuite);
return mochaCompletePromise;
}
module.exports = MochaRunner;