From e525eaadcc2688dc57a84b4ad13ab20aa97be93f Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 1 Feb 2018 18:18:06 -0500 Subject: [PATCH] abstract test results into callbacks --- index.js | 24 +++++++++++++++++++++++- src/testRunner.js | 21 ++++++++------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index eaf3785691..980b58eba6 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,29 @@ let Deployer = require('./src/deployer.js'); let TestRunner = require('./src/testRunner.js'); var runTest = function(contractName, contractObj, cb) { - TestRunner.runTest(contractName, contractObj, cb); + var testCallback = function(result) { + if (result.type === 'contract') { + console.log(("#" + result.value).green); + } else if (result.type === 'testPass') { + console.log("\t✓ ".green.bold + result.value.grey); + } else if (result.type === 'testFailure') { + console.log("\t✘ ".bold.red + result.value.red); + } + } + var resultsCallback = function(err, result) { + if (err) { + return cb(err); + } + if (result.passingNum > 0) { + console.log((result.passingNum + " passing").green); + } + if (result.failureNum > 0) { + console.log((result.failureNum + " failing").red); + } + cb(); + } + + TestRunner.runTest(contractName, contractObj, testCallback, resultsCallback); } var runTestFile = function(filename, web3) { diff --git a/src/testRunner.js b/src/testRunner.js index 3c047a6d02..5bc69129a5 100644 --- a/src/testRunner.js +++ b/src/testRunner.js @@ -2,7 +2,7 @@ var async = require('async'); var changeCase = require('change-case'); require('colors'); -function runTest(testName, testObject, callback) { +function runTest(testName, testObject, testCallback, resultsCallback) { let runList = []; let specialFunctions = ['beforeAll']; let availableFunctions = testObject._jsonInterface.filter((x) => x.type === 'function').map((x) => x.name); @@ -18,19 +18,16 @@ function runTest(testName, testObject, callback) { let passingNum = 0, failureNum = 0; - console.log(("#" + testName).green); + testCallback({type: "contract", value: testName}); async.eachOfLimit(runList, 1, function(func, index, next) { let method = testObject.methods[func.name].apply(testObject.methods[func.name], []); if (func.constant) { method.call().then((result) => { if (result) { - // TODO: should instead be returned in a callback, the caller can - // decide how to handle the output (so works both in console and - // browser) - console.log("\t✓ ".green.bold + changeCase.sentenceCase(func.name).grey); + testCallback({type: "testPass", value: changeCase.sentenceCase(func.name)}); passingNum += 1; } else { - console.log("\t✘ ".bold.red + changeCase.sentenceCase(func.name).red); + testCallback({type: "testFailure", value: changeCase.sentenceCase(func.name)}); failureNum += 1; } next(); @@ -41,12 +38,10 @@ function runTest(testName, testObject, callback) { }); } }, function() { - if (passingNum > 0) { - console.log((passingNum + " passing").green); - } - if (failureNum > 0) { - console.log((failureNum + " failing").red); - } + resultsCallback(null, { + passingNum: passingNum, + failureNum: failureNum + }); }); }