From d14c6e37fa4c9666599e78256cc0c9ccc39d45e5 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sat, 17 Oct 2015 23:24:31 +0200 Subject: [PATCH] handle promises in throws() --- lib/assert.js | 27 +++++++++++++++++++++ lib/test.js | 18 ++++++++++++-- test/test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 101 insertions(+), 10 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index e96e507..2dbe85a 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -69,6 +69,24 @@ x.notSame = function (val, expected, msg) { }; x.throws = function (fn, err, msg) { + if (fn && fn.then) { + err = err || Error; + + return fn + .catch(err, function (err) { + return err; + }) + .then(function (value) { + if (value instanceof err) { + return; + } + + x.throws(function () { + throw value; + }, err, msg); + }); + } + try { assert.throws(fn, err, msg); } catch (err) { @@ -77,6 +95,15 @@ x.throws = function (fn, err, msg) { }; x.doesNotThrow = function (fn, msg) { + if (fn && fn.then) { + return fn + .catch(function (err) { + x.doesNotThrow(function () { + throw err; + }, msg); + }); + } + try { assert.doesNotThrow(fn, msg); } catch (err) { diff --git a/lib/test.js b/lib/test.js index 7084e5c..d3602f3 100644 --- a/lib/test.js +++ b/lib/test.js @@ -41,12 +41,26 @@ Test.prototype._assert = function () { Object.keys(assert).forEach(function (el) { Test.prototype[el] = function () { - this._assert(); + var self = this; try { - assert[el].apply(assert, arguments); + var fn = assert[el].apply(assert, arguments); + + if (fn && fn.then) { + fn + .then(function () { + self._assert(); + }) + .catch(function (err) { + self.assertError = err; + self._assert(); + }); + } else { + this._assert(); + } } catch (err) { this.assertError = err; + this._assert(); } }; }); diff --git a/test/test.js b/test/test.js index e126992..e7c6942 100644 --- a/test/test.js +++ b/test/test.js @@ -136,7 +136,7 @@ test('handle falsy testing of objects', function (t) { }); }); -test('handle throws', function (t) { +test('handle throws with error', function (t) { ava(function (a) { a.throws(function () { throw new Error('foo'); @@ -149,7 +149,45 @@ test('handle throws', function (t) { }); }); -test('handle throws with error', function (t) { +test('handle throws without error', function (t) { + ava(function (a) { + a.throws(function () { + return; + }); + + a.end(); + }).run().catch(function (err) { + t.true(err); + t.end(); + }); +}); + +test('handle throws with rejected promise', function (t) { + ava(function (a) { + a.plan(1); + + var promise = Promise.reject(new Error()); + a.throws(promise); + }).run().then(function (a) { + t.false(a.assertError); + t.end(); + }); +}); + +test('handle throws with resolved promise', function (t) { + ava(function (a) { + a.plan(1); + + var promise = Promise.resolve(); + a.throws(promise); + }).run().catch(function (err) { + t.is(err.name, 'AssertionError'); + t.true(err); + t.end(); + }); +}); + +test('handle doesNotThrow with error', function (t) { ava(function (a) { a.doesNotThrow(function () { throw new Error('foo'); @@ -157,12 +195,13 @@ test('handle throws with error', function (t) { a.end(); }).run().catch(function (err) { + t.is(err.name, 'AssertionError'); t.true(err); t.end(); }); }); -test('handle falsy throws', function (t) { +test('handle doesNotThrow without error', function (t) { ava(function (a) { a.doesNotThrow(function () { return; @@ -175,13 +214,24 @@ test('handle falsy throws', function (t) { }); }); -test('handle falsy throws with error', function (t) { +test('handle doesNotThrow with resolved promise', function (t) { ava(function (a) { - a.throws(function () { - return; - }); + a.plan(1); - a.end(); + var promise = Promise.resolve(); + a.doesNotThrow(promise); + }).run().then(function (a) { + t.false(a.assertError); + t.end(); + }); +}); + +test('handle doesNotThrow with rejected promise', function (t) { + ava(function (a) { + a.plan(1); + + var promise = Promise.reject(new Error()); + a.doesNotThrow(promise); }).run().catch(function (err) { t.true(err); t.end();