0
votes

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?

1

1 Answers

1
votes

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

  1. Installed [email protected] (Note: your dependencies can be very different from mine as you are using solidity ^0.4.17)

enter image description here

  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).

enter image description here