From 5921b0a1370f5b126989eae9727a39e39655f701 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Fri, 21 Jun 2019 14:46:49 +0200 Subject: [PATCH] this is poc --- lib/assertions/rejects.js | 27 ++++++++++++++++++++------- lib/assertions/resolves.js | 8 ++++++++ lib/create-verifier.js | 27 +++++++++++++++++++++++++++ lib/define-assertion.js | 2 ++ 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/assertions/rejects.js b/lib/assertions/rejects.js index 7b77074d..75e8b41e 100644 --- a/lib/assertions/rejects.js +++ b/lib/assertions/rejects.js @@ -10,20 +10,33 @@ module.exports = function(referee) { function thenCallback() { this.reject("${0} did not reject, it resolved instead"); } + + function emitExecuted() { + referee.emit("async_executed"); + } + referee.add("rejects", { - assert: createAsyncAssertion(thenCallback, function(actual, expected) { - if (!samsam.identical(actual, expected)) { - this.reject(assertMessage); - return; - } - this.resolve(); - }), + assert: createAsyncAssertion( + thenCallback, + function(actual, expected) { + if (!samsam.identical(actual, expected)) { + this.reject(assertMessage); + emitExecuted(); + return; + } + this.resolve(); + emitExecuted(); + }, + referee + ), refute: createAsyncAssertion(thenCallback, function(actual, expected) { if (samsam.identical(actual, expected)) { this.reject(refuteMessage); + emitExecuted(); return; } this.resolve(); + emitExecuted(); }), assertMessage: assertMessage, refuteMessage: refuteMessage diff --git a/lib/assertions/resolves.js b/lib/assertions/resolves.js index 00b56a8a..edce9e1d 100644 --- a/lib/assertions/resolves.js +++ b/lib/assertions/resolves.js @@ -10,20 +10,28 @@ module.exports = function(referee) { function catchCallback() { this.reject("${0} did not resolve, it rejected instead"); } + function emitExecuted() { + referee.emit("async_executed"); + } + referee.add("resolves", { assert: createAsyncAssertion(function(actual, expected) { if (!samsam.identical(actual, expected)) { this.reject(assertMessage); + emitExecuted(); return; } this.resolve(); + emitExecuted(); }, catchCallback), refute: createAsyncAssertion(function(actual, expected) { if (samsam.identical(actual, expected)) { this.reject(refuteMessage); + emitExecuted(); return; } this.resolve(); + emitExecuted(); }, catchCallback), assertMessage: assertMessage, refuteMessage: refuteMessage diff --git a/lib/create-verifier.js b/lib/create-verifier.js index 73beadff..e3187ae7 100644 --- a/lib/create-verifier.js +++ b/lib/create-verifier.js @@ -3,6 +3,15 @@ function createVerifier(referee) { function verifier() { var count = 0; + var asyncCount = 0; + + function decrementAsyncCount() { + asyncCount -= 1; + } + + function incrementAsyncCount() { + asyncCount += 1; + } function incrementCount() { count += 1; @@ -11,9 +20,15 @@ function createVerifier(referee) { referee.on("pass", incrementCount); referee.on("failure", incrementCount); + referee.on("async_added", incrementAsyncCount); + referee.on("async_executed", decrementAsyncCount); + function verify(expected) { referee.off("pass", incrementCount); referee.off("failure", incrementCount); + referee.off("rejected", incrementCount); + referee.off("async_added", incrementAsyncCount); + referee.off("async_executed", decrementAsyncCount); if ( typeof expected !== "undefined" && @@ -36,6 +51,12 @@ function createVerifier(referee) { "Expected assertion count to be at least 1, but was 0" ); } + + if (expected && asyncCount > 0) { + throw new Error( + "There are " + asyncCount + " pending async assertions" + ); + } } Object.defineProperty(verify, "count", { @@ -44,6 +65,12 @@ function createVerifier(referee) { } }); + Object.defineProperty(verify, "asyncCount", { + get: function() { + return asyncCount; + } + }); + return verify; } diff --git a/lib/define-assertion.js b/lib/define-assertion.js index 20662f6f..54950db6 100644 --- a/lib/define-assertion.js +++ b/lib/define-assertion.js @@ -49,6 +49,8 @@ function createAssertion(referee, type, name, func, minArgs, messageValues) { var result = func.apply(ctx, arguments); if (typeof Promise === "function" && result instanceof Promise) { + referee.emit("async_added"); + // Here we need to return the promise in order to tell test // runners that this is an asychronous assertion. return result.then(function() {