New custom Mocha reporter so Travis CI output is browser friendly.

This commit is contained in:
Richard Moore 2018-08-02 17:10:38 -04:00
parent 957ccd2eaf
commit 3cd0e8bf88
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
4 changed files with 114 additions and 7 deletions

View File

@ -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')\"", "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", "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": "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-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", "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" "version": "npm dist"
}, },
"dependencies": { "dependencies": {

103
tests/reporter.js Normal file
View File

@ -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;

View File

@ -41,7 +41,7 @@ function equals(name, actual, expected) {
function TestContractEvents() { function TestContractEvents() {
return ethers.utils.fetchJson('https://api.ethers.io/api/v1/?action=triggerTest&address=' + contract.address).then(function(data) { 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) { function waitForEvent(eventName, expected) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {

View File

@ -41,17 +41,19 @@
if (!window.nextTick) { if (!window.nextTick) {
window.nextTick = function (callback) { setTimeout(callback, 0); } window.nextTick = function (callback) { setTimeout(callback, 0); }
} }
</script>
<!-- Inject the mocha describe and it functions -->
<script type="text/javascript">
// https://github.com/nathanboktae/mocha-phantomjs-core#usage // https://github.com/nathanboktae/mocha-phantomjs-core#usage
if (typeof(initMochaPhantomJS) === 'function') { if (typeof(initMochaPhantomJS) === 'function') {
initMochaPhantomJS(); initMochaPhantomJS();
} }
// Inject the mocha describe and it functions
mocha.setup({ ui: 'bdd' });
</script> </script>
<!-- Inject the mocha describe and it functions -->
<script type="text/javascript">mocha.setup('bdd')</script>
<!-- Load the browser dist ethers package --> <!-- Load the browser dist ethers package -->
<script type="text/javascript" src="../dist/ethers.min.js"></script> <script type="text/javascript" src="../dist/ethers.min.js"></script>
<script type="text/javascript" src="../dist/wordlist-it.js"></script> <script type="text/javascript" src="../dist/wordlist-it.js"></script>
@ -64,8 +66,10 @@
<!-- Run the test cases! --> <!-- Run the test cases! -->
<script type="text/javascript"> <script type="text/javascript">
mocha.reporter(tests.reporter);
// Use this to focus on specific test cases // Use this to focus on specific test cases
//mocha.grep(new RegExp('wordlists')).run(); //mocha.grep(new RegExp('easyseed')).run();
mocha.run(); mocha.run();
</script> </script>
</body> </body>