When running Truffle JavaScript tests using Ganache or ganache-cli, there is nothing to do: Truffle resets the blockchain state between each test. The initial snapshot is done just after loading the contracts.
More precisely, the state is reset before each contract
(Truffle replacement for mocha's describe
). For example:
var MyContract = artifacts.require("./MyContract.sol");
contract('MyContract - A', function(accounts) {
before("Setup", async function () {
// Suppose balance of accounts[0] == 100ETH
});
it("should do Alpha", async function() {
// Balance of accounts[0] == 100ETH
decreaseAcount0By1ETH();
// Balance of accounts[0] == 99ETH
});
it("should do Beta", async function() {
// Balance of accounts[0] == 99ETH,
// because state is *not* reset between 'it'
});
contract('MyContract - B', function(accounts) {
it("should do Alpha", async function() {
// Balance of accounts[0] == 100ETH again,
// because state is reset between 'contract'
});
});
This is something you can track in Ganache logs. Look for evm_snapshot
and evm_revert
.