1

Following an aging tutorial, I'm trying to learn how to test a smart contract:

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

const web3 =  new Web3(ganache.provider());  
const { abi, bytecode} = require('../compile')



let accounts;
let simpleStorage;

beforeEach(async ()=>{
    // Get a list of all accounts
    accounts = await web3.eth.getAccounts();
    //Use one of those account to deply the contract
    simpleStorage = await new web3.eth.Contract(abi)
    .deploy({
        data: bytecode,
        arguments: ['Hi there!'] 
    })
    .send({
        from: accounts[0],
        gas: '1000000'
    });
});

describe('SimpleStorage', ()=> {
    it('deploy a contract', ()=>{
        console.log(simpleStorage);
    });
});

The compile.js is like:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxpath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxpath, 'UTF-8');

var input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));

 
exports.abi = output.contracts['Inbox.sol']['SimpleStorage'].abi;
exports.bytecode = output.contracts['Inbox.sol']['SimpleStorage'].evm.bytecode.object;

And the contract Inbox.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract SimpleStorage {
    uint256 public num;

    function set(uint256 _num) public {
        num = _num;
    }
    function get() public view returns (uint256) {
        return num;
    }
}

But I get this error:

0 passing (290ms)
  1 failing

  1) "before each" hook for "deploy a contract":
     Uncaught Error: Callback was already called.
      at /home/pc/Desktop/inbox/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/merkle-patricia-tree/node_modules/async/lib/async.js:43:1
      at d.<anonymous> (node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/merkle-patricia-tree/node_modules/async/lib/async.js:358:1)
      at d.emit (node:events:390:28)
      at d.destroy (node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/level-ws/level-ws.js:140:1)
      at finish (node:internal/streams/writable:763:14)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)



/home/pc/Desktop/inbox/node_modules/mocha/lib/runner.js:965
    throw err;
    ^

Error: done() called multiple times in hook <"before each" hook for "deploy a contract"> (of root suite)
    at createMultipleDoneError (/home/pc/Desktop/inbox/node_modules/mocha/lib/errors.js:428:13)
    at multiple (/home/pc/Desktop/inbox/node_modules/mocha/lib/runnable.js:290:24)
    at done (/home/pc/Desktop/inbox/node_modules/mocha/lib/runnable.js:301:14)
    at /home/pc/Desktop/inbox/node_modules/mocha/lib/runnable.js:371:11
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_MOCHA_MULTIPLE_DONE',
  valueType: 'undefined',
  value: undefined
}

version in package.json:

"ganache-cli": "^6.1.8",
"mocha": "^9.1.3",
"solc": "^0.8.11",
"web3": "^1.6.1"

This error bugs me for hours now. Appreciate your help to resolve it.

4 Answers 4

0

the only issue I see is, the callback for it should be async

describe('SimpleStorage', ()=> {
    it('deploy a contract',  async  ()=>{
        console.log(simpleStorage);
    });
});
0

One issue is that you pass a constructor argument (Hi there!), but your contract doesn't take any constructor arguments, so this should probably be an empty array.

Edit: Actually I cannot reproduce this locally. What is your environment (e.g. node version, OS) and how do you run your test? Do you by any chance have a repo to share?

0

try "ganache-cli": "^6.12.2"

0

Install the latest version 7.0.3 of ganache instead ganache-cli

npm install --save-dev ganache

in code just use

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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