OpenZeppelin Test Helpers

Assertion library for Ethereum smart contract testing. Make sure your contracts behave as expected.
Overview
Installation
npm install --save-dev @openzeppelin/test-helpers
Hardhat
Install web3
and the hardhat-web3
plugin.
npm install --save-dev @nomiclabs/hardhat-web3 web3
Remember to include the plugin in your configuration as explained in the installation instructions.
Usage
Import @openzeppelin/test-helpers
in your test files to access the different assertions and utilities.
const {
BN,
constants,
expectEvent,
expectRevert,
} = require('@openzeppelin/test-helpers');
const ERC20 = artifacts.require('ERC20');
contract('ERC20', function ([sender, receiver]) {
beforeEach(async function () {
this.value = new BN(1);
this.erc20 = await ERC20.new();
});
it('reverts when transferring tokens to the zero address', async function () {
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 }
);
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 });
expect(await this.erc20.balanceOf(receiver))
.to.be.bignumber.equal(this.value);
});
});
Learn More
License
MIT