mirror of https://github.com/embarklabs/embark.git
feat(@embark/test-runner): add reports to tests (#1864)
This commit is contained in:
parent
f9557d4c93
commit
230fe592a6
|
@ -16,7 +16,7 @@ class ContractsManager {
|
|||
this.contracts = {};
|
||||
this.contractDependencies = {};
|
||||
this.deployOnlyOnConfig = false;
|
||||
this.compileError = false;
|
||||
this.compileError = null;
|
||||
this.compileOnceOnly = options.compileOnceOnly;
|
||||
this._web3 = null;
|
||||
|
||||
|
@ -532,12 +532,12 @@ class ContractsManager {
|
|||
}
|
||||
], function (err) {
|
||||
if (err) {
|
||||
self.compileError = true;
|
||||
self.compileError = err;
|
||||
self.events.emit("status", __("Build error"));
|
||||
self.events.emit("outputError", __("Error building Dapp, please check console"));
|
||||
self.logger.error(__("Error Building contracts"));
|
||||
} else {
|
||||
self.compileError = false;
|
||||
self.compileError = null;
|
||||
}
|
||||
self.logger.trace("finished".underline);
|
||||
|
||||
|
|
|
@ -48,14 +48,16 @@
|
|||
"@babel/cli": "7.2.3",
|
||||
"@babel/core": "7.2.2",
|
||||
"@types/async": "2.0.50",
|
||||
"async": "3.1.0",
|
||||
"cross-env": "5.2.0",
|
||||
"eslint": "5.7.0",
|
||||
"mocha": "6.2.0",
|
||||
"npm-run-all": "4.1.5",
|
||||
"rimraf": "3.0.0",
|
||||
"tslint": "5.16.0",
|
||||
"typescript": "3.4.5",
|
||||
"typescript": "3.4.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "2.6.1",
|
||||
"mocha": "6.2.0",
|
||||
"web3": "1.2.1"
|
||||
},
|
||||
"engines": {
|
||||
|
|
|
@ -37,6 +37,7 @@ class MochaTestRunner {
|
|||
|
||||
run(options, cb) {
|
||||
const {events} = this.embark;
|
||||
const {reporter} = options;
|
||||
|
||||
const Module = require("module");
|
||||
const originalRequire = require("module").prototype.require;
|
||||
|
@ -67,6 +68,10 @@ class MochaTestRunner {
|
|||
next();
|
||||
}
|
||||
], (err) => {
|
||||
// Reset the gas accumulator so that we don't show deployment gas on the
|
||||
// first test.
|
||||
reporter.resetGas();
|
||||
|
||||
if (acctCb) {
|
||||
acctCb(err, accounts);
|
||||
}
|
||||
|
@ -80,8 +85,8 @@ class MochaTestRunner {
|
|||
(next) => { // request provider
|
||||
events.request("blockchain:client:provider", "ethereum", next);
|
||||
},
|
||||
(bcProvider, next) => { // set provider
|
||||
web3 = new Web3(bcProvider);
|
||||
(provider, next) => { // set provider
|
||||
web3 = new Web3(provider);
|
||||
next();
|
||||
},
|
||||
(next) => { // get accounts
|
||||
|
@ -126,7 +131,7 @@ class MochaTestRunner {
|
|||
(next) => { // initialize Mocha
|
||||
const mocha = new Mocha();
|
||||
|
||||
mocha.reporter(Reporter, { reporter: options.reporter });
|
||||
mocha.reporter(Reporter, { reporter: reporter });
|
||||
const describeWithAccounts = (scenario, cb) => {
|
||||
Mocha.describe(scenario, cb.bind(mocha, accounts));
|
||||
};
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
const Mocha = require('mocha');
|
||||
const {
|
||||
// EVENT_RUN_BEGIN,
|
||||
// EVENT_RUN_END,
|
||||
EVENT_TEST_BEGIN,
|
||||
EVENT_TEST_FAIL,
|
||||
EVENT_TEST_PASS
|
||||
// EVENT_SUITE_BEGIN,
|
||||
// EVENT_SUITE_END
|
||||
} = Mocha.Runner.constants;
|
||||
|
||||
class Reporter {
|
||||
|
@ -17,14 +14,19 @@ class Reporter {
|
|||
}
|
||||
|
||||
wireRunner() {
|
||||
// let testName = '';
|
||||
let startTime;
|
||||
|
||||
this.runner
|
||||
.on(EVENT_TEST_BEGIN, _test => {
|
||||
startTime = Date.now();
|
||||
})
|
||||
.on(EVENT_TEST_PASS, test => {
|
||||
this.reporter.report(test.fullTitle(), true);
|
||||
const duration = (Date.now() - startTime) / 1000.0;
|
||||
this.reporter.report(test.fullTitle(), duration, true);
|
||||
})
|
||||
.on(EVENT_TEST_FAIL, (test, err) => {
|
||||
this.reporter.report(test.fullTitle(), false, err.message);
|
||||
const duration = (Date.now() - startTime) / 1000.0;
|
||||
this.reporter.report(test.fullTitle(), duration, false, err.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,17 +3,17 @@ class Reporter {
|
|||
this.reporter = reporter;
|
||||
}
|
||||
|
||||
report(payload) {
|
||||
report(_err, payload) {
|
||||
switch(payload.type) {
|
||||
case 'contract':
|
||||
this.contract = payload.value;
|
||||
this.file = payload.filename;
|
||||
break;
|
||||
case 'testPass':
|
||||
this.reporter.report(`${this.contract} ${payload.value}`, true);
|
||||
this.reporter.report(`${ this.contract } ${ payload.value }`, payload.time, true);
|
||||
break;
|
||||
case 'testFailure':
|
||||
this.reporter.report(`${this.contract} ${payload.value}`, false, payload.errMsg);
|
||||
this.reporter.report(`${ this.contract } ${ payload.value }`, payload.time, false, payload.errMsg);
|
||||
break;
|
||||
default:
|
||||
console.log('dont know how to handle');
|
||||
|
@ -23,4 +23,3 @@ class Reporter {
|
|||
}
|
||||
|
||||
module.exports = Reporter;
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@
|
|||
"dependencies": {
|
||||
"@babel/runtime-corejs2": "7.3.1",
|
||||
"async": "2.6.1",
|
||||
"chalk": "2.4.2",
|
||||
"embark-i18n": "^4.1.1",
|
||||
"embark-utils": "^4.1.1",
|
||||
"fs-extra": "7.0.1",
|
||||
|
|
|
@ -30,7 +30,7 @@ class TestRunner {
|
|||
}
|
||||
|
||||
run(options, cb) {
|
||||
const reporter = new Reporter();
|
||||
const reporter = new Reporter(this.embark);
|
||||
const testPath = options.file || "test";
|
||||
|
||||
async.waterfall([
|
||||
|
|
|
@ -1,7 +1,32 @@
|
|||
const chalk = require('chalk');
|
||||
|
||||
class Reporter {
|
||||
constructor() {
|
||||
constructor(embark) {
|
||||
this.embark = embark;
|
||||
|
||||
this.passes = 0;
|
||||
this.fails = 0;
|
||||
this.gasAccumulator = 0;
|
||||
|
||||
this.wireGasUsage();
|
||||
}
|
||||
|
||||
wireGasUsage() {
|
||||
const {events} = this.embark;
|
||||
events.on('blockchain:proxy:response', (params) => {
|
||||
const {result} = params.respData;
|
||||
|
||||
if (!result || !result.gasUsed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const gas = parseInt(result.gasUsed);
|
||||
this.gasAccumulator += gas;
|
||||
});
|
||||
}
|
||||
|
||||
resetGas() {
|
||||
this.gasAccumulator = 0;
|
||||
}
|
||||
|
||||
header() {
|
||||
|
@ -18,15 +43,30 @@ class Reporter {
|
|||
}
|
||||
}
|
||||
|
||||
report(test, passed, message) {
|
||||
report(test, time, passed, message) {
|
||||
let timeFormat = 'green';
|
||||
if (time > 0.7) {
|
||||
timeFormat = 'yellow';
|
||||
} else if(time > 1) {
|
||||
timeFormat = 'red';
|
||||
}
|
||||
|
||||
const formattedGas = this.formatNumber(this.gasAccumulator);
|
||||
|
||||
if (passed) {
|
||||
this.passes++;
|
||||
|
||||
process.stdout.write(`>>> ${test} > PASS\n`);
|
||||
process.stdout.write(chalk`{bgGreen.white.bold ${' PASS '}} {underline ${ test }} {bold >} {${ timeFormat } ${ time }s} {bold >} {bold ${ formattedGas } gas}\n`);
|
||||
} else {
|
||||
this.fails++;
|
||||
process.stdout.write(`>>> ${test} > FAIL (${message || 'no error message'})\n`);
|
||||
process.stdout.write(chalk`{bgRed.white.bold ${' FAIL '}} {underline ${ test }} {bold >} {${ timeFormat } ${ time }s} {bold >} {bold ${ formattedGas } gas} > {red ${message || 'no error message'}}\n`);
|
||||
}
|
||||
|
||||
this.resetGas();
|
||||
}
|
||||
|
||||
// stolen from https://blog.abelotech.com/posts/number-currency-formatting-javascript/
|
||||
formatNumber(num) {
|
||||
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue