简体   繁体   中英

How to fix Timeout exceeded error for async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves

I am trying to deploy a simple contract using mocha and ganache, but I got this error: Acontract 1) "before each" hook for "Deploys a Contract"

0 passing (30s)
1 failing

1) "before each" hook for "Deploys a Contract":
Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure 
"done()" is called; if returning a Promise, ensure it resolves.

The contract code :

pragma solidity ^0.4.17;

contract Acontract {

string public message; // new variable

function Acontract(string initialMessage) public {
    message = initialMessage;
}

function setMessage(string newMessage) public {
    message = newMessage;
}


}

The test file 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 result;

beforeEach( async () => {
 accounts = await web3.eth.getAccounts();

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

describe('Acontract', ()=> {

  it('Deploys a Contract', async  ()=>{

console.log(result)

    });
});

How to solve this error, the code is simple, I tested getting accounts and it was ok, also deploy seems ok, when it comes to sending the code does not work! any suggestions?

I ran into a similar issue and fixed it like this: ( You may try number 2 first ).

  1. Installed web3@1.0.0-beta.37 ( Note: your dependencies can be very different from mine as you are using solidity ^0.4.17 )

在此处输入图片说明

  1. Added ganache provider in the code.
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
//Ganache Provider
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode} = require('../compile');

let accounts;
let result;

beforeEach( async () => {
    accounts = await web3.eth.getAccounts();

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

    //Set provider
    result.setProvider(provider);
});

describe('Acontract', ()=> {

    it('Deploys a Contract', async  ()=>{

        console.log(result)

    });
});
  1. "Contract" object structure is changed a bit in this version. So, I had to update compile.js export a bit as well ( You may not need it with your version of solidity compiler ).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM