Skip to content

Commit da59c8e

Browse files
committed
refactoring and code organization
Nudging slightly towards having a single 'main' method for a child test. Seriously considering making spawn test children, in-process subtest, and stdin subtests all inherit from some root class, just to get a consistent interface. Once the 'run the subtest' and 'collect the results' logic is fully separated, it'll be possible to have two run at once and collect them both up later, making parallel jobs possible. Having an enormous amount of tests makes this work even possible. Another helpful step may be to push for 100% coverage (or as close as is feasible.)
1 parent 642fada commit da59c8e

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

lib/test.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ function Test (options) {
101101

102102
this._timer = null
103103
this._timeout = 0
104-
if (options.timeout !== Infinity &&
105-
!isNaN(options.timeout) &&
106-
options.timeout > 0) {
107-
this.setTimeout(options.timeout)
108-
}
109104

110105
Readable.apply(this, options)
111106

@@ -194,9 +189,10 @@ Test.prototype._queueFail = function () {
194189
var extra = { at: this._calledAt }
195190

196191
switch (what) {
197-
case 'test':
198-
extra = q[2]
199-
msg = 'child test left in queue: ' + (q[1] || '(unnamed)')
192+
case '_runChild':
193+
extra = q[3]
194+
name = q[2] || '(unnamed)'
195+
msg = 'child test left in queue: ' + name
200196
break
201197

202198
case 'printResult':
@@ -467,34 +463,41 @@ Test.prototype.test = function test (name, extra, cb, deferred) {
467463
extra.at = stack.at(test)
468464
}
469465

470-
if (this._currentChild) {
471-
this._queue.push(['test', name, extra, cb, deferred])
472-
return deferred.promise
473-
}
466+
var child = this._createChild(name, extra, deferred)
474467

475-
var child = new Test(extra)
476-
if (!extra.buffered) {
477-
this.comment('Subtest: ' + name)
478-
}
468+
this._runChild(child, name, extra, cb, deferred)
469+
return deferred.promise
470+
}
479471

472+
Test.prototype._createChild = function (name, extra, deferred) {
473+
var self = this
474+
var child = new Test(extra)
480475
child._name = name
481476
child._parent = this
482477
if (!Object.prototype.hasOwnProperty.call(extra, 'bail')) {
483478
child._bail = this._bail
484479
}
480+
child._deferred = deferred
481+
return child
482+
}
485483

484+
Test.prototype._runChild = function (child, name, extra, cb, deferred) {
485+
if (this._currentChild) {
486+
this._queue.push(['_runChild', child, name, extra, cb, deferred])
487+
return
488+
}
489+
if (!child._buffered) {
490+
this.comment('Subtest: ' + name)
491+
}
486492
this._currentChild = child
487-
488-
var self = this
489-
if (!extra.buffered) {
490-
childStream(self, child)
493+
if (!child._buffered) {
494+
childStream(this, child)
491495
}
492-
self._level = child
493-
child._deferred = deferred
496+
this._level = child
497+
var self = this
494498
this._runBeforeEach(child, function () {
495-
self._runChild(child, name, extra, cb)
499+
self._childMain(child, name, extra, cb)
496500
})
497-
return deferred.promise
498501
}
499502

500503
Test.prototype._runBeforeEach = function (who, cb) {
@@ -562,10 +565,19 @@ function childExtra (extra, results) {
562565
delete extra.stack
563566
}
564567

565-
Test.prototype._runChild = function runChild (child, name, extra, cb) {
568+
Test.prototype._childMain = function childMain (child, name, extra, cb) {
566569
var self = this
567570
var results
568571
var ended = false
572+
573+
child._startTime = process.hrtime()
574+
if (extra.timeout !== Infinity &&
575+
!isNaN(extra.timeout) &&
576+
extra.timeout > 0) {
577+
578+
child.setTimeout(extra.timeout)
579+
}
580+
569581
child.on('complete', function (res) {
570582
results = pruneFailures(res)
571583
})

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"regen-fixtures": "node scripts/generate-test-test.js test/test/*.js",
4444
"test": "node bin/run.js test/*.* --coverage -t3600",
4545
"smoke": "node test/test.js \"*.js\" | node bin/run.js -",
46-
"posttest": "standard lib test"
46+
"posttest": "standard lib test",
47+
"t": "node bin/run.js test/*.* -sfails.txt"
4748
},
4849
"devDependencies": {
4950
"mkdirp": "^0.5.1",

test/timeout.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ tap.test('t.setTimeout()', function (t) {
5252
' not ok 2 - timeout!\n' +
5353
' ---\n' +
5454
' timeout: 1\n' +
55+
' at:\n' +
56+
' line: [\\d]+\n' +
57+
' column: [\\d]+\n' +
58+
' file: test.timeout\\.js\n' +
59+
'( type: global\n)?' +
60+
' source: |\n' +
61+
' tt = new Test\\([^\\)]+\\)\n' +
62+
' \\.\\.\\.\n' +
63+
' timeout: 1\n' +
5564
' \\.\\.\\.\n' +
5665
'\\s*\n' +
5766
' 1..2\n' +

0 commit comments

Comments
 (0)