Solidity

mocha beforeEach 沒有執行

  • June 5, 2021

beforeEach 鉤子沒有執行。這是一個奇怪的錯誤,我用Google搜尋但沒有找到任何東西。對於任何奇怪的事情,我刪除了節點模組並重新安裝它可以工作,但這次它沒有工作。

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");
});

我在 beforeEach 中放置了 2 個控制台語句,但它們沒有記錄任何內容。由於 beforeEach 掛鉤未執行, console.log("lottery", lotteryInstance)因此未定義日誌記錄。

compile.js 文件沒有任何內容。一切都是

beforeEach 將在 mocha 中的每個 it() 函式之前執行,因此您的程式碼中有 it() fn,如下面的程式碼片段所示

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

引用自:https://ethereum.stackexchange.com/questions/101361