Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added Runnable

Tj Holowaychukb30eb8c1e080
diff --git a/lib/runnable.js b/lib/runnable.js
new file mode 100644
index 00000000..f350888a
--- /dev/null
+++ b/lib/runnable.js
@@ -0,0 +1,94 @@
+
+/**
+ * Expose `Runnable`.
+ */
+
+module.exports = Runnable;
+
+/**
+ * Initialize a new `Runnable` with the given `title` and callback `fn`.
+ *
+ * @param {String} title
+ * @param {Function} fn
+ * @api private
+ */
+
+function Runnable(title, fn) {
+ this.title = title;
+ this.fn = fn;
+ this.async = fn && fn.length;
+ this.sync = ! this.async;
+ this.timeout(2000);
+}
+
+/**
+ * Set timeout `ms`.
+ *
+ * @param {Number} ms
+ * @return {Runnable} for chaining
+ * @api private
+ */
+
+Runnable.prototype.timeout = function(ms){
+ this._timeout = ms;
+ return this;
+};
+
+/**
+ * Return the full title generated by recursively
+ * concatenating the parent's full title.
+ *
+ * @return {String}
+ * @api public
+ */
+
+Runnable.prototype.fullTitle = function(){
+ return this.parent.fullTitle() + ' ' + this.title;
+};
+
+/**
+ * Run the test and invoke `fn(err)`.
+ *
+ * @param {Function} fn
+ * @api private
+ */
+
+Runnable.prototype.run = function(fn){
+ var self = this
+ , ms = this._timeout
+ , start = new Date
+ , timer;
+
+ // timeout
+ if (this.async) {
+ timer = setTimeout(function(){
+ fn(new Error('timeout of ' + ms + 'ms exceeded'));
+ }, ms);
+ }
+
+ // async
+ if (this.async) {
+ try {
+ this.fn(function(err){
+ clearTimeout(timer);
+ self.duration = new Date - start;
+ fn(err);
+ self.finished = true;
+ });
+ } catch (err) {
+ clearTimeout(timer);
+ fn(err);
+ this.finished = true;
+ }
+ return;
+ }
+
+ // sync
+ try {
+ if (!this.pending) this.fn();
+ this.duration = new Date - start;
+ fn();
+ } catch (err) {
+ fn(err);
+ }
+};
diff --git a/test.js b/test.js
new file mode 100644
index 00000000..0b080d5a
--- /dev/null
+++ b/test.js
@@ -0,0 +1,13 @@
+
+describe('something', function(){
+ beforeEach(function(done){
+ done()
+ done()
+ done()
+ })
+
+ it('should work', function(done){
+ // done(new Error('test'));
+ done();
+ })
+})
\ No newline at end of file