Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added: --watch all .js in the CWD. Closes #139

Tj Holowaychuk0b88e4be4ed1
diff --git a/Makefile b/Makefile
index cb9583a8..87df63ce 100644
--- a/Makefile
+++ b/Makefile
@@ -76,7 +76,7 @@ non-tty:
@cat /tmp/spec.out
watch:
- watch --interval=1 $(MAKE) mocha.{js,css}
+ @watch -q $(MAKE) mocha.{js,css}
tm:
mkdir -p $(TM_DEST)/$(TM_BUNDLE)
diff --git a/bin/_mocha b/bin/_mocha
index 3a25a6cc..254fdbf8 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -8,6 +8,7 @@ var program = require('commander')
, exec = require('child_process').exec
, path = require('path')
, mocha = require('../')
+ , utils = mocha.utils
, reporters = mocha.reporters
, interfaces = mocha.interfaces
, Runner = mocha.Runner
@@ -195,7 +196,7 @@ if (program.watch) {
play(frames);
- mocha.watch(files, function(){
+ utils.watch(utils.files(cwd), function(){
stop()
suite = suite.clone();
ui = interfaces[program.ui](suite);
diff --git a/lib/mocha.js b/lib/mocha.js
index 903bc342..01202bb2 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -19,4 +19,3 @@ exports.Runner = require('./runner');
exports.Suite = require('./suite');
exports.Hook = require('./hook');
exports.Test = require('./test');
-exports.watch = require('./watch');
diff --git a/lib/utils.js b/lib/utils.js
index 92e2a0d8..aff31595 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,4 +1,19 @@
+/**
+ * Module dependencies.
+ */
+
+var fs = require('fs')
+ , path = require('path')
+ , join = path.join
+ , debug = require('debug')('watch');
+
+/**
+ * Ignored directories.
+ */
+
+var ignore = ['node_modules', '.git'];
+
/**
* Escape special characters in the given string of html.
*
@@ -107,3 +122,54 @@ exports.keys = Object.keys || function(obj) {
return keys;
};
+
+/**
+ * Watch the given `files` for changes
+ * and invoke `fn(file)` on modification.
+ *
+ * @param {Array} files
+ * @param {Function} fn
+ * @api private
+ */
+
+exports.watch = function(files, fn){
+ var options = { interval: 100 };
+ files.forEach(function(file){
+ debug('file %s', file);
+ fs.watchFile(file, options, function(curr, prev){
+ if (prev.mtime < curr.mtime) fn(file);
+ });
+ });
+};
+
+/**
+ * Ignored files.
+ */
+
+function ignored(path){
+ return !~ignore.indexOf(path);
+}
+
+/**
+ * Lookup files in the given `dir`.
+ *
+ * @return {Array}
+ * @api public
+ */
+
+exports.files = function(dir, ret){
+ ret = ret || [];
+
+ fs.readdirSync(dir)
+ .filter(ignored)
+ .forEach(function(path){
+ path = join(dir, path);
+ if (fs.statSync(path).isDirectory()) {
+ exports.files(path, ret);
+ } else if (path.match(/\.js$/)) {
+ ret.push(path);
+ }
+ });
+
+ return ret;
+};
\ No newline at end of file
diff --git a/lib/watch.js b/lib/watch.js
deleted file mode 100644
index 4bc00088..00000000
--- a/lib/watch.js
+++ /dev/null
@@ -1,17 +0,0 @@
-
-/**
- * Module dependencies.
- */
-
-var fs = require('fs')
- , debug = require('debug')('watch');
-
-module.exports = function(paths, fn){
- var options = { interval: 100 };
- paths.forEach(function(path){
- debug('watch %s', path);
- fs.watchFile(path, options, function(curr, prev){
- if (prev.mtime < curr.mtime) fn(path);
- });
- });
-};
\ No newline at end of file