At present there is no way I can see or is documented to be able to easily use something like this with the truffle framework:

So using chai and writing a test to deploy a contract with something like this.

c = await MyContract.new();
// this wont work...
truffleAssert.eventEmitted(c, "ContractCreated", (ev) => {
    return ev.param === myInputParam;

My contract constructor will emit a ContractCreated event. I want to assert that this happened and the values are as expected. The event itself and its content is irrelevant. Its more the process of asserting events were emitted and contract construction time is important.

I would expect or want that I can do this assertion and use the assertEventEmitted function above.

Currently the only way I can see to accomplish this is so:

    let event = c.allEvents({fromBlock: 0, toBlock: 'latest'});
    event.watch(function (error, result) {
        if (!error){
            console.log("Result: ", result);
            // or some assertions similar to assertEventEmitted(...)

I can't find an elegant way to read the event logs emitted during contract construction using the truffle toolset.

When you send a transaction in you get the result object you can assert, upon contract creation the object returned is different and as such the logs are not available to check for event emission.

Does anyone know of a way to get those event logs from a truffle contract creation call and use the truffle-assertions.assertEventEmitted function to test with?

Thanks in Advance.

2 Answers 2


Having posted details of this issue to the github for truffle I got some really involved options outlined by the developers here: https://github.com/trufflesuite/truffle/issues/1196

The long and short of it is the simplest method is to do the method I showed above in a test scenario until V5 of Truffle comes out or you can move the the next release right now on NPM.

This will give you some excellent semantics to catch the tx receipt and logs in a callback at the end of the contract creation calls. They are using the newer Web3.js libraries that have a whole raft of optimisations and helper functions added to common calls.

From cgewecke:

Another thing you might be interested in is Truffle V5 (which is available in pre-release form from npm as truffle@next). It uses Web3 1.0 and lets you listen for transactionHash and receipt events per transaction. You could do something like:

  .onReceipt( receipt => {
     done(); // Tell mocha you finished

Thanks https://github.com/cgewecke for all your help!


The truffle-assertions package can easily help with this. Once the package is installed you can run the following:

// Import the package to your file
const truffleAssert = require('truffle-assertions');

// Deploy the contract
let testContract = await TestContract.new()

// Get the hash of the deployment transaction
let txHash = testContract.transactionHash

// Get the transaction result using truffleAssert
let result = await truffleAssert.createTransactionResult(testContract, txHash)

// Now look at the events using truffleAssert
truffleAssert.eventEmitted(result, 'TestEvent');

You'll find more information about the package here https://github.com/rkalis/truffle-assertions

And you can install it as follows: npm install truffle-assertions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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