Consider following contract:
contract Emitter {
event Called();
function emit() {
Called();
}
}
And tests:
contract('Emitter', function(accounts) {
it('should emit Called event and fail (shows 1 emitted)', function(done) {
var emitter = Emitter.deployed();
emitter.emit().then(function() {
assert.isTrue(false);
}).then(done).catch(done);
});
it('should emit Called event', function(done) {
var emitter = Emitter.deployed();
emitter.emit().then(function() {
done();
}).catch(done);
});
it('should emit Called event 2', function(done) {
var emitter = Emitter.deployed();
emitter.emit().then(function() {
done();
}).catch(done);
});
it('should emit Called event and fail 2 (shows 3 emitted)', function(done) {
var emitter = Emitter.deployed();
emitter.emit().then(function() {
assert.isTrue(false);
}).then(done).catch(done);
});
it('should emit Called event and fail 3 (shows 1 emitted)', function(done) {
var emitter = Emitter.deployed();
emitter.emit().then(function() {
assert.isTrue(false);
}).then(done).catch(done);
});
});
Output:
Contract: Emitter
1) should emit Called event and fail (shows 1 emitted)
Events emitted during test:
---------------------------
Called()
---------------------------
✓ should emit Called event
✓ should emit Called event 2 (48ms)
2) should emit Called event and fail 2 (shows 3 emitted)
Events emitted during test:
---------------------------
Called()
Called()
Called()
---------------------------
3) should emit Called event and fail 3 (shows 1 emitted)
Events emitted during test:
---------------------------
Called()
---------------------------
2 passing (8s)
3 failing
As you can see, when 3rd test fails, it reports the events emitted between last failed test (exclusive) and current test (inclusive).
Hi there,
I had meant to respond to this sooner. Can you try out the 0.4.0 pre-release? https://github.com/ConsenSys/truffle/releases/tag/v0.3.9
One of the fixes/features is the following, which I believe fixes your issue:
Better log processing during failing tests. If one of your tests fail, Truffle will now better show the logs that applied to that specific failing test rather than display all logs fired over the course of the test run.
Please reopen if this does not fix your issue.
@tcoulter Hello, is there any way to display events emitted in all cases (even for succeeded tests)?
@cbruguera Yes, I believe this should be an option; potentially a set option in the mocha config.
@OTTTO @cbruguera @tcoulter Do you know how to log events at ALL times?
assert.equal(true, false); // failing on purpose to see log events
Any better options?
+1 for a simple general event list.
I'm having a case where I call contract A, which then calls contract B, and B emit events, those are not shown in the transaction logs, so I had to implement a whole listener on the side to capture those.
Simple version:
function getLastEvents(contract) {
return new Promise((resolve, reject) => {
const evFilter = contract.allEvents({
fromBlock: 'latest',
toBlock: 'latest',
});
evFilter.get((err, res) => {
if (err) return reject(err);
resolve(res);
});
});
}
Most helpful comment
@cbruguera Yes, I believe this should be an option; potentially a set option in the mocha config.