Web3.js: Unit test error with mocha and chai Timeout of 2000ms exceeded. For async tests and hooks

Created on 3 Jun 2019  路  6Comments  路  Source: ChainSafe/web3.js

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

[email protected] 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

more information needed

All 6 comments

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

I am new to this technology and currently learning Blockchain development.
I don't know how to do that. Please elaborate a little.

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.

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

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

I am having the same issue

Was this page helpful?
0 / 5 - 0 ratings