I am learning about smart contracts and blockchain development. I am currently trying to test the deployment of a simple contract using mocha but when running the test the following error is thrown :
Error: done() called multiple times in hook <"before each" hook for "deploys contract"> (of root suite) at createMultipleDoneError (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/errors.js:428:13) at multiple (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:290:24) at done (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:301:14) at /Users/nacholopez/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
Here is my compilation and testing code:
compile.js:
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, 'utf8');
const input = {
language: "Solidity",
sources: {
"Inbox.sol": {
content: source,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
},
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
module.exports = output.contracts["Inbox.sol"].Inbox;
Inbox.test.js :
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { abi, evm } = require('../compile');
let accounts;
let inbox;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(abi).
deploy({ data: evm.bytecode.object, arguments: ['hi there'] }).
send({ from: accounts[0], gas: '1000000' });
});
describe('Inbox', () => {
it('deploys contract', () => {
console.log(inbox);
});
});
I ran into the same issue as well and I made the callback to the 'it' function 'async' and that fixed the issue for me.
it("deploys a contract", async () => {
assert.ok(inbox.options.address);
});