JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17733
  • Score
    100M100P100Q10638F
  • License MIT

JavaScript testing helpers for Ethereum smart contract development.

Package Exports

  • @openzeppelin/test-helpers
  • @openzeppelin/test-helpers/configure

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@openzeppelin/test-helpers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

OpenZeppelin Test Helpers

NPM Package Build Status

Assertion library for Ethereum smart contract testing. Make sure your contracts behave as expected.

Test Helpers integrates seamlessly with OpenZeppelin Test Environment, but it also supports Truffle tests and regular web3 workflows.

Overview

Installation

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

Usage

Import @openzeppelin/test-helpers in your test files to access the different assertions and utilities.

Note: The following snippet uses OpenZeppelin Test Environment: a Truffle-based setup would work the same way.

const { accounts, contract } = require('@openzeppelin/test-environment');

const {
  BN,           // Big Number support
  constants,    // Common constants, like the zero address and largest integers
  expectEvent,  // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
} = require('@openzeppelin/test-helpers');

const ERC20 = contract.fromArtifacts('ERC20');

describe('ERC20', function () {
  const [sender, receiver] =  accounts;

  beforeEach(async function () {
    // The bundled BN library is the same one web3 uses under the hood
    this.value = new BN(1);

    this.erc20 = await ERC20.new();
  });

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

    // BN assertions are automatically available via chai-bn (if using Chai)
    expect(await this.erc20.balanceOf(receiver))
      .to.be.bignumber.equal(this.value);
  });
});

Learn More

License

MIT