Truffle: Testing does not reset state

Created on 21 Dec 2017  路  3Comments  路  Source: trufflesuite/truffle

  • [x] I've asked for help in the Truffle Gitter before filing this issue.

Issue

Testing does not reset state

Steps to Reproduce

Test.sol

contract Test {
    uint[] a;
    function length() public view returns (uint) {
        return a.length;
    }

    function add(uint val) public {
        a.push(val);
    }
}

test.js

var Test = artifacts.require("Test");
contract('Test', function(accounts) {
    it("test 0", async function() {
        var t = await Test.deployed();
        var l = await t.length.call();
        assert.equal(l.toNumber(), 0, 'array should be empty');
    });

    it("test 1", async function() {
        var t = await Test.deployed();
        await t.add(1);
    });

    it("test 2", async function() {
        var t = await Test.deployed();
        var l = await t.length.call();
        assert.equal(l.toNumber(), 0, 'array should be empty');
    });
});
  • truffle dev
  • test

Expected Behavior

all tests should pass

Actual Results

Contract: Test test 2:
     AssertionError: array should be empty: expected 1 to equal 0

Environment

  • Operating System: Ubuntu 16.04
  • Truffle version: v4.0.4
  • Ethereum client: None
  • node version: v8.9.1
  • npm version: 5.5.1

Most helpful comment

This might want to be reconsidered @cgewecke . For instance, when using the evm_increaseTime feature, the timestamp of all subsequent blocks will be increased by the previous value, which makes testing timestamp based functionalities quite cumbersome. This is one example, but I am sure there are others. Ideally, one could have the option of starting from scratch if desired, even it less efficient.

All 3 comments

Truffle tests dont reset state within the same file, if this is an issue for you, you can use a 'beforeEach' hook and specify a function which creates a new instance of your contract using 'MyContract.new([param1],[param2]...)'

Hi @timothywangdev! @JackPickering is correct. You can see some nice examples of the beforeEach pattern he's describing at the Cryptokitties contract repository here.

By design Truffle resets the state or "reverts" between each contract suite - e.g. each block of tests wrapped in Truffle's contract test abstraction.

Closing because this is intended behavior.

This might want to be reconsidered @cgewecke . For instance, when using the evm_increaseTime feature, the timestamp of all subsequent blocks will be increased by the previous value, which makes testing timestamp based functionalities quite cumbersome. This is one example, but I am sure there are others. Ideally, one could have the option of starting from scratch if desired, even it less efficient.

Was this page helpful?
0 / 5 - 0 ratings