Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added -g, --growl support. Closes #24

Tj Holowaychukbef4b105e7b5
diff --git a/Readme.md b/Readme.md
index 07e8ec00..8c0f14d3 100644
--- a/Readme.md
+++ b/Readme.md
@@ -8,6 +8,7 @@
- proper exit status for CI support etc
- auto-detects and disables coloring for non-ttys
- async test timeout support
+ - growl notification support
- extensible reporting
- dot matrix
- landing strip
diff --git a/bin/mocha b/bin/mocha
index e3d4d9b0..922018a2 100755
--- a/bin/mocha
+++ b/bin/mocha
@@ -5,6 +5,7 @@
*/
var program = require('commander')
+ , exec = require('child_process').exec
, path = require('path')
, mocha = require('../')
, reporters = mocha.reporters
@@ -30,6 +31,7 @@ program
.option('-r, --require <name>', 'require the given module')
.option('-R, --reporter <name>', 'specify the reporter to use', 'dot')
.option('-u, --ui <name>', 'specify user-interface (bdd|tdd|exports)', 'bdd')
+ .option('-g, --growl', 'enable growl support')
// report help
@@ -63,7 +65,7 @@ Error.stackTraceLimit = Infinity; // TODO: config
// reporter
var suite = new Suite('')
- , reporter = require('../lib/reporters/' + program.reporter)
+ , Reporter = require('../lib/reporters/' + program.reporter)
, ui = interfaces[program.ui](suite);
// files
@@ -94,6 +96,57 @@ files.forEach(function(file){
function run(suite) {
suite.emit('run');
var runner = new Runner(suite);
- new reporter(runner);
+ var reporter = new Reporter(runner);
+ if (program.growl) growl(runner, reporter)
runner.run();
+}
+
+// enable growl notifications
+
+function growl(runner, reporter) {
+ runner.on('end', function(){
+ var stats = reporter.stats;
+ if (stats.failures) {
+ var msg = stats.failures + ' of ' + stats.tests + ' tests failed:\n\n';
+ msg += titles(reporter.failures).join('\n');
+ notify(msg, { title: 'Fail' });
+ } else {
+ notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {
+ title: 'Pass'
+ });
+ }
+ });
+}
+
+// test titles
+
+function titles(tests) {
+ return tests.map(function(test, i){
+ return i + ') ' + test.title + ': ' + test.err.message;
+ });
+}
+
+// growl notification
+
+function notify(msg, options) {
+ var image
+ , args = ['growlnotify', '-m', '"' + msg + '"']
+ , options = options || {}
+ , fn = fn || function(){};
+
+ if (image = options.image) {
+ var flag, ext = path.extname(image).substr(1)
+ flag = flag || ext == 'icns' && 'iconpath'
+ flag = flag || /^[A-Z]/.test(image) && 'appIcon'
+ flag = flag || /^png|gif|jpe?g$/.test(ext) && 'image'
+ flag = flag || ext && (image = ext) && 'icon'
+ flag = flag || 'icon'
+ args.push('--' + flag, image)
+ }
+
+ if (options.sticky) args.push('--sticky');
+ if (options.priority) args.push('--priority', options.priority);
+ if (options.name) args.push('--name', options.name);
+ if (options.title) args.push(options.title);
+ exec(args.join(' '), fn);
}
\ No newline at end of file
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index a9319db5..70be9bf9 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -174,7 +174,9 @@ Base.prototype.epilogue = function(){
, stats.failures, stats.tests);
Base.list(this.failures);
console.log();
- process.exit(stats.failures);
+ process.nextTick(function(){
+ process.exit(stats.failures);
+ });
}
// pass
@@ -184,5 +186,7 @@ Base.prototype.epilogue = function(){
, stats.duration);
console.log();
- process.exit(0);
+ process.nextTick(function(){
+ process.exit(0);
+ });
};
diff --git a/package.json b/package.json
index 51d4863e..79b9390a 100644
--- a/package.json
+++ b/package.json
@@ -3,9 +3,6 @@
, "version": "0.0.1"
, "description": "Test framework inspired by JSpec, Expresso, Vows, & Qunit"
, "keywords": []
- , "devDependencies": {
- "should": "0.3.1"
- }
, "bin": { "mocha": "./bin/mocha" }
, "author": "TJ Holowaychuk <[email protected]>"
, "main": "index"
@@ -13,4 +10,7 @@
, "dependencies":{
"commander": "0.1.0"
}
+ , "devDependencies": {
+ "should": "0.3.1"
+ }
}