首页 > 解决方案 > 如何对来自 Node 的 MaxListenersExceededWarning 消息进行故障排除?

问题描述

我正在通过 mocha(通过 Node)运行 Selenium 测试,每次运行全部测试时,我都会在同一个确切位置看到此警告消息:

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

如果我运行一个测试,我不到这个问题,所以这个问题似乎与运行多个测试有关。

我试过指定--trace-warnings命令行参数,但它似乎没有做任何事情。具体来说,我修改了我的测试运行脚本来做到这一点:

node --trace-warnings node_modules/mocha/bin/mocha ...

我仍然看到相同的警告消息,但我没有看到堆栈跟踪。

SO 上的其他帖子提供了关于增加限制的建议,但这不是我想要在这里做的。我试图弄清楚为什么首先出现此警告。

标签: javascriptnode.jsseleniummocha.js

解决方案


我不知道为什么命令行参数不起作用,但我找到了一种从这里获取堆栈跟踪的替代方法:

https://nodejs.org/docs/latest/api/process.html#process_event_warning

process.on('warning', (warning) => {
  console.warn(warning.name);    // Print the warning name
  console.warn(warning.message); // Print the warning message
  console.warn(warning.stack);   // Print the stack trace
});

推荐阅读