API Reference

test-environment exposes a number of variables that are used to interact with the local testing blockchain it sets up. These are described in detail here:

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

accounts

accounts: string[]

An array of strings with the addresses of the accounts available for testing. By default, there are 10 unlocked accounts with 100 ETH each, but this can be configured.

const [ sender, receiver ] = accounts;

await myToken.transfer(receiver, 100, { from: sender });

defaultSender

defaultSender: string

A special account that is used by contracts created via contract when no account is specified for a transaction (i.e. there is no explicit from). This account is not included in accounts to prevent accidental bugs during testing: whenever you want an account to make an action (deploy a contract, transfer ownership, etc.) you should be explicit about the sender of the transaction:

const [ owner ] = accounts;

// The deployment will be made by 'defaultSender' (not 'owner'!), making it
// the contract's owner
const myContract = await Ownable.new();

// And the following test will fail
expect(await myContract.owner()).to.equal(owner);

contract

contract.fromArtifact: (contract: string) => any;
contract.fromABI: (abi: object, bytecode?: string | undefined) => any;

The contract object is in charge of creating contracts from compilation artifacts. It does this via two functions:

  • fromArtifact looks for a .json file in the local or a dependency’s build/contracts directory (equivalent to Truffle’s artifact.require).

  • fromABI receives an ABI object directly, useful when the full compilation artifacts are not available.

They both return instances of either @truffle/contract (by default) or web3-eth-contract, depending on your configuration.

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

const myToken = await ERC20.new(initialBalance, initialHolder);
Head over to the documentation for Contract Loader to learn more.

web3

A web3 instance, connected to the local testing blockchain. Useful to access utilities like web3.eth.sign, web3.eth.getTransaction, or web3.utils.sha3.

provider

A web3 provider, connected to the local testing blockchain. Used in more advanced scenarios, such as creation of custom web3 or ethers instances.

isHelpersConfigured

isHelpersConfigured: boolean

A boolean indicating if the OpenZeppelin Test Helpers library was autodetected and configured.