☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added js API. Closes #265
TJ Holowaychuk9125b84f8e0e
diff --git a/Makefile b/Makefileindex 7f4c2546..60b224cf 100644--- a/Makefile+++ b/Makefile@@ -32,7 +32,10 @@ lib-cov:test: test-unit-test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep+test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi++test-jsapi:+ @node test/jsapitest-unit:@./bin/mocha \@@ -105,4 +108,4 @@ tm:mkdir -p $(TM_DEST)/$(TM_BUNDLE)cp -fr editors/$(TM_BUNDLE) $(TM_DEST)/$(TM_BUNDLE)-.PHONY: test-cov watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm clean+.PHONY: test-cov test-jsapi watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm cleandiff --git a/lib/mocha.js b/lib/mocha.jsindex 1d4e3b21..07c6c90f 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -5,12 +5,28 @@* MIT Licensed*/+/**+ * Module dependencies.+ */++var path = require('path');++/**+ * Expose `Mocha`.+ */++exports = module.exports = Mocha;+/*** Library version.*/exports.version = '0.14.1';+/**+ * Expose internals.+ */+exports.utils = require('./utils');exports.interfaces = require('./interfaces');exports.reporters = require('./reporters');@@ -20,3 +36,138 @@ exports.Runner = require('./runner');exports.Suite = require('./suite');exports.Hook = require('./hook');exports.Test = require('./test');++/**+ * Growl images.+ */++var images = {+ fail: __dirname + '/../images/error.png'+ , pass: __dirname + '/../images/ok.png'+};++/**+ * Setup mocha with `options`.+ *+ * Options:+ *+ * - `ui` name "bdd", "tdd", "exports" etc+ * - `reporter` reporter instance, defaults to `mocha.reporters.Dot`+ * - `globals` array of accepted globals+ * - `timeout` timeout in milliseconds+ * - `ignoreLeaks` ignore global leaks+ *+ * @param {Object} options+ * @api public+ */++function Mocha(options) {+ options = options || {};+ this.files = [];+ this.options = options;+ this.suite = new exports.Suite('', new exports.Context);+ this.ui(options.ui);+ this.reporter(options.reporter);+ if (options.timeout) this.suite.timeout(options.timeout);+}++/**+ * Add test `file`.+ *+ * @param {String} file+ * @api public+ */++Mocha.prototype.addFile = function(file){+ this.files.push(file);+ return this;+};++/**+ * Set reporter to `name`, defaults to "dot".+ *+ * @param {String} name+ * @api public+ */++Mocha.prototype.reporter = function(name){+ name = name || 'dot';+ this._reporter = require('./reporters/' + name);+ if (!this._reporter) throw new Error('invalid reporter "' + name + '"');+ return this;+};++/**+ * Set test UI `name`, defaults to "bdd".+ *+ * @param {String} bdd+ * @api public+ */++Mocha.prototype.ui = function(name){+ name = name || 'bdd';+ this._ui = exports.interfaces[name];+ if (!this._ui) throw new Error('invalid interface "' + name + '"');+ this._ui = this._ui(this.suite);+ return this;+};++/**+ * Load registered files.+ *+ * @api private+ */++Mocha.prototype.loadFiles = function(){+ var suite = this.suite;+ this.files.forEach(function(file){+ file = path.resolve(file);+ suite.emit('pre-require', global, file);+ suite.emit('require', require(file), file);+ suite.emit('post-require', global, file);+ });+};++/**+ * Enable growl support.+ *+ * @api private+ */++Mocha.prototype.growl = function(runner, reporter) {+ var notify = require('growl');++ runner.on('end', function(){+ var stats = reporter.stats;+ if (stats.failures) {+ var msg = stats.failures + ' of ' + runner.total + ' tests failed';+ notify(msg, { title: 'Failed', image: images.fail });+ } else {+ notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {+ title: 'Passed'+ , image: images.pass+ });+ }+ });+};++/**+ * Run tests and invoke `fn()` when complete.+ *+ * @param {Function} fn+ * @return {Runner}+ * @api public+ */++Mocha.prototype.run = function(fn){+ this.loadFiles();+ var suite = this.suite;+ var options = this.options;+ var runner = new exports.Runner(suite);+ var reporter = new this._reporter(runner);+ runner.ignoreLeaks = options.ignoreLeaks;+ if (options.grep) runner.grep(options.grep);+ if (options.globals) runner.globals(options.globals);+ if (options.growl) this.growl(runner, reporter);+ return runner.run(fn);+};diff --git a/test/jsapi/index.js b/test/jsapi/index.jsnew file mode 100644index 00000000..f658938c--- /dev/null+++ b/test/jsapi/index.js@@ -0,0 +1,31 @@++var Mocha = require('../../')+ , path = require('path');++var mocha = new Mocha({+ ui: 'bdd',+ globals: ['okGlobalA', 'okGlobalB'],+ // ignoreLeaks: true,+ growl: true+});++// mocha.reporter('spec');+require('should');++mocha.addFile('test/suite.js');+mocha.addFile('test/runner.js');+mocha.addFile('test/runnable.js');+mocha.addFile('test/hook.sync.js');+mocha.addFile('test/hook.sync.nested.js');+mocha.addFile('test/hook.async.js');+mocha.addFile('test/acceptance/duration.js');+mocha.addFile('test/acceptance/fs.js');+mocha.addFile('test/acceptance/globals.js');+mocha.addFile('test/acceptance/pending.js');+mocha.addFile('test/acceptance/timeout.js');++mocha.run(function(){+ console.log('done');+}).on('pass', function(test){+ // console.log('... %s', test.title);+});\ No newline at end of file