Skip to content

Commit

Permalink
[New] Implements TAP TODO directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ronkorving authored and ljharb committed Feb 14, 2016
1 parent 5d0d706 commit 5cdaf54
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
10 changes: 6 additions & 4 deletions lib/results.js
Expand Up @@ -21,6 +21,7 @@ function Results () {
this.count = 0;
this.fail = 0;
this.pass = 0;
this.todo = 0;
this._stream = through();
this.tests = [];
this._only = null;
Expand Down Expand Up @@ -107,7 +108,7 @@ Results.prototype._watch = function (t) {
write(encodeResult(res, self.count + 1));
self.count ++;

if (res.ok) self.pass ++
if (res.ok || res.todo) self.pass ++
else {
self.fail ++;
self.emit('fail');
Expand All @@ -125,9 +126,10 @@ Results.prototype.close = function () {

write('\n1..' + self.count + '\n');
write('# tests ' + self.count + '\n');
write('# pass ' + self.pass + '\n');
if (self.fail) write('# fail ' + self.fail + '\n')
else write('\n# ok\n')
write('# pass ' + (self.pass + self.todo) + '\n');
if (self.todo) write('# todo ' + self.todo + '\n');
if (self.fail) write('# fail ' + self.fail + '\n');
else write('\n# ok\n');

self._stream.queue(null);
};
Expand Down
16 changes: 9 additions & 7 deletions lib/test.js
Expand Up @@ -66,6 +66,7 @@ function Test (name_, opts_, cb_) {
this.assertCount = 0;
this.pendingCount = 0;
this._skip = args.opts.skip || false;
this._todo = args.opts.todo || false;
this._timeout = args.opts.timeout;
this._plan = undefined;
this._cb = args.cb;
Expand Down Expand Up @@ -203,12 +204,13 @@ Test.prototype._assert = function assert (ok, opts) {
var extra = opts.extra || {};

var res = {
id : self.assertCount ++,
ok : Boolean(ok),
skip : defined(extra.skip, opts.skip),
name : defined(extra.message, opts.message, '(unnamed assert)'),
operator : defined(extra.operator, opts.operator),
objectPrintDepth : self._objectPrintDepth
id: self.assertCount++,
ok: Boolean(ok),
skip: defined(extra.skip, opts.skip),
todo: defined(extra.todo, opts.todo, self._todo),
name: defined(extra.message, opts.message, '(unnamed assert)'),
operator: defined(extra.operator, opts.operator),
objectPrintDepth: self._objectPrintDepth
};
if (has(opts, 'actual') || has(extra, 'actual')) {
res.actual = defined(extra.actual, opts.actual);
Expand All @@ -218,7 +220,7 @@ Test.prototype._assert = function assert (ok, opts) {
}
this._ok = Boolean(this._ok && ok);

if (!ok) {
if (!ok && !res.todo) {
res.error = defined(extra.error, opts.error, new Error(res.name));
}

Expand Down
1 change: 1 addition & 0 deletions readme.markdown
Expand Up @@ -152,6 +152,7 @@ Available `opts` options are:
- opts.skip = true/false. See test.skip.
- opts.timeout = 500. Set a timeout for the test, after which it will fail. See test.timeoutAfter.
- opts.objectPrintDepth = 5. Configure max depth of expected / actual object printing. Environmental variable `NODE_TAPE_OBJECT_PRINT_DEPTH` can set the desired default depth for all tests; locally-set values will take precedence.
- opts.todo = true/false. Test will be allowed to fail.

If you forget to `t.plan()` out how many assertions you are going to run and you
don't call `t.end()` explicitly, your test will hang.
Expand Down
42 changes: 42 additions & 0 deletions test/todo.js
@@ -0,0 +1,42 @@
var tap = require('tap');
var tape = require('../');
var concat = require('concat-stream');

var common = require('./common');
var stripFullStack = common.stripFullStack;

tap.test('tape todo test', function (assert) {
var test = tape.createHarness({ exit: false });
assert.plan(1);

test.createStream().pipe(concat(function (body) {
assert.equal(
stripFullStack(body.toString('utf8')),
'TAP version 13\n'
+ '# success\n'
+ 'ok 1 this test runs\n'
+ '# failure\n'
+ 'not ok 2 should never happen # TODO\n'
+ ' ---\n'
+ ' operator: fail\n'
+ ' at: Test.<anonymous> ($TEST/todo.js:$LINE:$COL)\n'
+ ' ...\n'
+ '\n'
+ '1..2\n'
+ '# tests 2\n'
+ '# pass 2\n'
+ '\n'
+ '# ok\n'
)
}));

test('success', function (t) {
t.equal(true, true, 'this test runs');
t.end();
});

test('failure', { todo: true }, function (t) {
t.fail('should never happen');
t.end();
});
});

0 comments on commit 5cdaf54

Please sign in to comment.