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
Comprehensive assertion utilities for testing transaction failures with specific error messages, gas limit errors, and invalid opcodes. Essential for ensuring smart contracts fail gracefully under invalid conditions.
Tests that a transaction reverts with a specific error message.
/**
* Assert that a promise rejects with a specific revert reason
* @param {Promise} promise - Promise that should revert
* @param {string} expectedError - Expected revert reason string
* @returns {Promise<void>} - Resolves if assertion passes
*/
async function expectRevert(promise, expectedError);Usage Example:
const { expectRevert } = require('@openzeppelin/test-helpers');
// Test with specific revert reason
await expectRevert(
token.transfer(constants.ZERO_ADDRESS, 100),
'ERC20: transfer to the zero address'
);
// Test with custom contract error
await expectRevert(
myContract.restrictedFunction({ from: unauthorizedUser }),
'Ownable: caller is not the owner'
);Tests that a transaction reverts due to an assertion failure (invalid opcode).
/**
* Assert that a promise rejects with an assertion failure
* @param {Promise} promise - Promise that should fail with assertion
* @returns {Promise<void>} - Resolves if assertion passes
*/
function expectRevert.assertion(promise);Usage Example:
// Test assertion failure (invalid opcode)
await expectRevert.assertion(
myContract.functionWithAssert()
);Tests that a transaction reverts due to running out of gas.
/**
* Assert that a promise rejects due to out of gas error
* @param {Promise} promise - Promise that should run out of gas
* @returns {Promise<void>} - Resolves if assertion passes
*/
function expectRevert.outOfGas(promise);Usage Example:
// Test out of gas scenario
await expectRevert.outOfGas(
myContract.expensiveFunction({ gas: 21000 })
);Tests that a transaction reverts without checking the specific error message.
/**
* Assert that a promise rejects with any revert reason
* @param {Promise} promise - Promise that should revert
* @returns {Promise<void>} - Resolves if assertion passes
*/
function expectRevert.unspecified(promise);Usage Example:
// Test any revert without specific message
await expectRevert.unspecified(
myContract.unstableFunction()
);The library automatically detects and validates revert reason support:
Note: For providers without revert reason support, use expectRevert.unspecified() to avoid false negatives.
The library provides detailed error messages when assertions fail:
// If wrong revert reason is encountered:
// AssertionError: Wrong kind of exception received
// Expected: "ERC20: transfer to the zero address"
// Actual: "ERC20: insufficient balance"Install with Tessl CLI
npx tessl i tessl/npm-openzeppelin--test-helpers