Open Philogy opened 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?
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.
Hi @MrClottom ,
Unfortunately, I wasn’t able to reproduce this issue. I tried the following:
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;
}
}
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
> 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)
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
See https://github.com/OpenZeppelin/openzeppelin-test-environment/issues/48 for a potential cause of the error.
I'm not running with ganache, what version is your @openzeppelin/test-environment
package? What are the contents of your truffle-config.js
?
Hi @MrClottom ,
I am using OpenZeppelin Test Environment 0.1.6. My truffle-config.js
and package.json
are below:
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: "*"
// }
//}
//
};
{
"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"
}
}
@openzeppelin-test-environment version: 0.1.6 truffle version: 5.1.13 Steps to reproduce:
mkdir test-project; cd test-project
truffle unbox metacoin
truffle test
=> tests succeedsnpm i --save-dev @openzeppelin/test-environment mocha chai
4.2. Create package.json and add test script 4.3. Replace necessary valuesnpm test
=> Timeout errorAdditional notes: I tried running
--timeout 30000
but the tests still timedout. I had another project running with0.15
which worked great sad that0.16
will have to resort to normal truffle testing. Would love any help, thanks.