From b8b0c0b68650108d0618e23f908474c43a7d4b04 Mon Sep 17 00:00:00 2001 From: alex-seville Date: Sun, 2 Feb 2014 18:37:50 -0800 Subject: [PATCH 1/2] Allow deferred initialization of mocha test runner via a flag --- lib/hub/view/public/inject.js | 108 +++++++++++++---------- test/functional/cli.js | 1 + test/functional/fixture/mocha-async.html | 31 +++++++ 3 files changed, 95 insertions(+), 45 deletions(-) create mode 100644 test/functional/fixture/mocha-async.html diff --git a/lib/hub/view/public/inject.js b/lib/hub/view/public/inject.js index f41bc17e..ab84d49d 100644 --- a/lib/hub/view/public/inject.js +++ b/lib/hub/view/public/inject.js @@ -255,70 +255,88 @@ window.$yetify = function (options) { var self = this, tostring = {}.toString, mocha = win.mocha, - runner = mocha.run(), + runner = mocha.run, data = {}, tests = {}, passed = 0, failed = 0, total = 0; - function complete(results) { - self.fire("results", results); - } + //override the existing mocha.run + //so we can initialize it async if we need to + mocha.run = function () { - runner.ignoreLeaks = true; + //restore the override + mocha.run = runner; + //initialize mocha and get a reference to the runner + runner = runner(); - runner.on('test end', function (test) { - var suiteName = test.title; + function complete(results) { + self.fire("results", results); + } - tests[suiteName] = { - message: (test.state === 'failed') ? test.err.message : "", - result: (test.state === 'passed') ? true : "fail", - name: suiteName - }; + runner.ignoreLeaks = true; + + runner.on('test end', function (test) { + var suiteName = test.title; - passed = (test.state === 'passed') ? passed + 1 : passed; - failed = (test.state === 'failed') ? failed + 1 : failed; - total = total + 1; - }); + tests[suiteName] = { + message: (test.state === 'failed') ? test.err.message : "", + result: (test.state === 'passed') ? true : "fail", + name: suiteName + }; + passed = (test.state === 'passed') ? passed + 1 : passed; + failed = (test.state === 'failed') ? failed + 1 : failed; + total = total + 1; + }); - runner.on('suite end', function (module) { - if (module.suites.length === 0) { - var suiteName = module.fullTitle(), - i; - data[suiteName] = { - name: suiteName, - passed: passed, - failed: failed, - total: total - }; + runner.on('suite end', function (module) { + if (module.suites.length === 0) { + var suiteName = module.fullTitle(), + i; - for (i in tests) { - data[suiteName][tests[i].name] = { - result: tests[i].result, - message: tests[i].message, - name: tests[i].name + data[suiteName] = { + name: suiteName, + passed: passed, + failed: failed, + total: total }; + + for (i in tests) { + data[suiteName][tests[i].name] = { + result: tests[i].result, + message: tests[i].message, + name: tests[i].name + }; + } + + tests = {}; + passed = failed = total = 0; } + }); + runner.on('end', function (test) { + var results = data; - tests = {}; - passed = failed = total = 0; - } - }); - runner.on('end', function (test) { - var results = data; + results.passed = (runner.total - runner.failures) || 0; + results.failed = runner.failures || 0; + results.total = runner.total; + // TODO: How do I get the test suite runtime? + results.duration = 0; + results.name = document.title; - results.passed = (runner.total - runner.failures) || 0; - results.failed = runner.failures || 0; - results.total = runner.total; - // TODO: How do I get the test suite runtime? - results.duration = 0; - results.name = document.title; + complete(results); + }); + }; - complete(results); - }); + //don't run if we've explicitly flagged this + //mocha instance as being async initialized + //otherwise call `run` to set up the test bindings and + //start the test runner + if (!mocha.asyncYeti) { + mocha.run(); + } } }); diff --git a/test/functional/cli.js b/test/functional/cli.js index 3463b13c..9d48d604 100644 --- a/test/functional/cli.js +++ b/test/functional/cli.js @@ -146,6 +146,7 @@ vows.describe("Yeti CLI").addBatch({ __dirname + "/fixture/qunit.html", __dirname + "/fixture/jasmine.html", __dirname + "/fixture/mocha.html", + __dirname + "/fixture/mocha-async.html", __dirname + "/fixture/doh.html" ]); }); diff --git a/test/functional/fixture/mocha-async.html b/test/functional/fixture/mocha-async.html new file mode 100644 index 00000000..bde15c6a --- /dev/null +++ b/test/functional/fixture/mocha-async.html @@ -0,0 +1,31 @@ + + + + Yeti Mocha Test + + + + + + +
+ + + From 0d3903d3b072dd935514b6d5ac7fc83d0f305625 Mon Sep 17 00:00:00 2001 From: Alex Seville Date: Mon, 3 Feb 2014 12:31:56 -0800 Subject: [PATCH 2/2] Using YUI.use instead of setTimeout --- test/functional/fixture/mocha-async.html | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/functional/fixture/mocha-async.html b/test/functional/fixture/mocha-async.html index bde15c6a..11b1d8a7 100644 --- a/test/functional/fixture/mocha-async.html +++ b/test/functional/fixture/mocha-async.html @@ -6,6 +6,7 @@ +
@@ -14,18 +15,18 @@ mocha.asyncYeti = true; //simulate an async initialization of mocha - //using a timeout. - setTimeout(function(){ - describe('hi mocha', function(){ - it('should have a passing test', function(){ - expect(true).to.be(true); - }) - }); + //using a YUI use + YUI().use("test", function (Y) { + describe('hi mocha', function(){ + it('should have a passing test', function(){ + expect(true).to.be(true); + }) + }); - if(!("$yetify" in window)) { - var runner = mocha.run(); - } - }, 50); + if(!("$yetify" in window)) { + var runner = mocha.run(); + } + });