Skip to content
Browse files

Add support for all the standard mocha reporters

  • Loading branch information...
1 parent 00b16b8 commit 1124d1babba6d6ad49e38ee20dc6ea0f5c964a21 @ForbesLindesay committed
Showing with 55 additions and 34 deletions.
  1. +25 −20 client.js
  2. +1 −0 component.json
  3. +26 −12 index.js
  4. +3 −2 package.json
View
45 client.js
@@ -1,31 +1,36 @@
+var jssn = require('jssn');
/**
* Listen to `runner` events to populate a global
- * `.mochaResults` var which may be used by selenium
+ * `.stream()` function which may be used by selenium
* to report on results.
*
- * cloud(mocha.run());
+ * cloud(mocha).run();
*
- * @param {Runner} runner
+ * @param {Mocha} mocha
* @api public
*/
-module.exports = function(runner){
- var failed = [];
+module.exports = function mochaCloud(mocha) {
+ var Mocha = mocha.constructor;
+
+ var events = [];
+ window.stream = function () {
+ var e = jssn.stringify(events);
+ events = [];
+ return e;
+ };
- runner.on('fail', function(test, err){
- failed.push({
- title: test.title,
- fullTitle: test.fullTitle(),
- error: {
- message: err.message,
- stack: err.stack
- }
- });
- });
+ var HTML = Mocha.reporters.HTML;
+ function SauceLabs(runner, root) {
+ var emit = runner.emit;
+ runner.emit = function () {
+ emit.apply(this, arguments);
+ events.push(arguments)
+ };
+ HTML.call(this, runner, root);
+ }
+ SauceLabs.prototype = HTML.prototype;
- runner.on('end', function(){
- runner.stats.failed = failed;
- global.mochaResults = runner.stats;
- });
-};
+ return mocha.reporter(SauceLabs);
+}
View
1 component.json
@@ -5,5 +5,6 @@
"keywords": ["mocha", "test", "tdd", "bdd", "cloud", "selenium"],
"scripts": ["client.js"],
"main": "client.js",
+ "dependencies": {"ForbesLindesay/jssn": "*"},
"license": "MIT"
}
View
38 index.js
@@ -6,7 +6,9 @@
var Emitter = require('events').EventEmitter
, debug = require('debug')('mocha-cloud')
, Batch = require('batch')
- , wd = require('wd');
+ , wd = require('wd')
+ , mocha = require('mocha')
+ , jssn = require('jssn');
/**
* Expose `Cloud`.
@@ -108,31 +110,43 @@ Cloud.prototype.start = function(fn){
conf.name = self.name;
batch.push(function(done){
+ var runner = new Emitter();
debug('running %s %s %s', conf.browserName, conf.version, conf.platform);
var browser = wd.remote('ondemand.saucelabs.com', 80, self.user, self.key);
- self.emit('init', conf);
+ self.emit('init', conf, runner);
+ runner.on('end', function () {
+ self.emit('end', conf, runner);
+ });
browser.init(conf, function(){
debug('open %s', self._url);
- self.emit('start', conf);
+ self.emit('start', conf, runner);
browser.get(self._url, function(err){
if (err) return done(err);
function wait() {
- browser.eval('window.mochaResults', function(err, res){
+ browser.eval('window.stream()', function(err, res){
if (err) return done(err);
- if (!res) {
- debug('waiting for results');
- setTimeout(wait, 1000);
- return;
+ var ended = false;
+ var log = jssn.parse(res, mocha);
+
+ for (var i = 0; i < log.length; i++) {
+ debug('Event %s: %j', log[i][0], log[i]);
+ if (log[i][0] === 'end') ended = true;
+ runner.emit.apply(runner, log[i]);
}
- debug('results %j', res);
- self.emit('end', conf, res);
- browser.quit();
- done(null, res);
+ if (!ended) {
+ debug('still running');
+ setTimeout(wait, 1000);
+ } else {
+ debug('tests completed');
+ self.emit('end', conf, res);
+ browser.quit();
+ done(null, res);
+ }
});
}
View
5 package.json
@@ -7,10 +7,11 @@
"dependencies": {
"wd": "0.0.26",
"debug": "~0.7.0",
- "batch": "~0.2.1"
+ "batch": "~0.2.1",
+ "mocha": "*",
+ "jssn": "*"
},
"devDependencies": {
- "mocha": "*",
"should": "*"
},
"main": "index"

0 comments on commit 1124d1b

Please sign in to comment.
Something went wrong with that request. Please try again.