Truffle Tests – How to Solve Timeout in ‘Before All’ Hook

truffletruffle-configtruffle-test

I'm trying to write my first tests for truffle, but I run in a timeout each time I want to execute the tests.

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

Contract deployment with truffle deploy to ganache and a parity node works fine, but when I run truffle test, truffle hangs for 2min and then runs into a timeout:

C:\Development\solidity>truffle test
Using network 'development'.

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

  Contract: MyContract
    1) "before all" hook: prepare suite

  0 passing (2m)
  1 failing

  1) Contract: MyContract
       "before all" hook: prepare suite:
     Error: Timeout of 120000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Development\solidity\test\first-tests.js)

truffle test run fine without any tests (0 passing (1ms)), but as soon as I create a test-file with a contract() as described in the docs truffle does not respond for 2 minutes, then runs into the timeout error shown above.

My test file in test/my_test.js:

const MyContract = artifacts.require("MyContract")

contract("MyContract", async () => {
  it("count should be zero after deployment", async () => {
    MyContract.deployed().then(instance => {
      assert.equal(0, 0)
    })
  })
})

My truffle-config.js:

const HDWalletProvider = require("truffle-hdwallet-provider")

module.exports = {
  networks: {
    development: {
      provider: new HDWalletProvider('aa bb cc .. xx yy zz', 'http://127.0.0.1:7545'),
      network_id: 5777,
    },
  },

  mocha: { enableTimeouts: false },
  compilers: { solc: {} },
}

Am I missing some important configuration used for running the tests? or why does it run into a timeout? If there's another error causing this timeout, is there a way to debug this in more detail to get a better error description?

The post I found here addresses problems with contract deployments that took too long, but my contract is deploy within 10 seconds, so that doesn't seem to be the same problem here.

Things I tried:

  • Updating my truffle-config.js to include mocha: { enableTimeouts: false } as suggested didn't change anything, still same behaviour.
  • Create an empty project and start from scratch. Same Behaviour.
    ( I did truffle init, write contract, add to migrations, add truffle-config, add test-file, run truffle build|deploy|test.)

Any hint on what to do to get my very basic test working would be highly appreciated.

Best Answer

There is an issue in a way you use HDWalletProvider when configuring development network in truffle-config.js.

You should add it like:

provider: () => new HDWalletProvider('your mnemonic', 'http://127.0.0.1:7545')

For me, it helped. Additional info can be found here.

Related Topic