CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-openzeppelin--test-helpers

JavaScript testing helpers for Ethereum smart contract development with assertions, event verification, balance tracking, and time manipulation.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

event-testing.mddocs/

Event Emission Testing

Advanced event assertion utilities for verifying emitted events with parameter validation, supporting both Truffle and web3 receipt formats. Essential for comprehensive smart contract testing.

Capabilities

Expect Event

Asserts that a specific event was emitted in a transaction receipt with optional parameter verification.

/**
 * Assert that an event was emitted in a transaction receipt
 * @param {TransactionReceipt} receipt - Transaction receipt to check
 * @param {string} eventName - Name of the event to look for
 * @param {object} eventArgs - Optional object with expected event parameters
 * @returns {object} - The matching event object
 */
function expectEvent(receipt, eventName, eventArgs = {});

Usage Examples:

const { expectEvent } = require('@openzeppelin/test-helpers');

// Basic event assertion
const receipt = await token.transfer(recipient, amount);
expectEvent(receipt, 'Transfer');

// Event with parameter validation
expectEvent(receipt, 'Transfer', {
  from: sender,
  to: recipient,
  value: amount,
});

// Partial parameter matching
expectEvent(receipt, 'Approval', {
  owner: owner,
  // spender and value not checked
});

Expect Event in Construction

Asserts that an event was emitted during contract construction.

/**
 * Assert that an event was emitted during contract construction
 * @param {ContractInstance} contract - Truffle contract instance
 * @param {string} eventName - Name of the event to look for
 * @param {object} eventArgs - Optional object with expected event parameters
 * @returns {Promise<object>} - The matching event object
 */
async function expectEvent.inConstruction(contract, eventName, eventArgs = {});

Usage Example:

// Check event emitted in constructor
const myContract = await MyContract.new(initialValue);
await expectEvent.inConstruction(myContract, 'Initialized', {
  value: initialValue,
});

Expect Event in Transaction

Asserts that an event was emitted in a specific transaction by hash.

/**
 * Assert that an event was emitted in a transaction by hash
 * @param {string} txHash - Transaction hash to check
 * @param {ContractInstance} emitter - Contract that should have emitted the event
 * @param {string} eventName - Name of the event to look for
 * @param {object} eventArgs - Optional object with expected event parameters
 * @returns {Promise<object>} - The matching event object
 */
async function expectEvent.inTransaction(txHash, emitter, eventName, eventArgs = {});

Usage Example:

// Check event in external transaction
const tx = await web3.eth.sendTransaction({
  from: sender,
  to: contract.address,
  data: encodedData,
});

await expectEvent.inTransaction(tx.transactionHash, contract, 'DataReceived', {
  sender: sender,
  data: expectedData,
});

Expect Event Not Emitted

Asserts that a specific event was NOT emitted in a transaction receipt.

/**
 * Assert that an event was NOT emitted in a transaction receipt
 * @param {TransactionReceipt} receipt - Transaction receipt to check
 * @param {string} eventName - Name of the event that should not be present
 * @returns {void}
 */
function expectEvent.notEmitted(receipt, eventName);

Usage Example:

// Ensure event was not emitted
const receipt = await token.approve(spender, 0);
expectEvent.notEmitted(receipt, 'Transfer');

Expect Event Not Emitted in Construction

Asserts that an event was NOT emitted during contract construction.

/**
 * Assert that an event was NOT emitted during contract construction
 * @param {ContractInstance} contract - Truffle contract instance
 * @param {string} eventName - Name of the event that should not be present
 * @returns {Promise<void>}
 */
async function expectEvent.notEmitted.inConstruction(contract, eventName);

Expect Event Not Emitted in Transaction

Asserts that an event was NOT emitted in a specific transaction by hash.

/**
 * Assert that an event was NOT emitted in a transaction by hash
 * @param {string} txHash - Transaction hash to check
 * @param {ContractInstance} emitter - Contract to check for events
 * @param {string} eventName - Name of the event that should not be present
 * @returns {Promise<void>}
 */
async function expectEvent.notEmitted.inTransaction(txHash, emitter, eventName);

Receipt Format Support

The library automatically handles both receipt formats:

Truffle Format:

{
  logs: [
    {
      event: 'Transfer',
      args: { from: '0x...', to: '0x...', value: BN }
    }
  ]
}

Web3 Format:

{
  events: {
    'Transfer': {
      returnValues: { from: '0x...', to: '0x...', value: '1000' }
    }
  }
}

Parameter Matching

Event parameter matching supports:

  • Exact matching: String, number, and boolean values
  • Big Number matching: Automatic BN comparison using chai-bn
  • Partial matching: Only specified parameters are checked
  • Null values: Explicit null parameter checking
// Big Number parameter matching
expectEvent(receipt, 'Transfer', {
  value: new BN('1000000000000000000'), // 1 ether in wei
});

// Null parameter checking
expectEvent(receipt, 'Approval', {
  spender: constants.ZERO_ADDRESS,
  value: null, // explicitly check for null
});

Error Messages

Detailed error messages help debug test failures:

// When event not found:
// AssertionError: No 'Transfer' events found

// When parameters don't match:
// AssertionError: expected event argument 'value' to have value 1000 but got 500

Install with Tessl CLI

npx tessl i tessl/npm-openzeppelin--test-helpers

docs

advanced-features.md

balance-tracking.md

constants-utilities.md

event-testing.md

index.md

revert-testing.md

time-manipulation.md

tile.json