I'm working on setting up some tests using Jest and Supertest with my Node Express app.
I'm also using Sentry to report my errors. I have the following lines of code in my app.js file to setup Sentry:
app.js
Sentry.init({ environment: "development", dsn: "..." });
app.use(Sentry.Handlers.requestHandler());
...
app.use(Sentry.Handlers.errorHandler());
I have a simple test that directs to the homepage and expects a 200 status code.
home.test.js
const app = require("../src/app");
const request = require("supertest")(app);
describe("homepage", () => {
it("welcomes the user", async () => {
await request.get("/").expect(200);
});
});
I then run my npm run test
script in my console. The test passes with no problem; however, when the test is run multiple times (more than 10), I receive an error message. The test will pass, but I receive the following message.
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 response listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 response listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 response listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 response listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
(node:84470) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 response listeners added to [ClientRequest]. Use emitter.setMaxListeners() to increase limit
I know there is a problem with Supertest because if my test only contains a simple test, for example expect(2+2).toBe(4);
there is no issue. It's only when I "request" a route (await request.get("/").expect(200);
).
Since I'm requesting the app, the next problem is that multiple error and response listeners are being added with app.use(Sentry.Handles.requestHandler());
and app.use(Sentry.Handlers.errorHandler());
. If I remove my Sentry initializing code, everything works perfectly.
I can't remove those lines, because Sentry won't work, but I'm not sure what to do. I have looked and looked, but can't find anybody else having similar problem. Honestly, I'm not sure what to search. I've been working on it for several days now and I'm at a loss.
Thanks for the help.