From 1d331b41ecae3104568d31215641089a4de8f60c Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Thu, 18 Jul 2019 15:09:56 -0400 Subject: [PATCH] test: Add testAsync() helper to Suite Add testAsync() helper function to Suite to simplify running tests that return a Promise. The test action is executed and if a syncronous error is thrown then it is immediately considered failed. If the Promise resolves successfully then the test is considered successful. If the Promise rejects with an Error then the test is considered failed. --- test/suite.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/suite.js b/test/suite.js index eca96159e..5960f2327 100644 --- a/test/suite.js +++ b/test/suite.js @@ -72,6 +72,19 @@ class Suite { const test = new Test(name, cb) this._queue.push(test) } + + testAsync (name, action) { + const test = new Test(name, cb => { + try { + Promise.resolve(action()) + .then(() => cb(null)) + .catch((err) => cb(err)) + } catch (err) { + cb(err) + } + }) + this._queue.push(test) + } } process.on('unhandledRejection', (e) => {