3

I want to write some tests using mocha and ganache-cli for my contract. I tried to deploy a compiled contract like so:

//ZombieFactory.test.js

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledZombieFactory = require('../ethereum/build/ZombieFactory.json');

let accounts;
let factory;

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

    factory = await new web3.eth.Contract(compiledZombieFactory.abi)
        .deploy({data: '0x' + compiledZombieFactory.evm.bytecode.object})
        .send({from: accounts[0], gas: '3000000'});

});

describe('ZombieFactory', () => {
    it('deploys the zombie factory', () => {
        assert.ok(factory.options.address);
    })
});

When I run this using npm run test, I get the following error:

ZombieFactory
    1) "before each" hook for "deploys the zombie factory"


  0 passing (2s)
  1 failing

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

I did some research and I suppose this error occurs because the code fails to deploy the contract successfully. I would like to know if this issue is due to the deploy code or something else.

2

ganache-cli is a bit to deprecated at this point. It is recommended to use truffle develop which replaces ganache-cli and supports latest Solidity versions.

You can find out a lot more from this awesome guy:

// Truffle Toolkit + Truffle Scripts
https://www.youtube.com/watch?v=X6DzzeoRTS0&t=623s

// Actual Testing
https://www.youtube.com/watch?v=v90hvMEjf_Q

Good Luck !

| improve this answer | |
  • 1
    I realized that working with latest versions of all packages without using a suite like truffle isn't a good idea. I started using truffle and everything works great now. Thanks buddy! – roheat Apr 3 '19 at 11:43
1

Found the solution!

web3@1.0.0-beta.51 is not that stable. Use the most stable release (as of now as version 1.0 is still in beta).

So basically: Uninstall npm uninstall web3@1.0.0-beta.51

Install npm install --save web3@1.0.0-beta.37

The same code runs without throwing any errors :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.