forked from kmiyashiro/grunt-mocha
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbridge.js
104 lines (85 loc) · 2.45 KB
/
bridge.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
/*
* Is injected into the spec runner file
* Copyright (c) 2012 Kelly Miyashiro
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
/*global mocha:true, alert:true, window:true */
(function() {
var MOCHA_EVENTS = [
'start',
'test',
'test end',
'suite',
'suite end',
'fail',
'pass',
'pending',
'end'
];
// Send messages to the parent phantom.js process via callPhantom
// http://phantomjs.org/api/webpage/handler/on-callback.html
function sendMessage(cmd, data) {
window.callPhantom({
cmd: cmd,
data: data
});
}
// Create a listener who'll bubble events from PhantomJS to Grunt
function createGruntListener(ev, runner) {
runner.on(ev, function(test, err) {
var data = {
err: err
};
if (test) {
data.title = test.title;
data.titlePath = test.titlePath();
data.fullTitle = test.fullTitle();
data.state = test.state;
data.duration = test.duration;
data.slow = test.slow;
data.pending = test.isPending();
}
sendMessage('mocha.' + ev, data);
});
}
// 1.4.2 moved reporters to Mocha instead of mocha
var mochaInstance = window.Mocha || window.mocha;
var GruntReporter = function(runner){
if (!mochaInstance) {
throw new Error('Mocha was not found, make sure you include Mocha in your HTML spec file.');
}
// Setup HTML reporter to output data on the screen
mochaInstance.reporters.HTML.call(this, runner);
// Create a Grunt listener for each Mocha events
for (var i = 0; i < MOCHA_EVENTS.length; i++) {
createGruntListener(MOCHA_EVENTS[i], runner);
}
};
var Klass = function () {};
Klass.prototype = mochaInstance.reporters.HTML.prototype;
GruntReporter.prototype = new Klass();
var options = window.PHANTOMJS;
// Default mocha options
var config = {
ui: 'bdd',
ignoreLeaks: true,
reporter: GruntReporter
};
// If options is a string, assume it is to set the UI (bdd/tdd etc)
if (typeof options === 'string') {
config.ui = options;
}
// Extend defaults with passed options
if (typeof options === 'object' && options !== null) {
for (var key in options.mocha) {
config[key] = options.mocha[key];
}
}
mocha.setup(config);
// task option `run`, automatically runs mocha for grunt only
if (options && options.run) {
mocha.run();
}
}());