3

It seems that when using the latest solidity-coverage (0.5.11), truffle (5.0.0-next.25) ganache-cli (6.2.4-beta.1) and solc (0.5.0) additional events / logs are emitted during tests which make the unit-tests fail.

My test code was like:

it('addSupplier', async () => {
        // Act
        const result = await contract.addSupplier(supplier1, 'Supplier 1');

        // Assert
        assert.lengthOf(result.logs, 1);

        const log = result.logs[0];
        assert.isNotNull(log);

        const event = log.event;
        assert.equal(event, 'AddSupplierEvent');

        const eventArgs = log.args;
        assert.isNotEmpty(eventArgs.from);
        assert.isNotEmpty(eventArgs.id);
        assert.equal(eventArgs.id, supplier1);
        assert.equal(eventArgs.name, 'Supplier 1');
});

And I expected 1 event to be submitted : AddSupplierEvent, however the output shows:

Events emitted during test:
[Coverage]     ---------------------------
[Coverage]
[Coverage]     __FunctionCoverageSupplyChainLogContract(fileName: C:/.../contracts/SupplyChainLogContract.sol, fnId: 1)
[Coverage]     __CoverageSupplyChainLogContract(fileName: C:/.../contracts/SupplyChainLogContract.sol, lineNumber: 9)
[Coverage]     __StatementCoverageSupplyChainLogContract(fileName: C:/.../contracts/SupplyChainLogContract.sol, statementId: 1)
...
[Coverage]     AddSupplierEvent(from: 0xd721da73Adf6AfBFfaC15A8a4Bae662D5D02c351, id: 0x8caa8760a712e9E96868314Bc59711094BAB263E, name: Supplier 1)

Is this by design ?

  • Do your tests pass with previous package versions (say, Truffle 0.4.14, Ganache 6.1.8 and Solc 0.4.24)? – goodvibration Dec 16 '18 at 10:46
  • Previous solidity version was 0.4.24 indeed, and probably also that Truffle version. (Instead of Ganache CLI, TestRPC was used ) – Stef Heyenrath Dec 16 '18 at 13:01
  • And are you sure that the exact same test passes under this configuration? – goodvibration Dec 16 '18 at 13:09
3

I would suggest using my truffle-assertions library to assert whether events have been emitted. Using that, it doesn't matter what index an event has been emitted, just that it has been emitted. That means your assertions will be a bit less strict than they are currently, but it will be more readable and you won't experience the issues you faced with solidity-coverage, since the emit order is irrelevant.

You can install the library with npm

npm install truffle-assertions

It can then be imported at the top of your test file

const truffleAssert = require('truffle-assertions');

And your test would change to the following:

it('addSupplier', async () => {
    const result = await contract.addSupplier(supplier1, 'Supplier 1');

    truffleAssert.eventEmitted(result, 'AddSupplierEvent', (ev) => {
        return !!ev.from && ev.id === supplier1 && ev.name === 'Supplier 1';
    });
});

Alternatively, you could choose to filter all solidity-coverage events from the log by doing something like the following:

const logs = result.logs.filter((ev) => !ev.event.startsWith('__'));
const log = logs[0];
...

This filters out all events that start with two underscores, which appears to be the pattern that solidity-coverage is following.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.