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

Add timeoutAfter method #132

Merged
merged 5 commits into from
Jan 19, 2015
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
16 changes: 16 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ function Test (name_, opts_, cb_) {
this._progeny = [];
this._ok = true;

if (args.opts.timeout !== undefined) {
this.timeoutAfter(args.opts.timeout);
}

for (var prop in this) {
this[prop] = (function bind(self, val) {
if (typeof val === 'function') {
Expand Down Expand Up @@ -105,6 +109,18 @@ Test.prototype.plan = function (n) {
this.emit('plan', n);
};

Test.prototype.timeoutAfter = function(ms) {
if (!ms) throw new Error('timeoutAfter requires a timespan');
var self = this;
var timeout = setTimeout(function() {
self.fail('test timed out after ' + ms + 'ms');
self.end();
}, ms);
this.once('end', function() {
clearTimeout(timeout);
});
}

Test.prototype.end = function (err) {
var self = this;
if (arguments.length >= 1) {
Expand Down
16 changes: 13 additions & 3 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ in [node-tap](https://github.com/isaacs/node-tap).
var test = require('tape')
```

## test(name, cb)
## test([name], [opts], cb)

Create a new test with an optional `name` string. `cb(t)` fires with the new
test object `t` once all preceeding tests have finished. Tests execute serially.
Create a new test with an optional `name` string and optional `opts` object.
`cb(t)` fires with the new test object `t` once all preceeding tests have
finished. Tests execute serially.

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.

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 All @@ -105,6 +111,10 @@ Generate a failing assertion with a message `msg`.

Generate a passing assertion with a message `msg`.

## t.timeoutAfter(ms)

Automatically timeout the test after X ms.

## t.skip(msg)

Generate an assertion that will be skipped over.
Expand Down
35 changes: 35 additions & 0 deletions test/timeoutAfter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var tape = require('../');
var tap = require('tap');

tap.test('timeoutAfter test', function (tt) {
tt.plan(1);

var test = tape.createHarness();
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.same(rs, [
'TAP version 13',
'timeoutAfter',
{ id: 1, ok: false, name: 'test timed out after 1ms' },
'tests 1',
'pass 0',
'fail 1'
]);
});

test.createStream().pipe(tc);

test('timeoutAfter', function (t) {
t.plan(1);
t.timeoutAfter(1);
});
});