Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Initial commit

Tj Holowaychukd6a539ee2b08
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..fbb64d6a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+node_modules
+*.sock
+testing
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 00000000..f1250e58
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
diff --git a/History.md b/History.md
new file mode 100644
index 00000000..c8aa68fa
--- /dev/null
+++ b/History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000..36a3ed7d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @echo "populate me"
+
+.PHONY: test
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 00000000..13907b91
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,104 @@
+
+# mocha
+
+ Mocha aims to take the best of several popular frameworks. BDD-style inspired by JSpec, parallelism inspired by Expresso, opt-in serial execution from Vows, and assertion expectancy from Qunit.
+
+```js
+describe('Array', function(){
+ describe('#indexOf()', function(){
+ it('should return -1 when not present', function(){
+ [1,2,3].indexOf(4).should.equal(-1);
+ });
+
+ it('should return the index when present', function(){
+ [1,2,3].indexOf(3).should.equal(2);
+ [1,2,3].indexOf(2).should.equal(1);
+ [1,2,3].indexOf(1).should.equal(0);
+ });
+ });
+});
+```
+
+## Features
+
+ - proper exit status for CI support etc
+ - auto-detects and disables coloring for non-ttys
+ - async test timeout support
+ - extensible reporting
+ - dot matrix reporter
+ - landing strip reporter
+ - test-anything-protocol (TAP) producer
+ - JSON reporter
+ - streaming JSON reporter
+
+## Todo
+
+ [ ] spec style
+ [ ] export style
+ [ ] vows style
+ [ ] tdd style
+ [ ] CLI
+ [ ] before callbacks
+ [ ] after callbacks
+ [ ] todo stdout only
+ [√] nesting
+ [ ] nested concat titles
+ [ ] mutation cov etc
+ [ ] browser support
+ [ ] hook for should.js?
+ [ ] assertions expected
+ [√] timeouts, never hang
+ [√] explicit done when arity > 1
+ [ ] reporting
+ [√] plain text
+ [√] ansi-escape colored text
+ [ ] html
+ [ ] TAP
+ [ ] json
+ [ ] xunit
+ [ ] pure-js code coverage
+ [ ] optional parallelism via processes
+ [ ] optional opt-in block-based serial execution
+ [ ] CI-friendly (exit code)
+ [ ] global leak reporting
+ [ ] ability to target any test(s) to run
+
+## Best practices
+
+### Makefiles
+
+ Be kind and don't make developers hunt around in your docs to figure
+ out how to run the tests, add a `make test` target to your _Makefile_:
+
+```
+test:
+ ./node_modules/.bin/mocha \
+ --reporter list
+
+.PHONY: test
+```
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 TJ Holowaychuk <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/bin/mocha b/bin/mocha
new file mode 100755
index 00000000..08bc782e
--- /dev/null
+++ b/bin/mocha
@@ -0,0 +1,95 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+var program = require('commander')
+ , path = require('path')
+ , mocha = require('../')
+ , reporters = mocha.reporters
+ , interfaces = mocha.interfaces
+ , Runner = mocha.Runner
+ , Suite = mocha.Suite
+ , vm = require('vm')
+ , fs = require('fs')
+ , join = path.join
+ , cwd = process.cwd();
+
+/**
+ * Files.
+ */
+
+var files = [];
+
+// options
+
+program
+ .version(mocha.version)
+ .usage('[options] <files>')
+ .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')
+
+// report help
+
+program.on('--help', function(){
+ console.log(' Reporters:');
+ console.log('');
+ console.log(' dot - dot matrix');
+ console.log(' json - single json object');
+ console.log(' progress - progress bar');
+ console.log(' list - spec-style listing');
+ console.log(' tap - test-anything-protocol');
+ console.log(' landing - unicode landing strip');
+ console.log(' json-stream - newline delimited json events');
+ console.log('');
+});
+
+// -r, --require
+
+program.on('require', function(mod){
+ require(mod);
+});
+
+// parse args
+
+program.parse(process.argv);
+
+// reporter
+
+var suite = new Suite('')
+ , reporter = require('../lib/reporters/' + program.reporter)
+ , ui = interfaces[program.ui](suite);
+
+// files
+
+var files = program.args;
+
+// display help when no files are given
+
+if (!files.length) {
+ process.stdout.write(program.helpInformation());
+ process.exit(0);
+}
+
+var pending = files.length;
+files.forEach(function(file){
+ file = require.resolve(join(cwd, file));
+ fs.readFile(file, 'utf8', function(err, str){
+ if (err) throw err;
+ var context = {};
+ for (var key in global) context[key] = global[key];
+ suite.emit('file', context, file);
+ vm.runInNewContext(str, context, file);
+ suite.emit('file end', context, file);
+ --pending || run(suite);
+ });
+});
+
+function run(suite) {
+ suite.emit('run');
+ var runner = new Runner(suite);
+ new reporter(runner);
+ runner.run();
+}
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 00000000..238f0f16
--- /dev/null
+++ b/index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/mocha');
\ No newline at end of file
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js
new file mode 100644
index 00000000..169fec40
--- /dev/null
+++ b/lib/interfaces/bdd.js
@@ -0,0 +1,55 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Suite = require('../suite')
+ , Test = require('../test');
+
+/**
+ * BDD-style interface:
+ *
+ * describe('Array', function(){
+ * describe('#indexOf()', function(){
+ * it('should return -1 when not present', function(){
+ *
+ * });
+ *
+ * it('should return the index when present', function(){
+ *
+ * });
+ * });
+ * });
+ *
+ */
+
+module.exports = function(suite){
+ var suites = [suite];
+
+ suite.on('file', function(context, file){
+
+ /**
+ * Describe a "suite" with the given `title`
+ * and callback `fn` containing nested suites
+ * and/or tests.
+ */
+
+ context.describe = function(title, fn){
+ var suite = suite = new Suite(title);
+ suites[0].addSuite(suite);
+ suites.unshift(suite);
+ fn();
+ suites.shift();
+ };
+
+ /**
+ * Describe a specification or test-case
+ * with the given `title` and callback `fn`
+ * acting as a thunk.
+ */
+
+ context.it = function(title, fn){
+ suites[0].addTest(new Test(title, fn));
+ };
+ });
+};
\ No newline at end of file
diff --git a/lib/interfaces/exports.js b/lib/interfaces/exports.js
new file mode 100644
index 00000000..e0e9a583
--- /dev/null
+++ b/lib/interfaces/exports.js
@@ -0,0 +1,52 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Suite = require('../suite')
+ , Test = require('../test');
+
+/**
+ * TDD-style interface:
+ *
+ * suite('Array', function(){
+ * suite('#indexOf()', function(){
+ * test('should return -1 when not present', function(){
+ *
+ * });
+ *
+ * test('should return the index when present', function(){
+ *
+ * });
+ * });
+ * });
+ *
+ */
+
+module.exports = function(suite){
+ var suites = [suite];
+
+ suite.on('file', function(context, file){
+ context.module = {};
+ context.module.exports = {};
+ });
+
+ suite.on('file end', function(context, file){
+ var obj = context.module.exports;
+ visit(obj);
+ });
+
+ function visit(obj) {
+ var suite;
+ for (var key in obj) {
+ if ('function' == typeof obj[key]) {
+ suites[0].addTest(new Test(key, obj[key]));
+ } else {
+ suites[0].addSuite(suite = new Suite(key));
+ suites.unshift(suite);
+ visit(obj[key]);
+ suites.shift();
+ }
+ }
+ }
+};
\ No newline at end of file
diff --git a/lib/interfaces/index.js b/lib/interfaces/index.js
new file mode 100644
index 00000000..0dc76674
--- /dev/null
+++ b/lib/interfaces/index.js
@@ -0,0 +1,4 @@
+
+exports.bdd = require('./bdd');
+exports.tdd = require('./tdd');
+exports.exports = require('./exports');
\ No newline at end of file
diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.js
new file mode 100644
index 00000000..cbbd6bea
--- /dev/null
+++ b/lib/interfaces/tdd.js
@@ -0,0 +1,55 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Suite = require('../suite')
+ , Test = require('../test');
+
+/**
+ * TDD-style interface:
+ *
+ * suite('Array', function(){
+ * suite('#indexOf()', function(){
+ * test('should return -1 when not present', function(){
+ *
+ * });
+ *
+ * test('should return the index when present', function(){
+ *
+ * });
+ * });
+ * });
+ *
+ */
+
+module.exports = function(suite){
+ var suites = [suite];
+
+ suite.on('file', function(context, file){
+
+ /**
+ * Describe a "suite" with the given `title`
+ * and callback `fn` containing nested suites
+ * and/or tests.
+ */
+
+ context.suite = function(title, fn){
+ var suite = suite = new Suite(title);
+ suites[0].addSuite(suite);
+ suites.unshift(suite);
+ fn();
+ suites.shift();
+ };
+
+ /**
+ * Describe a specification or test-case
+ * with the given `title` and callback `fn`
+ * acting as a thunk.
+ */
+
+ context.test = function(title, fn){
+ suites[0].addTest(new Test(title, fn));
+ };
+ });
+};
\ No newline at end of file
diff --git a/lib/mocha.js b/lib/mocha.js
new file mode 100644
index 00000000..f0b56a78
--- /dev/null
+++ b/lib/mocha.js
@@ -0,0 +1,18 @@
+
+/*!
+ * mocha
+ * Copyright(c) 2011 TJ Holowaychuk <[email protected]>
+ * MIT Licensed
+ */
+
+/**
+ * Library version.
+ */
+
+exports.version = '0.0.1';
+
+exports.interfaces = require('./interfaces');
+exports.reporters = require('./reporters');
+exports.Runner = require('./runner');
+exports.Suite = require('./suite');
+exports.Test = require('./test');
\ No newline at end of file
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
new file mode 100644
index 00000000..e484d1fe
--- /dev/null
+++ b/lib/reporters/base.js
@@ -0,0 +1,159 @@
+
+/**
+ * Module dependencies.
+ */
+
+var tty = require('tty');
+
+/**
+ * Check if both stdio streams are associated with a tty.
+ */
+
+var isatty = tty.isatty(1) && tty.isatty(2);
+
+/**
+ * Expose `Base`.
+ */
+
+exports = module.exports = Base;
+
+/**
+ * Enable coloring by default.
+ */
+
+exports.useColors = isatty;
+
+/**
+ * Default color map.
+ */
+
+exports.colors = {
+ 'pass': 90
+ , 'pass message': 32
+ , 'fail': 31
+ , 'fail message': 31
+ , 'error title': 0
+ , 'error message': 31
+ , 'error stack': 90
+};
+
+/**
+ * Color `str` with the given `type`,
+ * allowing colors to be disabled,
+ * as well as user-defined color
+ * schemes.
+ *
+ * @param {String} type
+ * @param {String} str
+ * @return {String}
+ * @api private
+ */
+
+var color = exports.color = function(type, str) {
+ if (!exports.useColors) return str;
+ return '\033[' + exports.colors[type] + 'm' + str + '\033[0m';
+};
+
+/**
+ * Expose term window size, with some
+ * defaults for when stderr is not a tty.
+ */
+
+exports.window = {
+ width: isatty ? tty.getWindowSize(1)[1] : 75
+};
+
+/**
+ * Expose some basic cursor interactions
+ * that are common among reporters.
+ */
+
+exports.cursor = {
+ hide: function(){
+ process.stdout.write('\033[?25l');
+ },
+
+ show: function(){
+ process.stdout.write('\033[?25h');
+ }
+};
+
+/**
+ * Outut the given `failures` as a list.
+ *
+ * @param {Array} failures
+ * @api public
+ */
+
+exports.list = function(failures){
+ console.error();
+ failures.forEach(function(test, i){
+ // format
+ var fmt = color('error title', ' %s) %s: ')
+ + color('error message', '%s')
+ + color('error stack', '\n%s\n');
+
+ // stack
+ var stack = test.err.stack.replace(/^/gm, ' ');
+ console.error(fmt, i, test.title, test.err.message, stack);
+ });
+};
+
+/**
+ * Unhide cursor on ^C.
+ */
+
+process.on('SIGINT', function(){
+ exports.cursor.show();
+ console.log('\n');
+ process.exit(1);
+});
+
+/**
+ * Initialize a new `Base` reporter.
+ *
+ * All other reporters generally
+ * inherit from this reporter, providing
+ * stats such as test duration, number
+ * of tests passed / failed etc.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function Base(runner) {
+ var self = this
+ , stats = this.stats = {}
+ , failures = this.failures = [];
+
+ runner.on('start', function(){
+ stats.start = new Date;
+ });
+
+ runner.on('suite', function(suite){
+ stats.suites = stats.suites || 0;
+ stats.suites++;
+ });
+
+ runner.on('test end', function(test){
+ stats.tests = stats.tests || 0;
+ stats.tests++;
+ });
+
+ runner.on('pass', function(test){
+ stats.passes = stats.passes || 0;
+ stats.passes++;
+ });
+
+ runner.on('fail', function(test, err){
+ stats.failures = stats.failures || 0;
+ stats.failures++;
+ test.err = err;
+ failures.push(test);
+ });
+
+ runner.on('end', function(){
+ stats.end = new Date;
+ stats.duration = new Date - stats.start;
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js
new file mode 100644
index 00000000..d1f7ed74
--- /dev/null
+++ b/lib/reporters/dot.js
@@ -0,0 +1,60 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , color = Base.color;
+
+/**
+ * Expose `Dot`.
+ */
+
+exports = module.exports = Dot;
+
+/**
+ * Initialize a new `Dot` matrix test reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function Dot(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , width = Base.window.width * .75 | 0
+ , n = 0;
+
+ // tests started
+ runner.on('start', function(){
+ process.stdout.write('\n ');
+ });
+
+ // test passed, output a dot
+ runner.on('pass', function(test){
+ if (++n % width == 0) process.stdout.write('\n ');
+ process.stdout.write(color('pass', '.'));
+ });
+
+ // test failed, output a dot
+ runner.on('fail', function(test, err){
+ if (++n % width == 0) process.stdout.write('\n ');
+ process.stdout.write(color('fail', '.'));
+ });
+
+ // tests are complete, output some stats
+ // and the failures if any
+ runner.on('end', function(){
+ console.error('\n');
+ if (stats.failures) {
+ console.error(color('fail message', ' ✖ %d of %d tests failed'), stats.failures, stats.tests);
+ Base.list(self.failures);
+ } else {
+ console.error(color('pass message', ' ✔ %d tests completed in %dms'), stats.tests, stats.duration);
+ }
+ console.error();
+ process.exit(stats.failures);
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/index.js b/lib/reporters/index.js
new file mode 100644
index 00000000..40835c7a
--- /dev/null
+++ b/lib/reporters/index.js
@@ -0,0 +1,9 @@
+
+exports.Base = require('./base');
+exports.Dot = require('./dot');
+exports.TAP = require('./tap');
+exports.JSON = require('./json');
+exports.List = require('./list');
+exports.Progress = require('./progress');
+exports.Landing = require('./landing');
+exports.JSONStream = require('./json-stream');
diff --git a/lib/reporters/json-stream.js b/lib/reporters/json-stream.js
new file mode 100644
index 00000000..ef83dbdd
--- /dev/null
+++ b/lib/reporters/json-stream.js
@@ -0,0 +1,65 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , color = Base.color;
+
+/**
+ * Expose `List`.
+ */
+
+exports = module.exports = List;
+
+/**
+ * Initialize a new `List` test reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function List(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , total = runner.total;
+
+ // tests started
+ runner.on('start', function(){
+ console.log(JSON.stringify(['start', { total: total }]));
+ });
+
+ // test passed
+ runner.on('pass', function(test){
+ console.log(JSON.stringify(['pass', clean(test)]));
+ });
+
+ // test failed
+ runner.on('fail', function(test, err){
+ console.log(JSON.stringify(['fail', clean(test)]));
+ });
+
+ // tests are complete
+ runner.on('end', function(){
+ process.stdout.write(JSON.stringify(['end', self.stats]));
+ });
+}
+
+/**
+ * Return a plain-object representation of `test`
+ * free of cyclic properties etc.
+ *
+ * @param {Object} test
+ * @return {Object}
+ * @api private
+ */
+
+function clean(test) {
+ return {
+ title: test.title
+ , fullTitle: test.fullTitle()
+ , duration: test.duration
+ }
+}
\ No newline at end of file
diff --git a/lib/reporters/json.js b/lib/reporters/json.js
new file mode 100644
index 00000000..ef5c2ff8
--- /dev/null
+++ b/lib/reporters/json.js
@@ -0,0 +1,70 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , cursor = Base.cursor
+ , color = Base.color;
+
+/**
+ * Expose `JSON`.
+ */
+
+exports = module.exports = JSONReporter;
+
+/**
+ * Initialize a new `JSON` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function JSONReporter(runner) {
+ var self = this;
+ Base.call(this, runner);
+
+ var tests = []
+ , failures = []
+ , passes = [];
+
+ runner.on('test end', function(test){
+ tests.push(test);
+ });
+
+ runner.on('pass', function(test){
+ passes.push(test);
+ });
+
+ runner.on('fail', function(test){
+ failures.push(test);
+ });
+
+ runner.on('end', function(){
+ var obj = {
+ stats: self.stats
+ , tests: tests.map(clean)
+ , failures: failures.map(clean)
+ , passes: passes.map(clean)
+ };
+
+ process.stdout.write(JSON.stringify(obj));
+ });
+}
+
+/**
+ * Return a plain-object representation of `test`
+ * free of cyclic properties etc.
+ *
+ * @param {Object} test
+ * @return {Object}
+ * @api private
+ */
+
+function clean(test) {
+ return {
+ title: test.title
+ , fullTitle: test.fullTitle()
+ , duration: test.duration
+ }
+}
\ No newline at end of file
diff --git a/lib/reporters/landing.js b/lib/reporters/landing.js
new file mode 100644
index 00000000..5714a3f5
--- /dev/null
+++ b/lib/reporters/landing.js
@@ -0,0 +1,98 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , cursor = Base.cursor
+ , color = Base.color;
+
+/**
+ * Expose `Landing`.
+ */
+
+exports = module.exports = Landing;
+
+/**
+ * Airplane color.
+ */
+
+Base.colors.plane = 0;
+
+/**
+ * Airplane crash color.
+ */
+
+Base.colors['plane crash'] = 31;
+
+/**
+ * Runway color.
+ */
+
+Base.colors.runway = 90;
+
+/**
+ * Initialize a new `Landing` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function Landing(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , width = Base.window.width * .75 | 0
+ , total = runner.total
+ , stream = process.stdout
+ , plane = color('plane', '✈')
+ , crashed = -1
+ , n = 0;
+
+ function runway() {
+ var buf = Array(width).join('-');
+ return ' ' + color('runway', buf);
+ }
+
+ runner.on('start', function(){
+ stream.write('\n ');
+ cursor.hide();
+ });
+
+ runner.on('test end', function(test){
+ // check if the plane crashed
+ var col = -1 == crashed
+ ? width * ++n / total | 0
+ : crashed;
+
+ // show the crash
+ if (test.failed) {
+ plane = color('plane crash', '✈');
+ crashed = col;
+ }
+
+ // render landing strip
+ stream.write('\033[4F\n\n');
+ stream.write(runway());
+ stream.write('\n ');
+ stream.write(color('runway', Array(col).join('⋅')));
+ stream.write(plane)
+ stream.write(color('runway', Array(width - col).join('⋅') + '\n'));
+ stream.write(runway());
+ stream.write('\033[0m');
+ });
+
+ runner.on('end', function(){
+ cursor.show();
+ console.log('\n');
+ if (stats.failures) {
+ console.error(color('fail message', ' ✖ %d of the %d passengers didnt make it!'), stats.failures, stats.tests);
+ Base.list(self.failures);
+ } else {
+ console.error(color('pass message', ' ✔ %d passengers arrived safely in %dms!'), stats.tests, stats.duration);
+ }
+ console.log();
+ process.exit(stats.failures);
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/list.js b/lib/reporters/list.js
new file mode 100644
index 00000000..de172310
--- /dev/null
+++ b/lib/reporters/list.js
@@ -0,0 +1,57 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , color = Base.color;
+
+/**
+ * Expose `List`.
+ */
+
+exports = module.exports = List;
+
+/**
+ * Initialize a new `List` test reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function List(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , n = 0;
+
+ // tests started
+ runner.on('start', function(){
+ console.error();
+ });
+
+ // test passed
+ runner.on('pass', function(test){
+ console.error(color('pass', ' %s'), test.fullTitle());
+ });
+
+ // test failed
+ runner.on('fail', function(test, err){
+ console.error(color('fail', ' %d) %s'), n++, test.fullTitle());
+ });
+
+ // tests are complete, output some stats
+ // and the failures if any
+ runner.on('end', function(){
+ console.error();
+ if (stats.failures) {
+ console.error(color('fail message', ' ✖ %d of %d tests failed'), stats.failures, stats.tests);
+ Base.list(self.failures);
+ } else {
+ console.error(color('pass message', ' ✔ %d tests completed in %dms'), stats.tests, stats.duration);
+ }
+ console.error();
+ process.exit(stats.failures);
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js
new file mode 100644
index 00000000..111f220b
--- /dev/null
+++ b/lib/reporters/progress.js
@@ -0,0 +1,84 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , color = Base.color;
+
+/**
+ * Expose `Progress`.
+ */
+
+exports = module.exports = Progress;
+
+/**
+ * General progress bar color.
+ */
+
+Base.colors.progress = 90;
+
+/**
+ * Initialize a new `Progress` matrix test reporter.
+ *
+ * @param {Runner} runner
+ * @param {Object} options
+ * @api public
+ */
+
+function Progress(runner, options) {
+ Base.call(this, runner);
+
+ var self = this
+ , options = options || {}
+ , stats = this.stats
+ , width = Base.window.width * .50 | 0
+ , total = runner.total
+ , complete = 0
+ , max = Math.max;
+
+ // default chars
+ options.open = options.open || '[';
+ options.complete = options.complete || '=';
+ options.incomplete = options.incomplete || '⋅';
+ options.close = options.close || ']';
+ options.verbose = true;
+
+ // tests started
+ runner.on('start', function(){
+ console.log();
+ Base.cursor.hide();
+ });
+
+ // tests complete
+ runner.on('test end', function(){
+ var incomplete = total - complete
+ , percent = complete++ / total
+ , n = width * percent | 0
+ , i = width - n;
+
+ process.stdout.write('\r');
+ process.stdout.write(color('progress', ' ' + options.open));
+ process.stdout.write(Array(n).join(options.complete));
+ process.stdout.write(Array(i).join(options.incomplete));
+ process.stdout.write(color('progress', options.close));
+ if (options.verbose) {
+ process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
+ }
+ });
+
+ // tests are complete, output some stats
+ // and the failures if any
+ runner.on('end', function(){
+ Base.cursor.show();
+ console.log('\n');
+ if (stats.failures) {
+ console.error(color('fail message', ' ✖ %d of %d tests failed'), stats.failures, stats.tests);
+ Base.list(self.failures);
+ } else {
+ console.error(color('pass message', ' ✔ %d tests completed in %dms'), stats.tests, stats.duration);
+ }
+ console.error();
+ process.exit(stats.failures);
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js
new file mode 100644
index 00000000..6a936a5b
--- /dev/null
+++ b/lib/reporters/spec.js
@@ -0,0 +1,47 @@
+
+exports = module.exports = Spec;
+
+exports.useColors = true;
+
+exports.colors = {
+ suite: '90'
+ , pass: '32'
+ , fail: '31'
+};
+
+function color(type, str) {
+ if (!exports.useColors) return str;
+ return '\033[' + exports.colors[type] + 'm' + str + '\033[0m';
+}
+
+function Spec(runner) {
+ var indents = 0;
+
+ function indent() {
+ return Array(indents + 1).join(' ');
+ }
+
+ runner.on('start', function(){
+ console.log();
+ });
+
+ runner.on('suite', function(suite){
+ console.log(' %s', color('suite', suite.title));
+ });
+
+ runner.on('test', function(test){
+ console.log(' %s', color(test.title);
+ });
+
+ runner.on('pass', function(test){
+ console.log(' √');
+ });
+
+ runner.on('fail', function(test, err){
+ console.log(' x %s', err.message);
+ });
+
+ runner.on('end', function(){
+ console.log();
+ });
+}
\ No newline at end of file
diff --git a/lib/reporters/tap.js b/lib/reporters/tap.js
new file mode 100644
index 00000000..0dd9120c
--- /dev/null
+++ b/lib/reporters/tap.js
@@ -0,0 +1,46 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , cursor = Base.cursor
+ , color = Base.color;
+
+/**
+ * Expose `TAP`.
+ */
+
+exports = module.exports = TAP;
+
+/**
+ * Initialize a new `TAP` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function TAP(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , total = runner.total
+ , n = 1;
+
+ runner.on('start', function(){
+ console.log(' %d..%d', 1, total);
+ });
+
+ runner.on('test end', function(){
+ ++n;
+ });
+
+ runner.on('pass', function(test){
+ console.log(' ok %d %s', n, test.fullTitle());
+ });
+
+ runner.on('fail', function(test){
+ console.log(' not ok %d %s', n, test.fullTitle());
+ });
+}
\ No newline at end of file
diff --git a/lib/runner.js b/lib/runner.js
new file mode 100644
index 00000000..87bcc07e
--- /dev/null
+++ b/lib/runner.js
@@ -0,0 +1,107 @@
+
+/**
+ * Module dependencies.
+ */
+
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = Runner;
+
+/**
+ * Initialize a `Runner` for the given `suite`.
+ *
+ * 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
+ * - `pass` (test) test passed
+ * - `fail` (test, err) test failed
+ *
+ * @api public
+ */
+
+function Runner(suite) {
+ this.suite = suite;
+ this.total = suite.total();
+}
+
+/**
+ * Inherit from `EventEmitter.prototype`.
+ */
+
+Runner.prototype.__proto__ = EventEmitter.prototype;
+
+Runner.prototype.runTests = function(suite, fn){
+ var self = this
+ , tests = suite.tests;
+
+ function run(test) {
+ if (!test) return fn();
+ self.emit('test', test);
+
+ // run the test
+ try {
+ // async
+ if (test.async) {
+ test.run(function(err){
+ if (err) {
+ test.failed = true
+ self.emit('fail', test, err);
+ } else {
+ self.emit('pass', test);
+ }
+ self.emit('test end', test);
+ run(tests.shift());
+ });
+ // sync
+ } else {
+ test.run();
+ test.passed = true;
+ self.emit('pass', test);
+ }
+ } catch (err) {
+ test.failed = true;
+ self.emit('fail', test, err);
+ }
+
+ // run the next test
+ if (test.sync) {
+ self.emit('test end', test);
+ run(tests.shift());
+ }
+ }
+
+ run(tests.shift());
+};
+
+Runner.prototype.runSuite = function(suite, fn){
+ var self = this;
+
+ this.emit('suite', suite);
+
+ // run sub-suites
+ var pending = suite.suites.length + 1;
+ suite.suites.forEach(function(suite){
+ self.runSuite(suite, function(){
+ --pending || fn();
+ });
+ });
+
+ // run tests
+ this.runTests(suite, function(){
+ --pending || fn();
+ });
+};
+
+Runner.prototype.run = function(){
+ var self = this;
+ this.emit('start');
+ this.runSuite(this.suite, function(){
+ self.emit('end');
+ });
+ return this;
+};
diff --git a/lib/suite.js b/lib/suite.js
new file mode 100644
index 00000000..00231c32
--- /dev/null
+++ b/lib/suite.js
@@ -0,0 +1,46 @@
+
+/**
+ * Module dependencies.
+ */
+
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = Suite;
+
+function Suite(title) {
+ this.title = title;
+ this.suites = [];
+ this.tests = [];
+}
+
+/**
+ * Inherit from `EventEmitter.prototype`.
+ */
+
+Suite.prototype.__proto__ = EventEmitter.prototype;
+
+Suite.prototype.addSuite = function(suite){
+ suite.parent = this;
+ this.suites.push(suite);
+ return this;
+};
+
+Suite.prototype.addTest = function(test){
+ test.parent = this;
+ this.tests.push(test);
+ return this;
+};
+
+Suite.prototype.fullTitle = function(){
+ if (this.parent) {
+ var full = this.parent.fullTitle();
+ if (full) return full + ' ' + this.title;
+ }
+ return this.title;
+};
+
+Suite.prototype.total = function(){
+ return this.suites.reduce(function(sum, suite){
+ return sum + suite.total();
+ }, 0) + this.tests.length;
+};
diff --git a/lib/test.js b/lib/test.js
new file mode 100644
index 00000000..ce824b50
--- /dev/null
+++ b/lib/test.js
@@ -0,0 +1,46 @@
+
+module.exports = Test;
+
+function Test(title, fn) {
+ this.title = title;
+ this.fn = fn;
+ this.async = !! fn.length;
+ this.sync = ! this.async;
+ this.timeout(5000);
+}
+
+Test.prototype.timeout = function(ms){
+ this._timeout = ms;
+ return this;
+};
+
+Test.prototype.fullTitle = function(){
+ return this.parent.fullTitle() + ' ' + this.title;
+};
+
+Test.prototype.run = function(fn){
+ var timer
+ , self = this
+ , ms = this._timeout
+ , start = new Date;
+
+ // timeout
+ if (this.async) {
+ timer = setTimeout(function(){
+ fn(new Error('timeout of ' + ms + 'ms exceeded'));
+ }, ms);
+ }
+
+ // async
+ if (this.async) {
+ this.fn(function(err){
+ clearTimeout(timer);
+ self.duration = new Date - start;
+ fn(err);
+ });
+ // sync
+ } else {
+ this.fn();
+ this.duration = new Date - start;
+ }
+};
diff --git a/package.json b/package.json
new file mode 100644
index 00000000..17cba58e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,13 @@
+{
+ "name": "mocha"
+ , "version": "0.0.1"
+ , "description": "Test framework inspired by JSpec, Expresso, Vows, & Qunit"
+ , "keywords": []
+ , "devDependencies": {
+ "commander": "0.1.0"
+ }
+ , "bin": { "mocha": "./bin/mocha" }
+ , "author": "TJ Holowaychuk <[email protected]>"
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
\ No newline at end of file