Etherlime
Search…
etherlime test

etherlime test

Syntax

1
etherlime test [path] [timeout] [skip-compilation] [gas-report] [runs] [solcVersion]
2
[output] [port]
Copied!
Parameters:
  • path - [Optional] By specifying path you can set a path to a
    selected 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 in
    milliseconds. Defaults to 2000 ms.
  • skip-compilation - [Optional] This parameter controls wether a
    compilation will be ran before the tests are started. Default:
    false.
  • gas-report - [Optional] Enables Gas reporting future that will
    show Gas Usage after each test. Default: false.
  • runs - [Optional] By specifying runs between 1 and 999 you
    enabled the optimizer and set how many times the optimizer will be
    run. By default the optimizer is not enabled.
  • solcVersion - [Optional] By specifying solcVersion you can
    set 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 - see
    verbose logs in the console and structured - structured output in
    a 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. It
    contains the secretKey (private key) and instance of ethers.Wallet
    of the account.
  • The assert object has function:
    • assert.revert(promiseOfFailingTransaction) for testing reverting transactions
    • assert.revertWith(promiseOfFailingTransaction, expectedRevertMessage) for testing reverting transaction with specific revert message
    • assert.notRevert(promiseOfNotFailingTransaction) for testing transaction is executed successfully
    • assert.isAddress(value) for testing a value is a proper address
    • assert.isPrivateKey(value) for testing a value is a proper private key
    • assert.isHash(value) for testing a value is a proper hex
    • assert.emit(function, eventName) for testing an event is emitted after function execution
    • assert.emitWithArgs(function, eventName, [args]) for testing an event is emitted with certain arguments after function execution
    • assert.balanceChanged(function, account, value) for testing the balance of an account has been changed after function execution
    • assert.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 etherlime
    ganache to move seconds ahead. You need to pass your provider
    from the EtherlimeGanacheDeployer
  • utils.setTimeTo(provider, timestamp) method allowing etherlime
    ganache to move to the desired timestamp ahead. You need to pass
    your provider from the EtherlimeGanacheDeployer
  • utils.mineBlock(provider) method telling the etherlime ganache
    to 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 user
    to 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 user
    get 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

1
const etherlime = require('etherlime-lib');
2
const Billboard = require('../build/Billboard.json');
3
4
describe('Example', () => {
5
let owner = accounts[3];
6
let deployer;
7
8
beforeEach(async () => {
9
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
10
});
11
12
it('should set correct owner', async () => {
13
const BillboardContract = await deployer.deploy(Billboard, {});
14
let _owner = await BillboardContract.owner();
15
16
assert.strictEqual(_owner, owner.signer.address,
17
'Initial contract owner does not match');
18
});
19
});
Copied!

execute function from another account

1
const etherlime = require('etherlime-lib');
2
const ethers = require('ethers');
3
const Billboard = require('../build/Billboard.json');
4
5
describe('Example', () => {
6
let aliceAccount = accounts[3];
7
let deployer;
8
9
beforeEach(async () => {
10
deployer = new etherlime.EtherlimeGanacheDeployer(aliceAccount.secretKey);
11
const BillboardContract = await deployer.deploy(Billboard, {});
12
});
13
14
it('should execute function from another account', async () => {
15
let bobsAccount = accounts[4].signer;
16
const transaction = await BillboardContract
17
.from(bobsAccount /* Could be address or just index in accounts like 3 */)
18
.buy('Billboard slogan', { value: ONE_ETHER });
19
assert.equal(transaction.from, bobsAccount.address);
20
});
21
});
Copied!

accounts

1
const Billboard = require('../build/Billboard.json');
2
const etherlime = require('etherlime-lib');
3
4
describe('Billboard', () => {
5
let owner = accounts[5];
6
7
it('should initialize contract with correct values', async () => {
8
const deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
9
const BillboardContract = await deployer.deploy(Billboard, {});
10
11
// Do something with the contract
12
});
13
});
Copied!

assert.revert

1
it('should throw if throwing method is called', async () => {
2
await assert.revert(contract.throwingMethod());
3
});
Copied!

assert.revertWith

1
it('should throw with specific revert message', async () => {
2
await assert.revertWith(contract.throwingMethod(), expectedRevertMessage);
3
});
Copied!

assert.notRevert

1
it('should assert that function not revert and is executed successfully', async () => {
2
await assert.notRevert(contract.notThrowingMethod());
3
});
Copied!

assert.isAddress

1
const etherlime = require('etherlime');
2
const Billboard = require('../build/Billboard.json');
3
4
describe('Example', () => {
5
let owner = accounts[3];
6
let deployer;
7
let BillboardContract;
8
9
beforeEach(async () => {
10
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
11
BillboardContract = await deployer.deploy(Billboard, {});
12
});
13
14
it('should be valid address', async () => {
15
assert.isAddress(BillboardContract.contractAddress, "The contract was not deployed");
16
})
17
18
});
Copied!

assert.isPrivateKey

1
it('should be valid private key', async () => {
2
let aliceAccount = accounts[3];
3
assert.isPrivateKey(aliceAccount.secretKey);
4
});
Copied!

Check if the desired event was broadcasted in the transaction receipt

1
const etherlime = require('etherlime-lib');
2
const Billboard = require('../build/Billboard.json');
3
const assert = require('chai').assert;
4
5
describe('Billboard', () => {
6
let owner = accounts[5];
7
8
it('should emit event', async () => {
9
const deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
10
const BillboardContract = await deployer.deploy(Billboard, {});
11
12
const expectedEvent = 'LogBillboardBought';
13
await assert.emit(BillboardContract.buy('Billboard slogan', { value: 10000 }), expectedEvent)
14
});
15
});
Copied!

Check if the desired event was broadcasted with specific arguments

1
it('should emit event with certain arguments', async () => {
2
const expectedEvent = 'LogBillboardBought';
3
const expectedArgs = 'Billboard slogan', 1000];
4
await assert.emitWithArgs(BillboardContract.buy('Billboard slogan', { value: 10000 }), expectedEvent, expectedArgs)
5
})
Copied!

Check if a balance was changed on ethers sent

1
it('should change balance on ethers sent', async () => {
2
let bobsAccount = accounts[4].signer
3
await assert.balanceChanged(bobsAccount.sendTransaction({
4
to: aliceAccount.signer.address,
5
value: 200
6
}), bobsAccount, '-200')
7
})
Copied!

Check if multiple balances changed on ethers sent

1
it('should change multiple balances on ethers sent', async () => {
2
let sender = accounts[1].signer;
3
let receiver = accounts[2].signer;
4
await assert.balancesChanged(sender.sendTransaction({
5
to: receiver.address,
6
value: 200
7
}), [sender, receiver], ['-200', 200])
8
})
Copied!

Time travel and snapshot

1
const etherlime = require('etherlime');
2
const Billboard = require('../build/Billboard.json');
3
4
describe('Example', () => {
5
let owner = accounts[3];
6
let deployer;
7
let BillboardContract;
8
9
beforeEach(async () => {
10
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
11
BillboardContract = await deployer.deploy(Billboard, {});
12
});
13
14
it('should do something in the future', async () => {
15
let seconds = 600000;
16
await utils.timeTravel(deployer.provider, seconds);
17
18
// Do what is needed to be done in the future
19
})
20
21
it('should snapshot the current state', async () => {
22
let snapshotID = await utils.snapshot(deployer.provider);
23
24
// Additional logic comes here
25
})
26
27
it('should revert the state to a previous snapshot', async () => {
28
await utils.revertState(deployer.provider, snapshotID);
29
30
// Add before or after the reversion the logic you need
31
32
});
Copied!