Skip to content

Commit

Permalink
[New] allow TODO tests to be "ok" with env var TODO_IS_OK
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 26, 2024
1 parent d39cb8d commit 6cd06f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (function () {
}());

function createHarness(conf_) {
var results = new Results();
var results = new Results({ todoIsOK: !!(process.env.TODO_IS_OK === '1') });
if (!conf_ || conf_.autoclose !== false) {
results.once('done', function () { results.close(); });
}
Expand Down
12 changes: 7 additions & 5 deletions lib/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function invalidYaml(str) {
return $exec(yamlIndicators, str) !== null;
}

function encodeResult(res, count) {
function encodeResult(res, count, todoIsOK) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += (res.ok || (todoIsOK && res.todo) ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';

if (res.skip) {
Expand Down Expand Up @@ -91,8 +91,9 @@ function encodeResult(res, count) {
return output;
}

function Results() {
if (!(this instanceof Results)) { return new Results(); }
function Results(options) {
if (!(this instanceof Results)) { return new Results(options); }
var opts = (arguments.length > 0 ? arguments[0] : options) || {};
this.count = 0;
this.fail = 0;
this.pass = 0;
Expand All @@ -101,6 +102,7 @@ function Results() {
this.tests = [];
this._only = null;
this._isRunning = false;
this.todoIsOK = !!opts.todoIsOK;
}

inherits(Results, EventEmitter);
Expand Down Expand Up @@ -197,7 +199,7 @@ Results.prototype._watch = function (t) {
write('# ' + res + '\n');
return;
}
write(encodeResult(res, self.count + 1));
write(encodeResult(res, self.count + 1, self.todoIsOK));
self.count++;

if (res.ok || res.todo) {
Expand Down
38 changes: 38 additions & 0 deletions test/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var tap = require('tap');
var tape = require('../');
var concat = require('concat-stream');
var mockProperty = require('mock-property');

var common = require('./common');
var stripFullStack = common.stripFullStack;
Expand Down Expand Up @@ -42,3 +43,40 @@ tap.test('tape todo test', function (assert) {
t.end();
});
});

tap.test('tape todo test with TODO_IS_OK', function (assert) {
assert.teardown(mockProperty(process.env, 'TODO_IS_OK', { value: '1' }));
var test = tape.createHarness({ exit: false });
assert.plan(1);

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

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 6cd06f5

Please sign in to comment.