Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make t.plan() optional #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -29,10 +28,10 @@ function createHarness () {
var run = function () {
running = true;
out.push(t);
cb(t);
t.run();
};

if (running) {
if (running || pending.length) {
pending.push(run);
}
else run();
Expand Down Expand Up @@ -62,3 +61,5 @@ function createHarness () {
test.stream = out;
return test;
}

// vim: set softtabstop=4 shiftwidth=4:
47 changes: 35 additions & 12 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -38,7 +58,8 @@ Test.prototype.plan = function (n) {

Test.prototype.end = function () {
if (!this.ended) this.emit('end');
if (!this._planError && this.assertCount !== this._plan) {
if (this._plan !== undefined &&
!this._planError && this.assertCount !== this._plan) {
this._planError = true;
this.fail('plan != count', {
expected : this._plan,
Expand Down Expand Up @@ -258,3 +279,5 @@ Test.prototype.doesNotThrow = function (fn, expected, msg, extra) {
extra : extra
});
};

// vim: set softtabstop=4 shiftwidth=4:
19 changes: 19 additions & 0 deletions test/child_ordering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var test = require('../');

var childRan = false;

test('parent', function(t) {
t.test('child', function(t) {
childRan = true;
t.pass('child ran');
t.end();
});
t.end();
});

test('uncle', function(t) {
t.ok(childRan, 'Child should run before next top-level test');
t.end();
});

// vim: set softtabstop=4 shiftwidth=4:
15 changes: 15 additions & 0 deletions test/plan_optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var test = require('../');

test('plan should be optional', function (t) {
t.pass('no plan here');
t.end();
});

test('no plan async', function (t) {
setTimeout(function() {
t.pass('ok');
t.end();
});
});

// vim: set softtabstop=4 shiftwidth=4:
34 changes: 34 additions & 0 deletions test/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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) {
t.equal(ran, 3, 'ran the right number of tests');
t.end();
});

// vim: set softtabstop=4 shiftwidth=4: