Skip to content

Commit

Permalink
test.only
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Jan 19, 2013
1 parent 1786bcc commit 8a99091
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 5 deletions.
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function createHarness (conf_) {
var count = 0;

var began = false;
var only = false;
var out = new Render();

var test = function (name, conf, cb) {
Expand All @@ -37,7 +38,9 @@ function createHarness (conf_) {
onexit(function (code) {
t._exit();
out.close();
if (!code && !t._ok) process.exit(1);
if (!code && !t._ok && (!only || name === only)) {
process.exit(1);
}
});
}

Expand All @@ -52,6 +55,11 @@ function createHarness (conf_) {
t.run();
};

if (only && name !== only) {
count--;
return;
}

if (running || pending.length) {
pending.push(run);
}
Expand Down Expand Up @@ -94,6 +102,16 @@ function createHarness (conf_) {
}
};

test.only = function (name) {
if (only) {
throw new Error("there can only be one only test");
}

only = name;

return test.apply(null, arguments);
};

test.stream = out;
return test;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function encodeResult (res, count) {
output += inner + 'expected: ' + ex + '\n';
output += inner + 'actual: ' + ac + '\n';
}
if (res.at) {
output += inner + 'at: ' + res.at + '\n';
}
if (res.operator === 'error' && res.actual && res.actual.stack) {
var lines = String(res.actual.stack).split('\n');
output += inner + 'stack:\n';
Expand Down
26 changes: 25 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter;
var deepEqual = require('deep-equal');
var defined = require('defined');
var path = require('path');

module.exports = Test;

Expand Down Expand Up @@ -102,14 +103,37 @@ Test.prototype._assert = function assert (ok, opts) {
name : defined(extra.message, opts.message, '(unnamed assert)'),
operator : defined(extra.operator, opts.operator),
actual : defined(extra.actual, opts.actual),
expected : defined(extra.expected, opts.expected)
expected : defined(extra.expected, opts.expected),
};
this._ok = Boolean(this._ok && ok);

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

var e = new Error('exception');
var err = (e.stack || '').split('\n');
var dir = path.dirname(__dirname) + '/';

for (var i = 0; i < err.length; i++) {
var m = /^\s*\bat\s+(.+)/.exec(err[i]);
if (!m) continue;

var s = m[1].split(/\s+/);
var filem = /(\/[^:\s]+:(\d+)(?::(\d+))?)/.exec(s[1]);
if (!filem) continue;

if (filem[1].slice(0, dir.length) === dir) continue;

res.functionName = s[0];
res.file = filem[1];
res.line = Number(filem[2]);
if (filem[3]) res.column = filem[3];

res.at = m[1];
break;
}

self.emit('result', res);

if (self._plan === self.assertCount) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "tape",
"version" : "0.1.5",
"version" : "0.2.0",
"description" : "tap-producing test harness for node and browsers",
"main" : "index.js",
"bin" : {},
Expand Down
10 changes: 8 additions & 2 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ var test = require('tape');

test('timing test', function (t) {
t.plan(2);

t.equal(typeof Date.now, 'function');
var start = Date.now();

setTimeout(function () {
t.equal(Date.now() - start, 100);
}, 100);
Expand Down Expand Up @@ -161,6 +161,12 @@ By default the TAP output goes to `process.stdout` or `console.log()` if the
environment doesn't have `process.stdout`. You can pipe the output to someplace
else if you `test.stream.pipe()` to a destination stream on the first tick.

## test.only(name, cb)

Like `test(name, cb)` except if you use `.only` this is the only test case
that will run for the entire process, all other test cases using tape will
be ignored

# install

With [npm](https://npmjs.org) do:
Expand Down
48 changes: 48 additions & 0 deletions test/only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var tap = require('tap');
var tape = require('../');

tap.test('tape only test', function (tt) {
var test = tape.createHarness({ exit: false });
var tc = tap.createConsumer();

var rows = []
tc.on('data', function (r) { rows.push(r) })
tc.on('end', function () {
var rs = rows.map(function (r) {
if (r && typeof r === 'object') {
return { id: r.id, ok: r.ok, name: r.name.trim() };
}
else {
return r;
}
})

tt.deepEqual(rs, [
'TAP version 13',
'run success',
{ id: 1, ok: true, name: 'assert name'},
'tests 1',
'pass 1',
'ok'
])

tt.end()
})

test.stream.pipe(tc)

test("never run fail", function (t) {
t.equal(true, false)
t.end()
})

test("never run success", function (t) {
t.equal(true, true)
t.end()
})

test.only("run success", function (t) {
t.ok(true, "assert name")
t.end()
})
})

0 comments on commit 8a99091

Please sign in to comment.