Skip to content

Allow deferred initialization of mocha test runner via a flag #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 63 additions & 45 deletions lib/hub/view/public/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
});

Expand Down
1 change: 1 addition & 0 deletions test/functional/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]);
});
Expand Down
32 changes: 32 additions & 0 deletions test/functional/fixture/mocha-async.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<title>Yeti Mocha Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../dep/dev/mocha.css" />
<script src="../../../dep/dev/mocha.js"></script>
<script src="../../../dep/dev/expect.js"></script>
<script type="text/javascript" src="../../../dep/dev/yui-test.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
mocha.asyncYeti = true;

//simulate an async initialization of mocha
//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();
}
});
</script>
</body>
</html>