首页 > 解决方案 > 是什么导致 web3js 测试代码中的 MaxListenersExceededWarning 警告

问题描述

我已经测试了以太坊合约的代码

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 () => {
  // Get a list of all accounts
  accounts = await web3.eth.getAccounts();

  // Use one of those accounts to deploy the contract
  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 a contract", () => {
    console.log(accounts);
    console.log(inbox);
    assert.ok(inbox.options.address);
  });
});

这显示以下警告

(node:52888) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit

造成这种情况的可能原因是什么以及如何解决?

标签: node.jsmocha.jsethereumsolidityweb3js

解决方案


得到了答案。这是带有版本 1 的发送功能的 web3js 中的一个问题。

所以要么设置最大听众使用

web3.currentProvider.setMaxListeners(300);

或者升级到 v2^ 它已经固定在那里


推荐阅读