0

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);
  console.log("something");
});

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

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

1

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 () => {
it('test1',async()=>{
    console.log("lottery", lotteryInstance);
    console.log("printing");
}) });
1
  • Thank you! Havent used mocha for a while
    – Yilmaz
    Jun 5 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.