-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocha.js
124 lines (108 loc) · 4.06 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
var q = require('q');
/**
* Execute the Runner's test cases through Mocha.
*
* @param {Runner} runner The current Cerebro Runner.
* @param {Array} specs Array of Directory Path Strings.
* @return {q.Promise} Promise resolved with the test results
*/
exports.run = function(runner, specs) {
var Mocha = require('mocha'),
mocha = new Mocha(runner.getConfig().mochaOpts);
var deferred = q.defer();
// Mocha doesn't set up the ui until the pre-require event, so
// wait until then to load mocha-webdriver adapters as well.
mocha.suite.on('pre-require', function() {
try {
// We need to re-wrap all of the global functions, which selenium-webdriver/
// testing only does when it is required. So first we must remove it from
// the cache.
delete require.cache[require.resolve('selenium-webdriver/testing')];
var mochaAdapters = require('selenium-webdriver/testing');
global.after = mochaAdapters.after;
global.afterEach = mochaAdapters.afterEach;
global.before = mochaAdapters.before;
global.beforeEach = mochaAdapters.beforeEach;
// The implementation of mocha's it.only uses global.it, so since that has
// already been wrapped we must avoid wrapping it a second time.
// See Mocha.prototype.loadFiles and bdd's context.it.only for more details.
var originalOnly = global.it.only;
global.it = mochaAdapters.it;
global.it.only = global.iit = originalOnly;
global.it.skip = global.xit = mochaAdapters.xit;
} catch (err) {
deferred.reject(err);
}
});
mocha.loadFiles();
runner.runTestPreparer().then(function() {
specs.forEach(function(file) {
mocha.addFile(file);
});
var testResult = [];
var mochaRunner = mocha.run(function(failures) {
try {
if (runner.getConfig().onComplete) {
runner.getConfig().onComplete();
}
deferred.resolve({
failedCount: failures,
specResults: testResult
});
} catch (err) {
deferred.reject(err);
}
});
mochaRunner.on('test',function(test){
var testInfo ={
test_name :test.title,
suite_name : test.parent.title}
runner.emit('testStart', testInfo)
})
mochaRunner.on('test end',function(test){
var testInfo ={
test_name :test.title,
suite_name : test.parent.title}
runner.emit('testEnd', testInfo)
})
mochaRunner.on('suite end',function(test){
var testInfo ={
test_name :test.title,
suite_name : test.parent.title}
runner.emit('suiteEnd', testInfo)
})
mochaRunner.on('pass', function(test) {
var testInfo = {
name: test.title,
category: test.fullTitle().slice(0, -test.title.length).trim()
};
runner.emit('testPass', testInfo);
testResult.push({
description: test.title,
assertions: [{
passed: true
}],
duration: test.duration
});
});
mochaRunner.on('fail', function(test) {
var testInfo = {
name: test.title,
category: test.fullTitle().slice(0, -test.title.length).trim()
};
runner.emit('testFail', testInfo);
testResult.push({
description: test.title,
assertions: [{
passed: false,
errorMsg: test.err.message,
stackTrace: test.err.stack
}],
duration: test.duration
});
});
}).catch (function(reason) {
deferred.reject(reason);
});
return deferred.promise;
};