From a23216b9877765bd8e933268140bf5e6d878d9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Yves=20G=C3=A9rardy?= Date: Fri, 1 Jun 2018 13:42:14 +0200 Subject: [PATCH] [ospec] record errors thrown in tests as failures --- ospec/ospec.js | 18 ++++++++++++------ ospec/tests/test-ospec.js | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index 99ca8b4a..f27021bf 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -179,12 +179,18 @@ else window.o = m() startTimer() } } else { - var p = fn() - if (p && p.then) { - startTimer() - p.then(function() { done() }, done) - } else { - nextTickish(next) + try{ + var p = fn() + if (p && p.then) { + startTimer() + p.then(function() { done() }, done) + } else { + nextTickish(next) + } + } catch (e) { + if (task.err != null) finalizeAsync(e) + // The errors of internal tasks (which don't have an Err) are ospec bugs and must be rethrown. + else throw e } } globalTimeout = noTimeoutRightNow diff --git a/ospec/tests/test-ospec.js b/ospec/tests/test-ospec.js index 90c26ae0..537890a3 100644 --- a/ospec/tests/test-ospec.js +++ b/ospec/tests/test-ospec.js @@ -281,6 +281,26 @@ o.spec("ospec", function() { }) }) + o.spec("throwing in test context is recoreded as a failure", function() { + var oo + o.beforeEach(function(){oo = o.new()}) + o.afterEach(function() { + oo.run(function(results) { + o(results.length).equals(1) + o(results[0].pass).equals(false) + }) + }) + o("sync test", function() { + oo("throw in sync test", function() {throw new Error}) + }) + o("async test", function() { + oo("throw in async test", function(done) { + throw new Error + done() // eslint-disable-line no-unreachable + }) + }) + }) + o.spec("timeout", function () { o("when using done()", function(done) { var oo = o.new()