beforeEach hook is not running. THis is a strange error, I googled but did not find anything. For anything strange, I delete node-modules and reinstall and it works but this time it did not work.

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

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

var { abi, evm } = require("../compile.js");
const bytecode = evm.bytecode.object;
const abi_string = JSON.stringify(abi); // convert object to string
// those are defined
console.log("Abistring", abi_string);
console.log("bytecode", bytecode)

let accounts;
let lotteryInstance;
// beforeEach(async () => {
beforeEach(async function () {
  accounts = await web3.eth.getAccounts();
  lotteryInstance = await new web3.eth.Contract(JSON.parse(abi_string))
    .deploy({ data: bytecode })
    .send({ from: accounts[0], gas: "1000000" });
  console.log("lotter", lotteryInstance);

describe("Lottery", async () => {
  console.log("lottery", lotteryInstance);

I placed 2 console statemenst inside beforeEach, but they do not log anything. Since beforeEach hook is not running, console.log("lottery", lotteryInstance) is logging undefined.

There is nothing with compile.js file. Everthing is


beforeEach will get executed at before every it() function in mocha.so you have it() fn in your code as shown in code snippet below

describe("Lottery", async () => {
    console.log("lottery", lotteryInstance);
}) });
  • Thank you! Havent used mocha for a while
    – Yilmaz
    Jun 5 '21 at 7:41

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.