JavaScript testing helpers for Ethereum smart contract development with assertions, event verification, balance tracking, and time manipulation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Advanced event assertion utilities for verifying emitted events with parameter validation, supporting both Truffle and web3 receipt formats. Essential for comprehensive smart contract testing.
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
});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,
});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,
});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');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);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);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' }
}
}
}Event parameter matching supports:
// 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
});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 500Install with Tessl CLI
npx tessl i tessl/npm-openzeppelin--test-helpers