Skip to content

Commit 2a60e79

Browse files
committed
Merge pull request #132 from FredrikNoren/master
Add timeoutAfter method
2 parents 811cf0a + ea6dc65 commit 2a60e79

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

lib/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ function Test (name_, opts_, cb_) {
5252
this._progeny = [];
5353
this._ok = true;
5454

55+
if (args.opts.timeout !== undefined) {
56+
this.timeoutAfter(args.opts.timeout);
57+
}
58+
5559
for (var prop in this) {
5660
this[prop] = (function bind(self, val) {
5761
if (typeof val === 'function') {
@@ -105,6 +109,18 @@ Test.prototype.plan = function (n) {
105109
this.emit('plan', n);
106110
};
107111

112+
Test.prototype.timeoutAfter = function(ms) {
113+
if (!ms) throw new Error('timeoutAfter requires a timespan');
114+
var self = this;
115+
var timeout = setTimeout(function() {
116+
self.fail('test timed out after ' + ms + 'ms');
117+
self.end();
118+
}, ms);
119+
this.once('end', function() {
120+
clearTimeout(timeout);
121+
});
122+
}
123+
108124
Test.prototype.end = function (err) {
109125
var self = this;
110126
if (arguments.length >= 1) {

readme.markdown

+13-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ in [node-tap](https://github.com/isaacs/node-tap).
7575
var test = require('tape')
7676
```
7777

78-
## test(name, cb)
78+
## test([name], [opts], cb)
7979

80-
Create a new test with an optional `name` string. `cb(t)` fires with the new
81-
test object `t` once all preceeding tests have finished. Tests execute serially.
80+
Create a new test with an optional `name` string and optional `opts` object.
81+
`cb(t)` fires with the new test object `t` once all preceeding tests have
82+
finished. Tests execute serially.
83+
84+
Available `opts` options are:
85+
- opts.skip = true/false. See test.skip.
86+
- opts.timeout = 500. Set a timeout for the test, after which it will fail.
87+
See test.timeoutAfter.
8288

8389
If you forget to `t.plan()` out how many assertions you are going to run and you
8490
don't call `t.end()` explicitly, your test will hang.
@@ -105,6 +111,10 @@ Generate a failing assertion with a message `msg`.
105111

106112
Generate a passing assertion with a message `msg`.
107113

114+
## t.timeoutAfter(ms)
115+
116+
Automatically timeout the test after X ms.
117+
108118
## t.skip(msg)
109119

110120
Generate an assertion that will be skipped over.

test/timeoutAfter.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var tape = require('../');
2+
var tap = require('tap');
3+
4+
tap.test('timeoutAfter test', function (tt) {
5+
tt.plan(1);
6+
7+
var test = tape.createHarness();
8+
var tc = tap.createConsumer();
9+
10+
var rows = [];
11+
tc.on('data', function (r) { rows.push(r) });
12+
tc.on('end', function () {
13+
var rs = rows.map(function (r) {
14+
if (r && typeof r === 'object') {
15+
return { id : r.id, ok : r.ok, name : r.name.trim() };
16+
}
17+
else return r;
18+
});
19+
tt.same(rs, [
20+
'TAP version 13',
21+
'timeoutAfter',
22+
{ id: 1, ok: false, name: 'test timed out after 1ms' },
23+
'tests 1',
24+
'pass 0',
25+
'fail 1'
26+
]);
27+
});
28+
29+
test.createStream().pipe(tc);
30+
31+
test('timeoutAfter', function (t) {
32+
t.plan(1);
33+
t.timeoutAfter(1);
34+
});
35+
});

0 commit comments

Comments
 (0)