☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added -w, --watch. Closes #72
TJ Holowaychukac30906f2909
diff --git a/bin/mocha b/bin/mochaindex ed441d27..c4c56c53 100755--- a/bin/mocha+++ b/bin/mocha@@ -43,6 +43,7 @@ program.option('-g, --grep <pattern>', 'only run tests matching <pattern>').option('-t, --timeout <ms>', 'set test-case timeout in milliseconds [2000]').option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]', parseInt)+ .option('-w, --watch', 'watch test files for changes').option('-C, --no-colors', 'force disabling of colors').option('-c, --colors', 'force enabling of colors').option('-G, --growl', 'enable growl notification support')@@ -157,29 +158,75 @@ if (!files.length) {});}-// require test files before running-// the root suite+// --watch-var pending = files.length;-files.forEach(function(file){- file = join(cwd, file);- suite.emit('pre-require', global, file);- suite.emit('require', require(file), file);- suite.emit('post-require', global, file);- --pending || run(suite);+if (program.watch) {+ console.log();+ hideCursor();+ process.on('SIGINT', function(){+ showCursor();+ console.log('\n');+ process.exit();+ });++ var frames = [+ ' \033[96m◜ \033[90mwatching\033[0m'+ , ' \033[96m◠ \033[90mwatching\033[0m'+ , ' \033[96m◝ \033[90mwatching\033[0m'+ , ' \033[96m◞ \033[90mwatching\033[0m'+ , ' \033[96m◡ \033[90mwatching\033[0m'+ , ' \033[96m◟ \033[90mwatching\033[0m'+ ];++ play(frames);++ mocha.watch(files, function(){+ stop()+ suite = suite.clone();+ ui = interfaces[program.ui](suite);+ Suite.reset();+ load(files, function(){+ run(suite, function(){+ play(frames);+ });+ });+ });++ return;+}++// load++load(files, function(){+ run(suite, process.exit);});+// require test files before+// running the root suite++function load(files, fn) {+ var pending = files.length;+ files.forEach(function(file){+ file = join(cwd, file);+ delete require.cache[file];+ suite.emit('pre-require', global, file);+ suite.emit('require', require(file), file);+ suite.emit('post-require', global, file);+ --pending || fn();+ });+}+// run the given suite-function run(suite) {+function run(suite, fn) {suite.emit('run');var runner = new Runner(suite);var reporter = new Reporter(runner);runner.globals(program.globals);if (program.ignoreLeaks) runner.ignoreLeaks = true;if (program.grep) runner.grep(new RegExp(program.grep));- if (program.growl) growl(runner, reporter)- runner.run();+ if (program.growl) growl(runner, reporter);+ runner.run(fn);}// enable growl notifications@@ -224,8 +271,49 @@ function notify(msg, options) {exec(args.join(' '), fn);}-// parse comma delimited list+/**+ * Parse list.+ */function list(str) {return str.split(/ *, */);}++/**+ * Hide the cursor.+ */++function hideCursor(){+ process.stdout.write('\033[?25l');+};++/**+ * Show the cursor.+ */++function showCursor(){+ process.stdout.write('\033[?25h');+};++/**+ * Stop play()ing.+ */++function stop() {+ clearInterval(play.timer);+}++/**+ * Play the given array of strings.+ */++function play(arr, interval) {+ var len = arr.length+ , interval = interval || 100+ , i = 0;++ play.timer = setInterval(function(){+ var str = arr[i++ % len];+ process.stdout.write('\r' + str);+ }, interval);+}diff --git a/lib/mocha.js b/lib/mocha.jsindex 58c9ab7b..5c8fd1ee 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -17,4 +17,5 @@ exports.Runnable = require('./runnable');exports.Runner = require('./runner');exports.Suite = require('./suite');exports.Hook = require('./hook');-exports.Test = require('./test');\ No newline at end of file+exports.Test = require('./test');+exports.watch = require('./watch');\ No newline at end of filediff --git a/lib/reporters/base.js b/lib/reporters/base.jsindex 72b390a1..04c4a58c 100644--- a/lib/reporters/base.js+++ b/lib/reporters/base.js@@ -209,9 +209,6 @@ Base.prototype.epilogue = function(){console.error(fmt, stats.failures, this.runner.total);Base.list(this.failures);console.error();- process.nextTick(function(){- process.exit(stats.failures);- });return;}@@ -222,7 +219,4 @@ Base.prototype.epilogue = function(){console.log(fmt, stats.tests || 0, stats.duration);console.log();- process.nextTick(function(){- process.exit(0);- });};diff --git a/lib/reporters/doc.js b/lib/reporters/doc.jsindex 8175e433..cf7e7128 100644--- a/lib/reporters/doc.js+++ b/lib/reporters/doc.js@@ -53,10 +53,6 @@ function Doc(runner) {var code = utils.escape(clean(test.fn.toString()));console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);});-- runner.on('end', function(){- process.exit(stats.failures);- });}/**diff --git a/lib/reporters/html.js b/lib/reporters/html.jsindex 3297d4ec..7fefa2d2 100644--- a/lib/reporters/html.js+++ b/lib/reporters/html.js@@ -95,10 +95,6 @@ function HTML(runner) {}stack[0].append(el);});-- runner.on('end', function(){- process.exit(stats.failures);- });}function error(msg) {diff --git a/lib/reporters/json-stream.js b/lib/reporters/json-stream.jsindex f6956791..7cb8fbed 100644--- a/lib/reporters/json-stream.js+++ b/lib/reporters/json-stream.js@@ -39,9 +39,7 @@ function List(runner) {});runner.on('end', function(){- process.stdout.write(JSON.stringify(['end', self.stats]), function(){- process.exit(0);- });+ process.stdout.write(JSON.stringify(['end', self.stats]));});}diff --git a/lib/reporters/json.js b/lib/reporters/json.jsindex d6edce98..ef5c2ff8 100644--- a/lib/reporters/json.js+++ b/lib/reporters/json.js@@ -48,9 +48,7 @@ function JSONReporter(runner) {, passes: passes.map(clean)};- process.stdout.write(JSON.stringify(obj), function(){- process.exit(0);- });+ process.stdout.write(JSON.stringify(obj));});}diff --git a/lib/reporters/tap.js b/lib/reporters/tap.jsindex 15e9021f..e0087fe2 100644--- a/lib/reporters/tap.js+++ b/lib/reporters/tap.js@@ -44,8 +44,4 @@ function TAP(runner) {console.log('not ok %d %s', n, test.fullTitle());console.log(err.stack.replace(/^/gm, ' '));});-- runner.on('end', function(){- process.exit(stats.failures);- });}\ No newline at end of filediff --git a/lib/runner.js b/lib/runner.jsindex 2ece55b9..2f9be465 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -37,6 +37,7 @@ function Runner(suite) {this._globals = [];this.suite = suite;this.total = suite.total();+ this.failures = 0;this.on('test end', function(test){ self.checkGlobals(test); });this.on('hook end', function(hook){ self.checkGlobals(hook); });this.grep(/.*/);@@ -108,6 +109,7 @@ Runner.prototype.checkGlobals = function(test){*/Runner.prototype.fail = function(test, err){+ ++this.failures;test.failed = true;this.emit('fail', test, err);};@@ -115,7 +117,7 @@ Runner.prototype.fail = function(test, err){/*** Fail the given `hook` with `err`.*- * Hook failures (currently) hard-exit due+ * Hook failures (currently) hard-end due* to that fact that a failing hook will* surely cause subsequent tests to fail,* causing jumbled reporting.@@ -126,9 +128,9 @@ Runner.prototype.fail = function(test, err){*/Runner.prototype.failHook = function(hook, err){+ ++this.failures;this.fail(hook, err);this.emit('end');- process.exit(0);};/**@@ -353,14 +355,22 @@ Runner.prototype.runSuite = function(suite, fn){};/**- * Run the root suite.+ * Run the root suite and invoke `fn(failures)`+ * on completion.*+ * @param {Function} fn* @api public*/-Runner.prototype.run = function(){+Runner.prototype.run = function(fn){var self = this;+ // callback+ self.on('end', function(){+ process.removeListener('uncaughtException', uncaught);+ fn(self.failures);+ });+// run suitesthis.emit('start');this.runSuite(this.suite, function(){@@ -368,11 +378,13 @@ Runner.prototype.run = function(){});// uncaught exception- process.on('uncaughtException', function(err){+ function uncaught(err){self.fail(self.test, err);self.emit('test end', self.test);self.emit('end');- });+ }++ process.on('uncaughtException', uncaught);return this;};diff --git a/lib/suite.js b/lib/suite.jsindex 3783aec2..e992a77b 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -18,6 +18,16 @@ exports = module.exports = Suite;var map = {};+/**+ * Reset the suite map.+ *+ * @api public+ */++exports.reset = function(){+ map = {};+};+/*** Create a new `Suite` with the given `title`* and parent `Suite`. When a suite with the@@ -65,6 +75,19 @@ function Suite(title) {Suite.prototype.__proto__ = EventEmitter.prototype;+/**+ * Return a clone of this `Suite`.+ *+ * @return {Suite}+ * @api private+ */++Suite.prototype.clone = function(){+ var suite = new Suite(this.title);+ suite.timeout(this._timeout);+ return suite;+};+/*** Set timeout `ms` or short-hand such as "2s".*diff --git a/lib/watch.js b/lib/watch.jsnew file mode 100644index 00000000..3fb325cd--- /dev/null+++ b/lib/watch.js@@ -0,0 +1,15 @@++/**+ * Module dependencies.+ */++var fs = require('fs');++module.exports = function(paths, fn){+ var options = { interval: 100 };+ paths.forEach(function(path){+ fs.watchFile(path, options, function(curr, prev){+ if (prev.mtime < curr.mtime) fn(path);+ });+ });+};\ No newline at end of file