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

revert-testing.mddocs/

Transaction Revert Testing

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.

Capabilities

Expect Revert

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'
);

Expect Revert Assertion

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()
);

Expect Revert Out of Gas

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 })
);

Expect Revert Unspecified

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()
);

Provider Compatibility

The library automatically detects and validates revert reason support:

  • Ganache >=2.2.0: Full revert reason support
  • Hardhat: Full revert reason support
  • Other providers: Limited support with warnings

Note: For providers without revert reason support, use expectRevert.unspecified() to avoid false negatives.

Error Handling

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

docs

advanced-features.md

balance-tracking.md

constants-utilities.md

event-testing.md

index.md

revert-testing.md

time-manipulation.md

tile.json