I have the following javascript test for a simple ERC 20 token. Which on the call return token.balanceOf.call(accounts[2]); will fetch some old data and fail from a previous test transaction writting similar, but in another contract() scope on the same contract. Using TestRpc

contract('ERC Allowance', function (accounts) {

    it("should be able to lend money and that user spend it", function () {
        var token;

        return Token.deployed().then(function (instance) {
            token= instance;
            return instance.approve(accounts[1], 5555, {from: accounts[0]});
            .then(function (approveTransaction) {
                return chftoken .allowance.call(accounts[0], accounts[1]);
            .then(function (allowance) {
                assert.equal(allowance, 5555, "allowance should be increased");
                return token.transferFrom.call(accounts[0], accounts[2], 55555, {from: accounts[1]});
            .then(function (failedTransaction) {
                return token.balanceOf.call(accounts[2]);
            .then(function (balance) {
                assert.equal(balance.valueOf(), 0, "transfer should not go through");
                return token.transferFrom.call(accounts[0], accounts[9], 10000, {from: accounts[1]});
            .then(function (transactionCompleted) {
                return token.balanceOf.call(accounts[2])
            .then(function (balance) {
                assert.equal(balance, 5555, "the balance should be transferred")

2 Answers 2


To reset the state, you have to tell the test suite to deploy a new instance of the contract before it starts.

You can tell it to deploy a new contract before each "it" with beforeEach(). Every test will start with an initialized state (and a new contract .deployed() address).

contract( ...

  var contract;

  beforeEach(function() {
     return Contract.new()
     .then(function(instance) {
        contract = instance;

  it("should ...

Hope I didn't flub the syntax. Truffle 3.x style.

  • I could not use .new() with my upgradeable contract (OpenZeppelin), probably because it's not supposed to as there are more than one contract involved. I had to use the deployProxy just like it's done in the migration file. Doc: docs.openzeppelin.com/upgrades-plugins/1.x/…
    – nyg
    Commented Apr 9, 2021 at 16:43
  • Right. You would have to adapt the principle by deploying a new Proxy in that case. Commented Apr 9, 2021 at 17:48

Following @rob's answer, you can also use the async version which might look quite clean to your appeal:

let contract;

beforeEach('should setup the contract instance', async () => {
  contract = await Contract.new();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.