简体   繁体   中英

What is causing the MaxListenersExceededWarning warning in web3js test code

I've testing code for a contract in ethereum

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

This is showing the following warning

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

What is the probable reason for this and how to resolve it?

Got the answer. It's an issue in web3js with send function for version 1.

So either set max listeners using

web3.currentProvider.setMaxListeners(300);

Or upgrade to v2^ it's fixed there .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM