Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test error with mocha and chai Timeout of 2000ms exceeded. For async tests and hooks #2868

Closed
shreyasjain opened this issue Jun 3, 2019 · 8 comments

Comments

@shreyasjain
Copy link

shreyasjain commented Jun 3, 2019

My code:


const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface,bytecode} = require('../compile');

let accounts;
let inbox;

beforeEach( async() => {
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(JSON.parse(interface))
  .deploy({data: bytecode,arguments:['Hi There !'] })
  .send({from: accounts[0], gas:'1000000'});
});

describe("inbox", () => {
it('deploys a contract', () => {
    console.log(inbox);
 })
})

The error while running it using npm run test:

`shreyas@shreyas-Inspiron-3520:~/inbox$ npm run test

inbox@1.0.0-beta.55 test /home/shreyas/inbox mocha

inbox 1) "before each" hook for "deploys a contract"

0 passing (2s) 1 failing

  1. "before each" hook for "deploys a contract": Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7)

I searched for solution in so many similar questions, but none of them helped me. Please help. I already spent hours on this issue.

Important point:
The myContract.deploy() is working fine, but myContract.deploy().send() is not. I think there is something wrong with the send() method. You can check it by downloading the gist from the link below:

Git Repository of my project: https://github.com/shreyasjain/inbox

@nivida
Copy link
Contributor

nivida commented Jun 3, 2019

Did you configure the transaction confirmation blocks? (https://web3js.readthedocs.io/en/1.0/web3.html#transactionconfirmationblocks)

@shreyasjain

This comment has been minimized.

@shreyasjain
Copy link
Author

I tried it like this:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface,bytecode} = require('../compile');

let a = web3.transactionConfirmationBlocks
let b = web3.eth.transactionConfirmationBlocks
let c = web3.shh.transactionConfirmationBlocks

console.log(a);
console.log(b);
console.log(c);

let accounts;
let inbox;

beforeEach(async() => {

  accounts = await web3.eth.getAccounts();

   inbox = await  web3.eth.Contract(JSON.parse(interface))
            .deploy({data: bytecode, arguments: ['Hi'] })
            .send({from: accounts[1], gas:1000000});


});

describe("inbox", () => {
  it('deploys a contract', () => {
    console.log(inbox);
  })
})

Was it the right way to use it?
The values of a,b and c are 24 in this case.

@nivida
Copy link
Contributor

nivida commented Jun 3, 2019

Please read the documentation and ask your question on stackoverflow. We also have a gitter channel with a lot of active developers.

@nivida nivida closed this as completed Jun 3, 2019
@starcommandeer
Copy link

hi @shreyasjain did you find out what the problem was?

@starcommandeer
Copy link

I am having the same issue

@r4881t
Copy link

r4881t commented Jun 18, 2021

I also faced this issue and it seems like running mocha with timeout fixes this. Something like mocha server/**/*.test.js --timeout 10000

@MMukkarram
Copy link

MMukkarram commented Feb 17, 2023

I had similar issue. i just change the const {interface, bytecode} to const {abi, bytecode} because we sending api in comile.JS file.
======================= Code ======================================
Inbox.test.js

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {abi, bytecode}= require('../compile');
let accounts;
let inbox;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(abi)
.deploy({ data: bytecode, arguments: ['Hi.Buddy']})
.send({from: accounts[0], gas:'1000000'});
});
describe('Inbox Testing', () => {
it('Deploys a contract', () => {
console.log(inbox);
});
});

compile.js

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol");
const source = fs.readFileSync(inboxPath, "utf8");
const input = {
language: "Solidity",
sources: {
"Inbox.sol": {
content: source
}
},
settings: {
metadata: {
useLiteralContent: true
},
outputSelection: {
"": {
"
": ["*"]
}
}
}
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
var outputContracts = output.contracts['Inbox.sol']['Inbox'];

console.log(output);

module.exports.abi = outputContracts.abi;
module.exports.bytecode = outputContracts.evm.bytecode.object;

Inbox.sol

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;

contract Inbox {
string public message;
constructor(string memory initialMessage) {
message = initialMessage;
}
function setMessage(string memory newMessage) public {
message = newMessage;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants