[Pkg-javascript-commits] [node-tap] 73/186: Properly catch errors throw in 'end' events

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:45 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-tap.

commit 9aed6db896a69c982421eb3301db82e84adf872f
Author: isaacs <i at izs.me>
Date:   Thu Nov 9 14:19:19 2017 +0000

    Properly catch errors throw in 'end' events
    
    Regression introduced on 3c156af4ff7cb2583581c96fd7478546f52685ca
---
 lib/test.js                            | 12 ++++-
 tap-snapshots/unit-test.js-TAP.test.js | 94 ++++++++++++++++++++++++++++++++--
 unit/test.js                           | 16 +++++-
 3 files changed, 117 insertions(+), 5 deletions(-)

diff --git a/lib/test.js b/lib/test.js
index e0b7a57..b66eebd 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -258,7 +258,17 @@ class Test extends Base {
         this.ondone = cb
     }
 
-    const ret = this.cb(this)
+    // This bit of overly clever line-noise wraps the call to user-code
+    // in a try-catch. We can't rely on the domain for this yet, because
+    // the 'end' event can trigger a throw after the domain is unhooked,
+    // but before this is no longer the official "active test"
+    const ret = (() => {
+      try {
+        return this.cb(this)
+      } catch (er) {
+        this.threw(er)
+      }
+    })()
 
     if (ret && ret.then) {
       this.promise = ret
diff --git a/tap-snapshots/unit-test.js-TAP.test.js b/tap-snapshots/unit-test.js-TAP.test.js
index cde31fe..c2d1244 100644
--- a/tap-snapshots/unit-test.js-TAP.test.js
+++ b/tap-snapshots/unit-test.js-TAP.test.js
@@ -1074,7 +1074,7 @@ TAP version 13
         function: Test.tt.test.tt
       test: child test
       source: |
-        tt.threw(new Error('ok'))).then(tt.end)
+        tt.threw(new Error('ok'))).then(tt.end),
       ...
     
     1..1
@@ -1100,7 +1100,7 @@ TAP version 13
         function: Test.tt.test.tt
       test: child test
       source: |
-        tt.threw(new Error('ok'))).then(tt.end)
+        tt.threw(new Error('ok'))).then(tt.end),
       ...
     
     1..1
@@ -1126,7 +1126,7 @@ TAP version 13
         function: Test.tt.test.tt
       test: child test
       source: |
-        tt.threw(new Error('ok'))).then(tt.end)
+        tt.threw(new Error('ok'))).then(tt.end),
       ...
     
     Bail out! # ok
@@ -1141,6 +1141,94 @@ ok 1 - child test # SKIP filter: only
 
 `
 
+exports[`unit/test.js TAP short output checks child end event thrower no options > child end event thrower 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    1..1
+    ok 1 - should be equal
+# end() event
+ok 1 - (unnamed test) # {time}
+
+not ok 2 - beep
+  ---
+  stack: |
+    {STACK}
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Test.<anonymous>
+  test: (unnamed test)
+  source: |
+    throw new Error('beep')
+  ...
+
+1..2
+# failed 1 of 2 tests
+
+`
+
+exports[`unit/test.js TAP short output checks child end event thrower buffered > child end event thrower 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    1..1
+    ok 1 - should be equal
+# end() event
+ok 1 - (unnamed test) # {time}
+
+not ok 2 - beep
+  ---
+  stack: |
+    {STACK}
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Test.<anonymous>
+  test: (unnamed test)
+  source: |
+    throw new Error('beep')
+  ...
+
+1..2
+# failed 1 of 2 tests
+
+`
+
+exports[`unit/test.js TAP short output checks child end event thrower bailout > child end event thrower 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    1..1
+    ok 1 - should be equal
+# end() event
+ok 1 - (unnamed test) # {time}
+
+not ok 2 - beep
+  ---
+  stack: |
+    {STACK}
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Test.<anonymous>
+  test: (unnamed test)
+  source: |
+    throw new Error('beep')
+  ...
+
+Bail out! # beep
+BAILOUT: "# beep"
+`
+
+exports[`unit/test.js TAP short output checks child end event thrower runOnly > child end event thrower 1`] = `
+TAP version 13
+ok 1 - (unnamed test) # SKIP filter: only
+1..1
+# skip: 1
+
+`
+
 exports[`unit/test.js TAP assertions and weird stuff error > error 1`] = `
 TAP version 13
 ok 1 - this is not an error
diff --git a/unit/test.js b/unit/test.js
index 0e86c1d..e83b1d3 100644
--- a/unit/test.js
+++ b/unit/test.js
@@ -184,7 +184,21 @@ t.test('short output checks', t => {
     // using a Domain to catch beyond async stack drops
     'gentle thrower': tt => tt.threw(new Error('ok')),
     'child thrower': tt => tt.test('child test', tt =>
-      tt.threw(new Error('ok'))).then(tt.end)
+      tt.threw(new Error('ok'))).then(tt.end),
+
+    'child end event thrower': tt => {
+      tt.test(tt => {
+        tt.plan(1)
+
+        tt.on('end', function () {
+          tt.comment('end() event')
+          throw new Error('beep')
+        })
+
+        tt.equal(3, 3)
+      })
+      tt.end()
+    }
   }
 
   const keys = Object.keys(cases)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tap.git



More information about the Pkg-javascript-commits mailing list