etherlime test
etherlime test
Syntax
etherlime test [path] [timeout] [skip-compilation] [gas-report] [runs] [solcVersion]
[output] [port]
Parameters:
path
- [Optional] By specifyingpath
you can set a path to aselected directory or you can set the path directly to the
javascript file which contains your tests. By default the
path
points to
./test
.timeout
- [Optional] This parameter defines the test timeout inmilliseconds. Defaults to 2000 ms.
skip-compilation
- [Optional] This parameter controls wether acompilation will be ran before the tests are started. Default:
false.
gas-report
- [Optional] Enables Gas reporting future that willshow Gas Usage after each test. Default: false.
runs
- [Optional] By specifyingruns
between 1 and 999 youenabled the optimizer and set how many times the optimizer will be
run. By default the optimizer is not enabled.
solcVersion
- [Optional] By specifyingsolcVersion
you canset the version of the solc which will be used for compiling the
smart contracts. By default it use the solc version from the
node_modules folder.
output
- [Optional] Defines the way that the logs are shown.Choices:
none
- silences the output of logs,normal
- seeverbose logs in the console and
structured
- structured output ina file meant for inter program communication.
port
- [Optional] The port that the etherlime ganache is runing.Used for wiring up the default accounts correctly. Defaults to 8545
Global Objects
We've augmented the test runner with the following things you can use:
In your unit tests you can use the global
accounts
object. Itcontains the secretKey (private key) and instance of ethers.Wallet
of the account.
The assert object has function:
assert.revert(promiseOfFailingTransaction)
for testing reverting transactionsassert.revertWith(promiseOfFailingTransaction, expectedRevertMessage)
for testing reverting transaction with specific revert messageassert.notRevert(promiseOfNotFailingTransaction)
for testing transaction is executed successfullyassert.isAddress(value)
for testing a value is a proper addressassert.isPrivateKey(value)
for testing a value is a proper private keyassert.isHash(value)
for testing a value is a proper hexassert.emit(function, eventName)
for testing an event is emitted after function executionassert.emitWithArgs(function, eventName, [args])
for testing an event is emitted with certain arguments after function executionassert.balanceChanged(function, account, value)
for testing the balance of an account has been changed after function executionassert.balancesChanged(function, [accounts], [values])
for testing the balances of multiple accounts has been changed after function execution
Available Utils
On your disposal there is a global available utils object. Here are the methods it exposes:
utils.timeTravel(provider, seconds)
method allowing etherlimeganache to move
seconds
ahead. You need to pass your providerfrom the EtherlimeGanacheDeployer
utils.setTimeTo(provider, timestamp)
method allowing etherlimeganache to move to the desired
timestamp
ahead. You need to passyour provider from the EtherlimeGanacheDeployer
utils.mineBlock(provider)
method telling the etherlime ganacheto mine the next block. You need to pass your provider from the
EtherlimeGanacheDeployer
utils.snapshot(provider)
snapshot the state of the blockchain at the current block. Returns the integer id of the snapshot created
utils.revertState(provider, snapshotID)
revert the state of the blockchain to a previous snapshot.If no snapshot id is passed it will revert to the latest snapshot. Returns
true
.
utils.hasEvent(receipt, contract, eventName)
allowing the userto check if the desired event was broadcasted in the transaction
receipt. You need to pass the Transaction receipt, the contract
that emits it and the name of the Event.
utils.parseLogs(receipt, contract, eventName)
allowing the userget parsed events from a transaction receipt. You need to pass the
Transaction receipt, the contract that emits it and the name of
the Event. Always returns an event.
Examples
General Example
const etherlime = require('etherlime-lib');
const Billboard = require('../build/Billboard.json');
describe('Example', () => {
let owner = accounts[3];
let deployer;
beforeEach(async () => {
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
});
it('should set correct owner', async () => {
const BillboardContract = await deployer.deploy(Billboard, {});
let _owner = await BillboardContract.owner();
assert.strictEqual(_owner, owner.signer.address,
'Initial contract owner does not match');
});
});
execute function from another account
const etherlime = require('etherlime-lib');
const ethers = require('ethers');
const Billboard = require('../build/Billboard.json');
describe('Example', () => {
let aliceAccount = accounts[3];
let deployer;
beforeEach(async () => {
deployer = new etherlime.EtherlimeGanacheDeployer(aliceAccount.secretKey);
const BillboardContract = await deployer.deploy(Billboard, {});
});
it('should execute function from another account', async () => {
let bobsAccount = accounts[4].signer;
const transaction = await BillboardContract
.from(bobsAccount /* Could be address or just index in accounts like 3 */)
.buy('Billboard slogan', { value: ONE_ETHER });
assert.equal(transaction.from, bobsAccount.address);
});
});
accounts
const Billboard = require('../build/Billboard.json');
const etherlime = require('etherlime-lib');
describe('Billboard', () => {
let owner = accounts[5];
it('should initialize contract with correct values', async () => {
const deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
const BillboardContract = await deployer.deploy(Billboard, {});
// Do something with the contract
});
});
assert.revert
it('should throw if throwing method is called', async () => {
await assert.revert(contract.throwingMethod());
});
assert.revertWith
it('should throw with specific revert message', async () => {
await assert.revertWith(contract.throwingMethod(), expectedRevertMessage);
});
assert.notRevert
it('should assert that function not revert and is executed successfully', async () => {
await assert.notRevert(contract.notThrowingMethod());
});
assert.isAddress
const etherlime = require('etherlime');
const Billboard = require('../build/Billboard.json');
describe('Example', () => {
let owner = accounts[3];
let deployer;
let BillboardContract;
beforeEach(async () => {
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
BillboardContract = await deployer.deploy(Billboard, {});
});
it('should be valid address', async () => {
assert.isAddress(BillboardContract.contractAddress, "The contract was not deployed");
})
});
assert.isPrivateKey
it('should be valid private key', async () => {
let aliceAccount = accounts[3];
assert.isPrivateKey(aliceAccount.secretKey);
});
Check if the desired event was broadcasted in the transaction receipt
const etherlime = require('etherlime-lib');
const Billboard = require('../build/Billboard.json');
const assert = require('chai').assert;
describe('Billboard', () => {
let owner = accounts[5];
it('should emit event', async () => {
const deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
const BillboardContract = await deployer.deploy(Billboard, {});
const expectedEvent = 'LogBillboardBought';
await assert.emit(BillboardContract.buy('Billboard slogan', { value: 10000 }), expectedEvent)
});
});
Check if the desired event was broadcasted with specific arguments
it('should emit event with certain arguments', async () => {
const expectedEvent = 'LogBillboardBought';
const expectedArgs = 'Billboard slogan', 1000];
await assert.emitWithArgs(BillboardContract.buy('Billboard slogan', { value: 10000 }), expectedEvent, expectedArgs)
})
Check if a balance was changed on ethers sent
it('should change balance on ethers sent', async () => {
let bobsAccount = accounts[4].signer
await assert.balanceChanged(bobsAccount.sendTransaction({
to: aliceAccount.signer.address,
value: 200
}), bobsAccount, '-200')
})
Check if multiple balances changed on ethers sent
it('should change multiple balances on ethers sent', async () => {
let sender = accounts[1].signer;
let receiver = accounts[2].signer;
await assert.balancesChanged(sender.sendTransaction({
to: receiver.address,
value: 200
}), [sender, receiver], ['-200', 200])
})
Time travel and snapshot
const etherlime = require('etherlime');
const Billboard = require('../build/Billboard.json');
describe('Example', () => {
let owner = accounts[3];
let deployer;
let BillboardContract;
beforeEach(async () => {
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
BillboardContract = await deployer.deploy(Billboard, {});
});
it('should do something in the future', async () => {
let seconds = 600000;
await utils.timeTravel(deployer.provider, seconds);
// Do what is needed to be done in the future
})
it('should snapshot the current state', async () => {
let snapshotID = await utils.snapshot(deployer.provider);
// Additional logic comes here
})
it('should revert the state to a previous snapshot', async () => {
await utils.revertState(deployer.provider, snapshotID);
// Add before or after the reversion the logic you need
});
Last updated
Was this helpful?