From 3cd0e8bf88f7128e297e71c22ecd1bc1c6229b9c Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 2 Aug 2018 17:10:38 -0400 Subject: [PATCH] New custom Mocha reporter so Travis CI output is browser friendly. --- package.json | 4 +- tests/reporter.js | 103 +++++++++++++++++++++++++++++++++++++++++ tests/test-contract.js | 2 +- tests/test.html | 12 +++-- 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 tests/reporter.js diff --git a/package.json b/package.json index 0fcc460be..4520550b3 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,8 @@ "dist-version": "node -e \"let v = require('./package.json').version; require('fs').writeFileSync('./src.ts/_version.ts', 'export const version = \\\"' + v +'\\\";\\n')\"", "eslint": "eslint index.js contracts/*.js providers/*.js utils/*.js wallet/*.js wordlists/*.js", "test": "if [ \"$RUN_PHANTOMJS\" = \"1\" ]; then npm run-script test-phantomjs; else npm run-script test-node; fi", - "test-node": "npm run dist-test && mocha tests/test-*.js", - "test-phantomjs": "npm run dist-test && gulp tests && phantomjs --web-security=false ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js ./tests/test.html", + "test-node": "npm run dist-test && mocha --no-colors --reporter tests/reporter tests/test-*.js", + "test-phantomjs": "npm run dist-test && gulp tests && phantomjs --web-security=false ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js ./tests/test.html ./tests/reporter.js", "version": "npm dist" }, "dependencies": { diff --git a/tests/reporter.js b/tests/reporter.js new file mode 100644 index 000000000..5f30e4726 --- /dev/null +++ b/tests/reporter.js @@ -0,0 +1,103 @@ + +function getTime() { + return (new Date()).getTime(); +} + +function getDelta(t0) { + var ds = parseInt((getTime() - t0) / 1000); + var minutes = parseInt(ds / 60); + var seconds = String(parseInt(ds) % 60); + while (seconds.length < 2) { seconds = '0' + seconds; } + return '(' + minutes + ':' + seconds + ')'; +} + +function Reporter(runner) { + var self = this; + var suites = []; + + function getIndent() { + var result = ''; + for (var i = 0; i < suites.length; i++) { result += ' '; } + return result; + } + + runner.on('suite', function(suite) { + if (!suite.title) { + console.log(getIndent() + 'Testing: Found ' + suite.suites.length + ' test suites'); + } else { + var filename = (suite.file || '').split('/').pop(); + if (filename) { filename = ' (' + filename + ')'; } + console.log(getIndent() + 'Test Suite: ' + suite.title + filename); + } + suites.push(suite); + suite._t0 = getTime(); + suite._countFail = 0; + suite._countPass = 0; + suite._countTotal = 0; + }); + + runner.on('suite end', function() { + var suite = suites.pop(); + console.log(getIndent() + ' Total Tests: ' + suite._countPass + '/' + suite._countTotal + ' passed ' + getDelta(suite._t0)); + console.log(''); + if (suites.length > 0) { + var currentSuite = suites[suites.length - 1]; + currentSuite._countFail += suite._countFail; + currentSuite._countPass += suite._countPass; + currentSuite._countTotal += suite._countTotal; + } + }); + + runner.on('test', function(test) { + var currentSuite = suites[suites.length - 1]; + currentSuite._countTotal++; + }); + + runner.on('fail', function(test, error) { + var currentSuite = suites[suites.length - 1]; + currentSuite._countFail++; + + var countFail = currentSuite._countFail; + var indent = getIndent(); + + if (countFail > 100) { + if (countFail === 101) { + console.log(indent + '[ Over 100 errors; skipping remaining suite output ]'); + } + return; + } + + if (countFail > 25) { + console.log(indent + 'Failure: ' + test.title + ' (too many errors; skipping dump)'); + return; + } + + console.log(indent + 'Failure: ' + test.title); + error.toString().split('\n').forEach(function(line) { + console.log(indent + ' ' + line); + }); + Object.keys(error).forEach(function(key) { + console.log(indent + ' ' + key + ': ' + error[key]); + }); + error.stack.split('\n').forEach(function(line) { + console.log(indent + ' ' + line); + }); + }); + + runner.on('pass', function(test) { + var currentSuite = suites[suites.length - 1]; + currentSuite._countPass++; + }); + +/* + runner.once('end', function() { + var ds = ((new Date()).getTime() - t0) / 1000; + var minutes = ds / 60; + var seconds = String(ds % 60); + while (seconds.length < 2) { seconds = '0' + seconds; } + console.log('Completed in ' + minutes + ':' + seconds); + }); +*/ +} + +module.exports = Reporter; diff --git a/tests/test-contract.js b/tests/test-contract.js index feca02fc5..78217e951 100644 --- a/tests/test-contract.js +++ b/tests/test-contract.js @@ -41,7 +41,7 @@ function equals(name, actual, expected) { function TestContractEvents() { return ethers.utils.fetchJson('https://api.ethers.io/api/v1/?action=triggerTest&address=' + contract.address).then(function(data) { - console.log(' Triggered Transaction Hash: ' + data.hash); + console.log(' *** Triggered Transaction Hash: ' + data.hash); function waitForEvent(eventName, expected) { return new Promise(function(resolve, reject) { diff --git a/tests/test.html b/tests/test.html index 8fcfd5524..ddd397667 100644 --- a/tests/test.html +++ b/tests/test.html @@ -41,17 +41,19 @@ if (!window.nextTick) { window.nextTick = function (callback) { setTimeout(callback, 0); } } + + + - - - @@ -64,8 +66,10 @@