How to fetch events with OpenZeppelin Test Helpers

There is expectEvent helper, but what if I want to fetch the emitted event and store value from it for my following tests.

1 Like

Hi @shakeib98,

OpenZeppelin Test Helpers has functionality to verify events where emitted with the right values, see example below. It doesn’t include functionality to parse the event. You can get the events in receipt.logs.

// test/Box.test.js

const { accounts, contract } = require('@openzeppelin/test-environment');
const { expect } = require('chai');

// Import utilities from Test Helpers
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

const Box = contract.fromArtifact('Box');

describe('Box', function () {
  const [ owner, other ] = accounts;

  // Use large integers ('big numbers')
  const value = new BN('42');

  beforeEach(async function () {
    // Deploy a new Box contract for each test
    this.contract = await Box.new({ from: owner });
  });

  // Test case
  it('retrieve returns a value previously stored', async function () {
    // Store a value - recall that only the owner account can do this!
    const receipt = await this.contract.store(42, { from: owner });

    console.log(receipt.logs)

    // Test that a ValueChanged event was emitted with the new value
    expectEvent(receipt, 'ValueChanged', { newValue: value });
  });
});

You could have a look at inLogs to see how to parse the logs: