From 94a90b356323b3ec167e11995b3116544de6d263 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sun, 25 Nov 2012 12:38:56 -0800 Subject: [PATCH] Use conf object, support skipping entire tests Fix #2 --- index.js | 5 ++--- lib/test.js | 44 +++++++++++++++++++++++++++++++++----------- test/skip.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 test/skip.js diff --git a/index.js b/index.js index 12c4ff70..c3cc926b 100644 --- a/index.js +++ b/index.js @@ -18,8 +18,7 @@ function createHarness () { cb = conf; conf = {}; } - var t = new Test; - t.name = name; + var t = new Test(name, conf, cb); process.nextTick(function () { if (!out.piped) out.pipe(createDefaultStream()); @@ -29,7 +28,7 @@ function createHarness () { var run = function () { running = true; out.push(t); - cb(t); + t.run(); }; if (running) { diff --git a/lib/test.js b/lib/test.js index c94c9a57..e5279b98 100644 --- a/lib/test.js +++ b/lib/test.js @@ -6,24 +6,44 @@ module.exports = Test; Test.prototype = new EventEmitter; -function Test (name, opts) { - if (!opts) opts = {}; +function Test (name_, opts_, cb_) { + var name = '(anonymous)'; + var opts = {}; + var cb; + + for (var i = 0; i < arguments.length; i++) { + switch (typeof arguments[i]) { + case 'string': + name = arguments[i]; + break; + case 'object': + opts = arguments[i] || opts; + break; + case 'function': + cb = arguments[i]; + } + } + EventEmitter.call(this); this.name = name || '(anonymous)'; this.assertCount = 0; + this._skip = opts.skip || false; + this._plan = undefined; + this._cb = cb; } -Test.prototype.test = function (name, cb) { - var self = this; - - if (typeof name === 'function') { - cb = name; - name = '(anonymous)'; +Test.prototype.run = function () { + if (this._skip) { + this.end(); + } else { + this._cb(this); } - - var t = new Test(name); - t.run = function () { cb(t) }; +}; + +Test.prototype.test = function (name, opts, cb) { + var self = this; + var t = new Test(name, opts, cb); self.emit('test', t); }; @@ -259,3 +279,5 @@ Test.prototype.doesNotThrow = function (fn, expected, msg, extra) { extra : extra }); }; + +// vim: set softtabstop=4 shiftwidth=4: diff --git a/test/skip.js b/test/skip.js new file mode 100644 index 00000000..81bff6af --- /dev/null +++ b/test/skip.js @@ -0,0 +1,37 @@ +var test = require('../'); +var ran = 0; + +test('do not skip this', { skip: false }, function(t) { + t.pass('this should run'); + ran ++; + t.end(); +}); + +test('skip this', { skip: true }, function(t) { + t.fail('this should not even run'); + t.end(); +}); + +test('skip subtest', function(t) { + ran ++; + t.test('do not skip this', { skip: false }, function(t) { + ran ++; + t.pass('this should run'); + t.end(); + }); + t.test('skip this', { skip: true }, function(t) { + t.fail('this should not even run'); + t.end(); + }); + t.end(); +}); + +test('right number of tests ran', function(t) { + // https://github.com/substack/tape/issues/4 + setTimeout(function() { + t.equal(ran, 3, 'ran the right number of tests'); + t.end(); + }); +}); + +// vim: set softtabstop=4 shiftwidth=4: