Skip to content
JavaScript testing helpers for Ethereum smart contract development
JavaScript Shell Solidity
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
contracts Decouple from truffle (#75) Oct 4, 2019
migrations Added truffle. Nov 30, 2018
scripts Scope npm package (#78) Oct 10, 2019
src Add advanceBlockTo method (#94) Nov 13, 2019
test-integration Rename 'environment' config option & add singletons setup (#96) Nov 12, 2019
test Add advanceBlockTo method (#94) Nov 13, 2019
.eslintignore Fixed linter setup. Dec 21, 2018
.eslintrc Decouple from truffle (#75) Oct 4, 2019
.gitattributes Added Solidity syntax highlighting. Nov 30, 2018
.gitignore Revert "Load ERC1820 registry using truffle artifacts" Oct 10, 2019
.travis.yml Accept web3 1.2.0 as valid version (#63) Jul 26, 2019 Add advanceBlockTo method (#94) Nov 13, 2019
LICENSE Initial commit Nov 30, 2018 Add advanceBlockTo method (#94) Nov 13, 2019
configure.js Rename 'environment' config option & add singletons setup (#96) Nov 12, 2019
package-lock.json 0.5.4 Nov 13, 2019
package.json 0.5.4 Nov 13, 2019
truffle-config.js Update contracts to compile using 0.5.8. (#55) Jun 6, 2019

OpenZeppelin Test Helpers

NPM Package Build Status

JavaScript testing helpers for Ethereum smart contract development. With support for Truffle and plain web3.js workflows.


npm install --save-dev @openzeppelin/test-helpers


// Import the modules you want from @openzeppelin/test-helpers
const { BN, constants, balance, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

// Optionally import Chai to write your assertions (must be installed separately)
const { expect } = require('chai');

const ERC20 = artifacts.require('ERC20');

contract('ERC20', function ([sender, receiver]) {
  beforeEach(async function () {
    this.erc20 = await;
    this.value = new BN(1); // The bundled BN library is the same one truffle and web3 use under the hood

  it('reverts when transferring tokens to the zero address', async function () {
    // Conditions that trigger a require statement can be precisely tested
    await expectRevert(
      this.erc20.transfer(constants.ZERO_ADDRESS, this.value, { from: sender }),
      'ERC20: transfer to the zero address',

  it('emits a Transfer event on successful transfers', async function () {
    const receipt = await this.erc20.transfer(receiver, this.value, { from: sender });

    // Event assertions can verify that the arguments are the expected ones
    expectEvent(receipt, 'Transfer', {
      from: sender,
      to: receiver,
      value: this.value,

  it('updates balances on successful transfers', async function () {
    this.erc20.transfer(receiver, this.value, { from: sender });

    // If Chai is installed, big number assertions are automatically available thanks to chai-bn
    assert(await balance(receiver));


This library supports both web3 and truffle contract instances. Where possible, helpers will automatically detect what you're using and work with both. For details about each helper see their documentation entries below.


In a truffle environment, the web3 provider will be pulled from truffle's global web3 instance. Otherwise, it defaults to http://localhost:8545. You can override this behavior and configure your own via the provider key:

require('@openzeppelin/test-helpers/configure')({ provider: 'http://localhost:8080' });


The singletons helper returns contract objects, which have multiple values that can be configured:

  • abstraction: the underlying contract abstraction type, 'web3' for web3-eth-contract and 'truffle' for @truffle/contract instances. Defaults to 'web3' unless a truffle environment is detected.
  • defaultGas: how much gas to allocate when a transaction's gas field is not specified. Defaults to 200k.
  • defaultSender: the sender address to use when a transaction's from field is not specified. No default.

While automatic detection and defaults should cover most use cases, all values can be manually supplied:

require('@openzeppelin/test-helpers/configure')({ singletons: { abstraction: 'web3', defaultGas: 6e6, defaultSender: '0x5a0b5...' } });

About truffle migrations

Automatic truffle environment detection does not work inside truffle migrations, so the helpers must be manually configured.

require('@openzeppelin/test-helpers/configure')({ provider: web3.currentProvider, singletons: { abstraction: 'truffle' } });


This documentation is a work in progress: if in doubt, head over to the tests directory to see examples of how each helper can be used.

All returned numbers are of type BN.


Helpers to inspect Ether balances of a specific account.

All of these functions return BN instances, with balances in 'wei' by default.

balance current

async balance.current(account, unit = 'wei')

Returns the current balance of an account.

const balance = await balance.current(account)
// same as new BN(web3.eth.getBalance(account))

const balanceEth = await balance.current(account, 'ether')
// same as new BN(web3.utils.fromWei(await web3.eth.getBalance(account), 'ether'))

balance tracker

Allows you to keep track of the changes in an account's Ether balance.

async balance.tracker(account, unit = 'wei')

Creates an instance of a balance tracker.

const tracker = await balance.tracker(account)
async tracker.get(unit = tracker.unit)

Returns the current balance of an account.

const tracker = await balance.tracker(account) // instantiation
const currentBalance = await tracker.get() // returns the current balance of account
async = tracker.unit)

Returns the change in the balance since the last time it was checked (with either get() or delta()).

const tracker = await balance.tracker(receiver, 'ether')
send.ether(sender, receiver, ether('10'))

Or using get():

const tracker = await balance.tracker(account) // instantiation
const currentBalance = await tracker.get() // returns the current balance of account

A tracker can also return all balances and deltas in a specific unit:

const tracker = await balance.tracker(account, 'gwei');
const balanceGwei = tracker.get(); // in gigawei
const balanceEther = tracker.get('ether'); // in ether


A bn.js object. Use new BN(number) to create BN instances.


A collection of useful constants.


The initial value of a type address variable, i.e., address(0) in Solidity.


The maximum unsigned integer 2^256 - 1 represented in BN.


The maximum signed integer 2^255 - 1 represented in BN.


The minimum signed integer -2^255 represented in BN.


Converts a value in Ether to wei.

expectEvent (receipt, eventName, eventArgs = {})

Asserts the logs in receipt contain an entry for an event with name eventName, for which all entries in eventArgs match. receipt is the object returned by either a web3 Contract or a truffle-contract call.

const web3Receipt = await'bar').send();
expectEvent(web3Receipt, 'Foo', { value: 'bar' });

const truffleReceipt = await'bar');
expectEvent(truffleReceipt, 'Foo', { value: 'bar' });

async inTransaction (txHash, emitter, eventName, eventArgs = {})

Same as expectEvent, but for events emitted in an arbitrary transaction (of hash txHash), by an arbitrary contract (emitter, the contract instance), even if it was indirectly called (i.e. if it was called by another smart contract and not an externally owned account).

// With web3 contracts
const contract = await MyContract.deploy().send();
const { transactionHash } = await'bar').send();
await expectEvent.inTransaction(transactionHash, contract, 'Foo', { value: 'bar' });

// With truffle contracts
const contract = await;
const { txHash } = await'bar');
await expectEvent.inTransaction(txHash, contract, 'Foo', { value: 'bar' });

async function inConstruction (emitter, eventName, eventArgs = {})

Same as inTransaction, but for events emitted during the construction of emitter. Note that this is currently only supported for truffle contracts.


Collection of assertions for transaction errors (similar to chai's throw).

async expectRevert (promise, message)

This helper asserts that promise was rejected due to a reverted transaction, and it will check that the revert reason includes message. Use expectRevert.unspecified when the revert reason is unknown. For example:

contract Owned {
    address private _owner;

    constructor () {
        _owner = msg.sender;

    function doOwnerOperation() public view {
        require(msg.sender == _owner, "Unauthorized");

Can be tested as follows:

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

const Owned = artifacts.require('Owned');

contract('Owned', ([owner, other]) => {
  beforeEach(async function () {
    this.owned =;

  describe('doOwnerOperation', function() {
    it('Fails when called by a non-owner account', async function () {
      await expectRevert(this.owned.doOwnerOperation({ from: other }), "Unauthorized");

async expectRevert.unspecified (promise)

This helper asserts that promise was rejected due to a reverted transaction caused by a require or revert statement.

async expectRevert.assertion (promise)

This helper asserts that promise was rejected due to a reverted transaction caused by an assert statement or an invalid opcode.

async expectRevert.outOfGas (promise)

This helper asserts that promise was rejected due to a transaction running out of gas.


ERC165 (interfaces = [])

Calculates the ERC165 interface ID of a contract, given a series of function signatures.

ERC1820 (name)

Calculates the ERC1820 interface hash of a contract, given its name.


async send.ether (from, to, value)

Sends value Ether from from to to.

async function send.transaction (target, name, argsTypes, argsValues, opts = {})

Sends a transaction to contract target, calling method name with argValues, which are of type argTypes (as per the method's signature).


async singletons.ERC1820Registry (funder)

Returns an instance of an ERC1820Registry deployed as per the specification (i.e. the registry is located at the canonical address). This can be called multiple times to retrieve the same instance.


async time.advanceBlock ()

Forces a block to be mined, incrementing the block height.

async time.advanceBlockTo (target)

Forces blocks to be mined until the the target block height is reached.

Note: Using this function to advance too many blocks can really slow down your tests. Keep its use to a minimum.

async time.latest ()

Returns the timestamp of the latest mined block. Should be coupled with advanceBlock to retrieve the current blockchain time.

async time.latestBlock ()

Returns the latest mined block number.

async time.increase (duration)

Increases the time of the blockchain by duration (in seconds), and mines a new block with that timestamp.

async time.increaseTo (target)

Same as increase, but a target time is specified instead of a duration.


Helpers to convert different time units to seconds. Available helpers are: seconds, minutes, hours, days, weeks and years.

await time.increase(time.duration.years(2));



You can’t perform that action at this time.