Comparing version 0.3.3 to 1.0.0
@@ -21,6 +21,6 @@ var falafel = require('falafel'); | ||
q.plan(2); | ||
q.ok(true); | ||
q.ok(true, 'inside ok'); | ||
setTimeout(function () { | ||
q.ok(true); | ||
q.ok(true, 'inside delayed'); | ||
}, 3000); | ||
@@ -27,0 +27,0 @@ }); |
195
index.js
var createDefaultStream = require('./lib/default_stream'); | ||
var Render = require('./lib/render'); | ||
var Test = require('./lib/test'); | ||
var createResultStream = require('./lib/results'); | ||
@@ -11,9 +11,2 @@ var canEmitExit = typeof process !== 'undefined' && process | ||
; | ||
var onexit = (function () { | ||
var stack = []; | ||
if (canEmitExit) process.on('exit', function (code) { | ||
for (var i = 0; i < stack.length; i++) stack[i](code); | ||
}); | ||
return function (cb) { stack.push(cb) }; | ||
})(); | ||
@@ -25,5 +18,37 @@ var nextTick = typeof setImmediate !== 'undefined' | ||
exports = module.exports = createHarness(); | ||
exports = module.exports = (function () { | ||
var harness; | ||
return function () { | ||
if (!harness) harness = createExitHarness(); | ||
return harness.apply(this, arguments); | ||
}; | ||
})(); | ||
function createExitHarness (conf) { | ||
if (!conf) conf = {}; | ||
var harness = createHarness(); | ||
var stream = harness.createStream(); | ||
stream.pipe(createDefaultStream()); | ||
var ended = false; | ||
stream.on('end', function () { ended = true }); | ||
if (conf.exit === false) return harness; | ||
if (!canEmitExit || !canExit) return harness; | ||
process.on('exit', function (code) { | ||
if (!ended) { | ||
for (var i = 0; i < harness._tests.length; i++) { | ||
var t = harness._tests[i]; | ||
t._exit(); | ||
} | ||
} | ||
process.exit(code || harness._exitCode); | ||
}); | ||
return harness; | ||
} | ||
exports.createHarness = createHarness; | ||
exports.Test = Test; | ||
exports.test = exports; // tap compat | ||
@@ -33,135 +58,51 @@ var exitInterval; | ||
function createHarness (conf_) { | ||
var pending = []; | ||
var running = false; | ||
var count = 0; | ||
var results; | ||
var began = false; | ||
var only = false; | ||
var closed = false; | ||
var out = new Render(); | ||
if (!conf_) conf_ = {}; | ||
var tests = []; | ||
if (conf_.exit === false && exitInterval) clearInterval(exitInterval); | ||
exitInterval = !exitInterval && conf_.exit !== false && canEmitExit | ||
&& typeof process._getActiveHandles === 'function' | ||
&& setInterval(function () { | ||
if (process._getActiveHandles().length === 1) { | ||
tests.forEach(function (t) { t._exit() }); | ||
var test = function (name, conf, cb) { | ||
if (!results) { | ||
results = createResultStream(); | ||
results.pause(); | ||
} | ||
}, 200); | ||
var exitCode = 0; | ||
var exit = function (c) { exitCode = c }; | ||
out.on('end', function () { | ||
nextTick(function () { | ||
clearInterval(exitInterval); | ||
if (canExit && conf_.exit !== false) process.exit(exitCode); | ||
}); | ||
}); | ||
var test = function (name, conf, cb) { | ||
count++; | ||
var t = new Test(name, conf, cb); | ||
tests.push(t); | ||
if (!conf || typeof conf !== 'object') conf = conf_; | ||
test._tests.push(t); | ||
if (conf.exit !== false) { | ||
onexit(function (code) { | ||
t._exit(); | ||
if (!closed) { | ||
closed = true | ||
out.close(); | ||
} | ||
if (!code && !t._ok && (!only || name === only)) { | ||
exit(1); | ||
} | ||
(function inspectCode (st) { | ||
st.on('test', function sub (st_) { | ||
inspectCode(st_); | ||
}); | ||
} | ||
st.on('result', function (r) { | ||
if (!r.ok) test._exitCode = 1 | ||
}); | ||
})(t); | ||
results.push(t); | ||
return t; | ||
}; | ||
test._tests = []; | ||
test.createStream = function () { | ||
if (!results) results = createResultStream(); | ||
var _pause = results.pause; | ||
var paused = false; | ||
results.pause = function () { paused = true }; | ||
nextTick(function () { | ||
if (!out.piped) out.pipe(createDefaultStream()); | ||
if (!began) out.begin(); | ||
began = true; | ||
var run = function () { | ||
running = true; | ||
out.push(t); | ||
t.run(); | ||
}; | ||
if (only && name !== only) { | ||
count--; | ||
return; | ||
} | ||
if (running || pending.length) { | ||
pending.push(run); | ||
} | ||
else run(); | ||
if (!paused) results.resume(); | ||
}); | ||
t.on('test', function sub (st) { | ||
count++; | ||
st.on('test', sub); | ||
st.on('end', onend); | ||
}); | ||
t.on('result', function (r) { if (!r.ok) exitCode = 1 }); | ||
t.on('end', onend); | ||
return t; | ||
function onend () { | ||
count--; | ||
if (this._progeny.length) { | ||
var unshifts = map(this._progeny, function (st) { | ||
return function () { | ||
running = true; | ||
out.push(st); | ||
st.run(); | ||
}; | ||
}); | ||
pending.unshift.apply(pending, unshifts); | ||
} | ||
nextTick(function () { | ||
running = false; | ||
if (pending.length) return pending.shift()(); | ||
if (count === 0 && !closed) { | ||
closed = true | ||
out.close(); | ||
} | ||
if (conf.exit !== false && canExit && !t._ok) { | ||
exit(1); | ||
} | ||
}); | ||
} | ||
return results; | ||
}; | ||
var only = false; | ||
test.only = function (name) { | ||
if (only) { | ||
throw new Error("there can only be one only test"); | ||
} | ||
only = name; | ||
if (only) throw new Error('there can only be one only test'); | ||
results.only(name); | ||
only = true; | ||
return test.apply(null, arguments); | ||
}; | ||
test._exitCode = 0; | ||
test.stream = out; | ||
return test; | ||
} | ||
function map (xs, f) { | ||
if (xs.map) return xs.map(f); | ||
var res = []; | ||
for (var i = 0; i < xs.length; i++) { | ||
res.push(f(xs[i])); | ||
} | ||
return res; | ||
} | ||
// vim: set softtabstop=4 shiftwidth=4: |
@@ -1,5 +0,7 @@ | ||
var EventEmitter = require('events').EventEmitter; | ||
var Stream = require('stream'); | ||
var deepEqual = require('deep-equal'); | ||
var defined = require('defined'); | ||
var path = require('path'); | ||
var inherits = require('util').inherits; | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -13,5 +15,6 @@ module.exports = Test; | ||
Test.prototype = new EventEmitter; | ||
inherits(Test, EventEmitter); | ||
function Test (name_, opts_, cb_) { | ||
var self = this; | ||
var name = '(anonymous)'; | ||
@@ -34,4 +37,3 @@ var opts = {}; | ||
EventEmitter.call(this); | ||
this.readable = true; | ||
this.name = name || '(anonymous)'; | ||
@@ -50,2 +52,3 @@ this.assertCount = 0; | ||
} | ||
this.emit('prerun'); | ||
try { | ||
@@ -57,3 +60,5 @@ this._cb(this); | ||
this.end(); | ||
return; | ||
} | ||
this.emit('run'); | ||
}; | ||
@@ -77,2 +82,9 @@ | ||
Test.prototype.end = function () { | ||
var self = this; | ||
if (this._progeny.length) { | ||
var t = this._progeny.shift(); | ||
t.on('end', function () { self.end() }); | ||
return; | ||
} | ||
if (!this.ended) this.emit('end'); | ||
@@ -96,9 +108,11 @@ if (this._plan !== undefined && | ||
expected : this._plan, | ||
actual : this.assertCount | ||
actual : this.assertCount, | ||
exiting : true | ||
}); | ||
} | ||
else if (!this.ended) { | ||
this.fail('test exited without ending'); | ||
this.fail('test exited without ending', { | ||
exiting: true | ||
}); | ||
} | ||
}; | ||
@@ -150,3 +164,6 @@ | ||
if (self._plan === self.assertCount) { | ||
if (self._plan === self.assertCount && extra.exiting) { | ||
if (!self.ended) self.end(); | ||
} | ||
else if (self._plan === self.assertCount) { | ||
nextTick(function () { | ||
@@ -153,0 +170,0 @@ if (!self.ended) self.end(); |
{ | ||
"name" : "tape", | ||
"version" : "0.3.3", | ||
"version" : "1.0.0", | ||
"description" : "tap-producing test harness for node and browsers", | ||
@@ -5,0 +5,0 @@ "main" : "index.js", |
@@ -34,3 +34,3 @@ var falafel = require('falafel'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -37,0 +37,0 @@ test('array', function (t) { |
@@ -35,3 +35,3 @@ var tape = require('../'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -38,0 +38,0 @@ test("circular", function (t) { |
@@ -34,3 +34,3 @@ var falafel = require('falafel'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -37,0 +37,0 @@ test('array', function (t) { |
@@ -39,3 +39,3 @@ var falafel = require('falafel'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -42,0 +42,0 @@ test('nested array test', function (t) { |
@@ -32,3 +32,3 @@ var tap = require('tap'); | ||
test.stream.pipe(tc) | ||
test.createStream().pipe(tc) | ||
@@ -35,0 +35,0 @@ test("never run fail", function (t) { |
@@ -31,3 +31,3 @@ var falafel = require('falafel'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -34,0 +34,0 @@ test('thrower', function (t) { |
@@ -35,3 +35,3 @@ var falafel = require('falafel'); | ||
test.stream.pipe(tc); | ||
test.createStream().pipe(tc); | ||
@@ -38,0 +38,0 @@ test('array', function (t) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
52553
1547