OpenZeppelin / openzeppelin-test-environment

[Not actively maintained] One-line setup for blazing-fast smart contracts tests
https://docs.openzeppelin.com/test-environment
MIT License
90 stars 39 forks source link

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. #144

Open Philogy opened 3 years ago

Philogy commented 3 years ago

@openzeppelin-test-environment version: 0.1.6 truffle version: 5.1.13 Steps to reproduce:

  1. mkdir test-project; cd test-project
  2. truffle unbox metacoin
  3. truffle test => tests succeeds
  4. Follow migrate instructions (https://docs.openzeppelin.com/test-environment/0.1/migrating-from-truffle): 4.1. npm i --save-dev @openzeppelin/test-environment mocha chai 4.2. Create package.json and add test script 4.3. Replace necessary values
  5. npm test => Timeout error

Additional notes: I tried running --timeout 30000 but the tests still timedout. I had another project running with 0.15 which worked great sad that 0.16 will have to resort to normal truffle testing. Would love any help, thanks.

frangio commented 3 years ago

Hi @MrClottom, I was not able to follow your reproduction steps. MetaCoin Truffle Box contains a migration, which does not work with Test Environment, and it's also necessary to link the contract to ConvertLib.

Can you provide complete reproduction steps?

Philogy commented 3 years ago

Removing the meta coin contracts and writing some from scratch which don't require a migration or library still leads to the Error: Timeout of 2000ms exceeded error.

abcoathup commented 3 years ago

Hi @MrClottom ,

Unfortunately, I wasn’t able to reproduce this issue. I tried the following:

MetaCoin.sol

I inlined ConvertLib so we don't need to link the contract

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.25 <0.7.0;

//import "./ConvertLib.sol";

// This is just a simple example of a coin-like contract.
// It is not standards compatible and cannot be expected to talk to other
// coin/token contracts. If you want to create a standards-compliant
// token, see: https://github.com/ConsenSys/Tokens. Cheers!

contract MetaCoin {
    mapping (address => uint) balances;

    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    constructor() public {
        balances[tx.origin] = 10000;
    }

    function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
        if (balances[msg.sender] < amount) return false;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Transfer(msg.sender, receiver, amount);
        return true;
    }

    function getBalanceInEth(address addr) public view returns(uint){
        //return ConvertLib.convert(getBalance(addr),2);
        return convert(getBalance(addr),2);
    }

    function getBalance(address addr) public view returns(uint) {
        return balances[addr];
    }

    function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount)
    {
        return amount * conversionRate;
    }
}

metacoin.js

I updated the tests to use expect and deployed a new contract for each test

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');

const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

const { expect } = require('chai');

const MetaCoin = contract.fromArtifact("MetaCoin");

describe('MetaCoin', () => {
  it('should put 10000 MetaCoin in the first account', async () => {
    const metaCoinInstance = await MetaCoin.new({from: accounts[0]});
    const balance = await metaCoinInstance.getBalance.call(accounts[0]);

    expect(balance.valueOf()).to.be.bignumber.equal(new BN('10000'));
  });
  it('should call a function that depends on a linked library', async () => {
    const metaCoinInstance = await MetaCoin.new({from: accounts[0]});
    const metaCoinBalance = (await metaCoinInstance.getBalance.call(accounts[0])).toNumber();
    const metaCoinEthBalance = (await metaCoinInstance.getBalanceInEth.call(accounts[0])).toNumber();

    expect(metaCoinEthBalance).to.be.equal(2 * metaCoinBalance);
  });
  it('should send coin correctly', async () => {
    const metaCoinInstance = await MetaCoin.new({from: accounts[0]});

    // Setup 2 accounts.
    const accountOne = accounts[0];
    const accountTwo = accounts[1];

    // Get initial balances of first and second account.
    const accountOneStartingBalance = (await metaCoinInstance.getBalance.call(accountOne)).toNumber();
    const accountTwoStartingBalance = (await metaCoinInstance.getBalance.call(accountTwo)).toNumber();

    // Make transaction from first account to second.
    const amount = 10;
    await metaCoinInstance.sendCoin(accountTwo, amount, { from: accountOne });

    // Get balances of first and second account after the transactions.
    const accountOneEndingBalance = (await metaCoinInstance.getBalance.call(accountOne)).toNumber();
    const accountTwoEndingBalance = (await metaCoinInstance.getBalance.call(accountTwo)).toNumber();

    expect(accountOneEndingBalance).to.be.equal(accountOneStartingBalance - amount);
    expect(accountTwoEndingBalance).to.be.equal(accountTwoStartingBalance + amount);
  });
});

npm test

$ npm test

> test-project@1.0.0 test /home/abcoathup/projects/forum/test-project
> mocha --exit --recursive

Version 9 of Highlight.js has reached EOL and is no longer supported. Please upgrade to version 10.

  MetaCoin
    ✓ should put 10000 MetaCoin in the first account (397ms)
    ✓ should call a function that depends on a linked library (105ms)
    ✓ should send coin correctly (200ms)

  3 passing (711ms)

Environment

WSL2 on Windows 10

$ npx truffle version
Truffle v5.1.53 (core: 5.1.53)
Solidity v0.5.16 (solc-js)
Node v10.22.1
Web3.js v1.2.9
frangio commented 3 years ago

See https://github.com/OpenZeppelin/openzeppelin-test-environment/issues/48 for a potential cause of the error.

Philogy commented 3 years ago

I'm not running with ganache, what version is your @openzeppelin/test-environment package? What are the contents of your truffle-config.js ?

abcoathup commented 3 years ago

Hi @MrClottom ,

I am using OpenZeppelin Test Environment 0.1.6. My truffle-config.js and package.json are below:

truffle-config.js

My truffle-config.js is unmodified.

module.exports = {
  // Uncommenting the defaults below 
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks;
  // see <http://truffleframework.com/docs/advanced/configuration>
  // for more details on how to specify configuration options!
  //
  //networks: {
  //  development: {
  //    host: "127.0.0.1",
  //    port: 7545,
  //    network_id: "*"
  //  },
  //  test: {
  //    host: "127.0.0.1",
  //    port: 7545,
  //    network_id: "*"
  //  }
  //}
  //
};

package.json

{
  "name": "test-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha --exit --recursive"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "@openzeppelin/test-environment": "^0.1.6",
    "@openzeppelin/test-helpers": "^0.5.9",
    "chai": "^4.2.0",
    "mocha": "^8.2.1",
    "truffle": "^5.1.54"
  }
}