Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Use Mocha prototype in browser

John Firebaughd5a5d1b279df
diff --git a/support/tail.js b/support/tail.js
index 4bc796a1..29005a5e 100644
--- a/support/tail.js
+++ b/support/tail.js
@@ -60,20 +60,13 @@ process.on = function(e, fn){
}
};
-/**
- * Expose mocha.
- */
-
-window.mocha = require('mocha');
-
// boot
;(function(){
- var utils = mocha.utils
- , options = {}
-
- // TODO: use new Mocha here... not mocha.grep etc
-
- mocha.suite = new mocha.Suite('', new mocha.Context());
+ /**
+ * Expose mocha.
+ */
+ var Mocha = window.Mocha = require('mocha'),
+ mocha = window.mocha = new Mocha({reporter: 'html'});
/**
* Highlight the given string of `js`.
@@ -107,7 +100,7 @@ window.mocha = require('mocha');
*/
function parse(qs) {
- return utils.reduce(qs.replace('?', '').split('&'), function(obj, pair){
+ return Mocha.utils.reduce(qs.replace('?', '').split('&'), function(obj, pair){
var i = pair.indexOf('=')
, key = pair.slice(0, i)
, val = pair.slice(++i);
@@ -118,11 +111,14 @@ window.mocha = require('mocha');
}
/**
- * Grep.
+ * Override ui to ensure that the ui functions are initialized.
+ * Normally this would happen in Mocha.prototype.loadFiles.
*/
- mocha.grep = function(str){
- options.grep = new RegExp(utils.escapeRegexp(str));
+ mocha.ui = function(ui) {
+ Mocha.prototype.ui.call(this, ui);
+ this.suite.emit('pre-require', window, null, this);
+ return this;
};
/**
@@ -130,14 +126,9 @@ window.mocha = require('mocha');
*/
mocha.setup = function(opts){
- if ('string' === typeof opts) options.ui = opts;
- else options = opts;
-
- ui = mocha.interfaces[options.ui];
- if (!ui) throw new Error('invalid mocha interface "' + ui + '"');
- if (options.timeout) mocha.suite.timeout(options.timeout);
- ui(mocha.suite);
- mocha.suite.emit('pre-require', window, null, mocha);
+ if ('string' === typeof opts) opts = {ui: opts};
+ for (var opt in opts) this[opt](opts[opt]);
+ return this;
};
/**
@@ -145,17 +136,16 @@ window.mocha = require('mocha');
*/
mocha.run = function(fn){
- mocha.suite.emit('run');
- var runner = new mocha.Runner(mocha.suite);
- var Reporter = options.reporter || mocha.reporters.HTML;
- var reporter = new Reporter(runner);
+ var options = this.options;
+ options.globals = options.globals || [];
+ options.globals.push('location');
+
var query = parse(window.location.search || "");
- if (query.grep) runner.grep(new RegExp(query.grep));
- if (options.grep) runner.grep(options.grep);
- if (options.ignoreLeaks) runner.ignoreLeaks = true;
- if (options.globals) runner.globals(options.globals);
- runner.globals(['location']);
- runner.on('end', highlightCode);
- return runner.run(fn);
+ if (query.grep) this.grep(query.grep);
+
+ return Mocha.prototype.run.call(this, function () {
+ highlightCode();
+ if (fn) fn();
+ });
};
})();
diff --git a/test/browser/opts.html b/test/browser/opts.html
index efbe16ca..31d29524 100644
--- a/test/browser/opts.html
+++ b/test/browser/opts.html
@@ -7,9 +7,8 @@
<script>
mocha.setup({
ui: 'bdd',
- globals: ['okGlobalA', 'okGlobalB', 'okGlobalC', 'callback*'],
- timeout: 1500
- })
+ globals: ['okGlobalA', 'okGlobalB', 'okGlobalC', 'callback*']
+ }).timeout(1500)
</script>
<script>
function assert(expr, msg) {