Skip to content

Commit

Permalink
Catch errors thrown in teardown handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 19, 2017
1 parent 7b96e86 commit 7bd7f22
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/tap.js
Expand Up @@ -97,7 +97,11 @@ TAP.prototype.onbeforeend = function () {
}

TAP.prototype.ondone = function () {
this.emit('teardown')
try {
this.emit('teardown')
} catch (er) {
this.threw(er)
}
}

// Root test runner doesn't have the 'teardown' event, because it
Expand Down
14 changes: 12 additions & 2 deletions lib/test.js
Expand Up @@ -294,7 +294,11 @@ Test.prototype.processSubtest = function (p) {
this.occupied = null
if (!p.passing() || !p.silent)
this.printResult(p.passing(), p.name, p.options, true)
p.emit('teardown')
try {
p.emit('teardown')
} catch (er) {
this.threw(er)
}
} else {
this.occupied = p
this.debug(' > subtest buffered, unfinished', p)
Expand Down Expand Up @@ -362,7 +366,13 @@ Test.prototype.onindentedend = function (p, er) {
this.debug('OIE(%s) b>shift into queue', this.name, this.queue)
p.options.stack = ''
this.printResult(p.passing(), p.name, p.options, true)
p.emit('teardown')

try {
p.emit('teardown')
} catch (er) {
this.threw(er)
}

this.debug('OIE(%s) shifted into queue', this.name, this.queue)
if (er)
this.threw(er)
Expand Down
5 changes: 5 additions & 0 deletions scripts/generate-test-test.js
Expand Up @@ -52,6 +52,11 @@ function generate (file, bail, buffer) {
var timere = new RegExp(timep, 'g')
output = output.replace(timere, '___/' + timep + '/~~~')

// strip out un-pretty-ified error stack lines
output = output.replace(
/^ at .*?:[0-9]+:[0-9]+\)?$/mg,
'___/^ at .*?:[0-9]+:[0-9]+\\)?$/~~~')

output = output.split(file).join('___/.*/~~~' + path.basename(file))
output = output.split(f).join('___/.*/~~~' + path.basename(f))

Expand Down
29 changes: 29 additions & 0 deletions test/test/teardown-throw-autocomplete--bail--buffer.tap
@@ -0,0 +1,29 @@
TAP version 13
not ok 1 - child ___/# time=[0-9.]+(ms)?/~~~ {
not ok 1 - test unfinished
---
{"at":{"column":3,"file":"test/test/teardown-throw-autocomplete.js","line":10},"source":"t.test('child', function (t) {\n","test":"child"}
...

Bail out! # test unfinished
}
Bail out! # test unfinished
Error: TAP teardown
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
{ name: 'TAP', autoend: true, test: 'TAP' }

28 changes: 28 additions & 0 deletions test/test/teardown-throw-autocomplete--bail.tap
@@ -0,0 +1,28 @@
TAP version 13
# Subtest: child
not ok 1 - test unfinished
---
{"at":{"column":3,"file":"test/test/teardown-throw-autocomplete.js","line":10},"source":"t.test('child', function (t) {\n","test":"child"}
...

Bail out! # test unfinished
Bail out! # test unfinished
Error: TAP teardown
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
{ name: 'TAP', autoend: true, test: 'TAP' }

43 changes: 43 additions & 0 deletions test/test/teardown-throw-autocomplete--buffer.tap
@@ -0,0 +1,43 @@
TAP version 13
not ok 1 - child ___/# time=[0-9.]+(ms)?/~~~ {
not ok 1 - test unfinished
---
{"at":{"column":3,"file":"test/test/teardown-throw-autocomplete.js","line":10},"source":"t.test('child', function (t) {\n","test":"child"}
...

not ok 2 - afterEach
---
{"at":{"column":9,"file":"test/test/teardown-throw-autocomplete.js","line":7},"source":"throw new Error('afterEach')\n","test":"child"}
...

1..2
# failed 2 of 2 tests
}

not ok 2 - child teardown
---
{"at":{"column":11,"file":"test/test/teardown-throw-autocomplete.js","line":12},"autoend":true,"source":"throw new Error('child teardown')\n","test":"TAP"}
...

1..2
# failed 2 of 2 tests
___/# time=[0-9.]+(ms)?/~~~
Error: TAP teardown
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
{ name: 'TAP', autoend: true, test: 'TAP' }

14 changes: 14 additions & 0 deletions test/test/teardown-throw-autocomplete.js
@@ -0,0 +1,14 @@
console.error = console.log
var t = require('../..')
t.tearDown(function () {
throw new Error('TAP teardown')
})
t.afterEach(function (cb) {
throw new Error('afterEach')
cb()
})
t.test('child', function (t) {
t.tearDown(function () {
throw new Error('child teardown')
})
})
43 changes: 43 additions & 0 deletions test/test/teardown-throw-autocomplete.tap
@@ -0,0 +1,43 @@
TAP version 13
# Subtest: child
not ok 1 - test unfinished
---
{"at":{"column":3,"file":"test/test/teardown-throw-autocomplete.js","line":10},"source":"t.test('child', function (t) {\n","test":"child"}
...

not ok 2 - afterEach
---
{"at":{"column":9,"file":"test/test/teardown-throw-autocomplete.js","line":7},"source":"throw new Error('afterEach')\n","test":"child"}
...

1..2
# failed 2 of 2 tests
not ok 1 - child ___/# time=[0-9.]+(ms)?/~~~

not ok 2 - child teardown
---
{"at":{"column":11,"file":"test/test/teardown-throw-autocomplete.js","line":12},"autoend":true,"source":"throw new Error('child teardown')\n","test":"TAP"}
...

1..2
# failed 2 of 2 tests
___/# time=[0-9.]+(ms)?/~~~
Error: TAP teardown
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
___/^ at .*?:[0-9]+:[0-9]+\)?$/~~~
{ name: 'TAP', autoend: true, test: 'TAP' }

0 comments on commit 7bd7f22

Please sign in to comment.