Skip to content

Commit

Permalink
feat(test/reporter): only show tx details on option
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville committed Mar 1, 2019
1 parent 87d92b6 commit 2173576
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
17 changes: 13 additions & 4 deletions packages/embark/src/cmd/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ class Cmd {
' vm - ' + __('start and use an Ethereum simulator (ganache)') + '\n' +
' embark - ' + __('use the node of a running embark process') + '\n' +
' <endpoint> - ' + __('connect to and use the specified node'))
.option('-d , --gasDetails', __('print the gas cost for each contract deployment when running the tests'))
.option('--gasDetails', __('print the gas cost for each contract deployment when running the tests. Deprecated: Please use --gas-details'))
.option('-d , --gas-details', __('print the gas cost for each contract deployment when running the tests'))
.option('-t , --tx-details', __('print the details of the transactions that happen during tests'))
.option('-c , --coverage', __('generate a coverage report after running the tests (vm only)'))
.option('--nobrowser', __('do not start browser after coverage report is generated'))
.option('--locale [locale]', __('language to use (default: en)'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'warn')
.option('--solc', __('run only solidity tests'))
Expand All @@ -261,8 +262,16 @@ class Cmd {
process.exit(1);
}
i18n.setOrDetectLocale(options.locale);
embark.runTests({file, solc:options.solc, logLevel: options.loglevel, gasDetails: options.gasDetails,
node: options.node, coverage: options.coverage, noBrowser: options.nobrowser, env: options.env || 'development'});
embark.runTests({
file,
solc: options.solc,
logLevel: options.loglevel,
gasDetails: options.gasDetails,
txDetails: options.txDetails,
node: options.node,
coverage: options.coverage,
env: options.env || 'development'
});
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/embark/src/lib/modules/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class TestRunner {
mocha.reporter(reporter, {
events: self.events,
gasDetails: options.gasDetails,
txDetails: options.txDetails,
gasLimit
});

Expand Down
46 changes: 27 additions & 19 deletions packages/embark/src/lib/modules/tests/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class EmbarkApiSpec extends Base {
};
};

runner.on('start', () => { this.embark.events.request('tests:results:reset'); });
runner.on('pass', test => { this.embark.events.request('tests:results:report', formatTest(test)); });
runner.on('fail', test => { this.embark.events.request('tests:results:report', formatTest(test)); });
runner.on('suite', suite => { if(suite.title !== '') suiteStack.push(suite.title); });
runner.on('start', () => this.embark.events.request('tests:results:reset'));
runner.on('pass', test => this.embark.events.request('tests:results:report', formatTest(test)));
runner.on('fail', test => this.embark.events.request('tests:results:report', formatTest(test)));
runner.on('suite', suite => {
if (suite.title !== '') suiteStack.push(suite.title);
});
runner.on('suite end', () => suiteStack.pop());
}
}
Expand All @@ -38,6 +40,7 @@ class EmbarkSpec extends Base {
self.listenForGas = true;
self.embarkEvents = options.reporterOptions.events;
self.gasDetails = options.reporterOptions.gasDetails;
self.txDetails = options.reporterOptions.txDetails;
self.gasLimit = options.reporterOptions.gasLimit;
let indents = 0;
let n = 0;
Expand All @@ -49,12 +52,14 @@ class EmbarkSpec extends Base {
self.txLogs = [];

function onContractReceipt(receipt) {
self.embarkEvents.request('contracts:contract', receipt.className, (contract) => {
if (contract) {
self.contracts.push(contract);
self.addressToContract = getAddressToContract(self.contracts, self.addressToContract);
}
});
if (self.txDetails) {
self.embarkEvents.request('contracts:contract', receipt.className, (contract) => {
if (contract) {
self.contracts.push(contract);
self.addressToContract = getAddressToContract(self.contracts, self.addressToContract);
}
});
}

if (self.gasDetails) {
const fmt = color('bright pass', ' ') +
Expand All @@ -68,12 +73,15 @@ class EmbarkSpec extends Base {
}

async function onBlockHeader(blockHeader) {
if(!self.listenForGas) {
if (!self.listenForGas) {
return;
}
self.stats.totalGasCost += blockHeader.gasUsed;
self.stats.test.gasUsed += blockHeader.gasUsed;

if (!self.txDetails) {
return;
}
self.embarkEvents.request("blockchain:block:byNumber", blockHeader.number, (err, block) => {
if (err) {
return this.logger.error('Error getting block header', err.message || err);
Expand Down Expand Up @@ -110,37 +118,37 @@ class EmbarkSpec extends Base {
return Array(indents).join(' ');
}

runner.on('start', function () {
runner.on('start', function() {
console.log();
});

runner.on('suite', function (suite) {
runner.on('suite', function(suite) {
++indents;
if (self.gasDetails) {
console.log();
}
console.log(color('suite', '%s%s'), indent(), suite.title);
});

runner.on('suite end', function () {
runner.on('suite end', function() {
--indents;
if (indents === 1) {
console.log();
}
});

runner.on('pending', function (test) {
runner.on('pending', function(test) {
const fmt = indent() + color('pending', ' - %s');
console.log(fmt, test.title);
});


runner.on('test', function () {
runner.on('test', function() {
self.stats.test.gasUsed = 0;
self.contracts = [];
});

runner.on('pass', function (test) {
runner.on('pass', function(test) {
let fmt =
indent() +
color('checkmark', ' ' + Base.symbols.ok) +
Expand All @@ -153,14 +161,14 @@ class EmbarkSpec extends Base {
self.txLogs = [];
});

runner.on('fail', function (test) {
runner.on('fail', function(test) {
console.log(indent() + color('fail', ' %d) %s') + ' - ' + color(self.getGasColor(self.stats.test.gasUsed), '[%d gas]'),
++n, test.title, self.stats.test.gasUsed);
self.txLogs.forEach(log => console.log(log));
self.txLogs = [];
});

runner.once('end', function () {
runner.once('end', function() {
runner.removeAllListeners();
self.embarkEvents.removeListener("deploy:contract:receipt", onContractReceipt);
self.embarkEvents.removeListener("block:header", onBlockHeader);
Expand Down

0 comments on commit 2173576

Please sign in to comment.