Skip to content

Instantly share code, notes, and snippets.

@paularmstrong
Last active March 29, 2018 03:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paularmstrong/6540027 to your computer and use it in GitHub Desktop.
Save paularmstrong/6540027 to your computer and use it in GitHub Desktop.
Mocha test runner with Require.js
define(['modules/foo'], function (foo) {
describe('test foo', function () {
// ...
});
});
/*global mocha:true, expect:true, describe:true, it:true */
(function () {
if (!mocha) {
throw new Exception("mocha not loaded");
}
/*
* Mocha Events:
*
* - `start` execution started
* - `end` execution complete
* - `suite` (suite) test suite execution started
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
* - `hook` (hook) hook execution started
* - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
*
*/
var originalReporter = mocha._reporter,
blanketReporter,
oldRun,
oldCallback;
blanketReporter = function (runner) {
runner.on('start', function () {
blanket.setupCoverage();
});
runner.on('end', function () {
blanket.onTestsDone();
});
runner.on('suite', function () {
blanket.onModuleStart();
});
runner.on('test', function () {
blanket.onTestStart();
});
runner.on('test end', function (test) {
blanket.onTestDone(test.parent.tests.length,
test.state === 'passed');
});
//I dont know why these became global leaks
runner.globals(['stats', 'failures', 'runner']);
originalReporter(runner);
};
mocha.reporter(blanketReporter);
oldRun = (window.mochaPhantomJS) ? mochaPhantomJS.run : mocha.run;
mocha.run = function (finishCallback) {
oldCallback = finishCallback;
console.log("waiting for blanket...");
};
blanket.beforeStartTestRunner({
callback: function () {
if (!blanket.options("existingRequireJS")){
oldRun(oldCallback);
}
mocha.run = oldRun;
oldRun();
}
});
})();
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>mocha.test.html</title>
<script src="mocha/mocha.js"></script>
<script src="expectjs/expect.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
</script>
<script>
var require = {
baseUrl: '../'
};
</script>
<script src="../lib/require.js"></script>
<script src="blanket.js" data-cover-adapter="mocha-blanket.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
var files = window.location.search.replace('?files=', '').split(',');
files = files.map(function (file) {
return file.replace('.js', '');
});
files.push('test/mocha-blanket');
require(files, function (e) {
mocha.setup('bdd');
// We also run our tests in phantomjs, which requires npm module mocha-phantomjs
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment