Supertest giving MaxListenersExceedingWarning error with my Jest testing in Node
Asked Answered
I

1

6

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.

Iou answered 24/5, 2020 at 16:7 Comment(1)
I've had the same issue for the last year now. Only solution I've found so far is not to mount the Sentry middlewares during tests.. would be nice to be able to leave them tho to better reproduce the production environment for integration testsSince
Z
2

I had this problem too and the solution I found was:

In package.json, in the test script, I set the NODE_ENV environment variable to test.

  "scripts": {
    "test": "NODE_ENV=test jest --detectOpenHandles -i",
    "test:watch": "NODE_ENV=test jest --watch --coverage=false --
  },

In app.js, if we are in a test environment, we won't start Sentry. This will be done from a conditional.

if (process.env.NODE_ENV !== 'test') {
  Sentry.init({ environment: "development", dsn: "..." })
}
Zoniazoning answered 27/10, 2021 at 14:37 Comment(1)
Had the same problem. Tried a lot of approaches but helped turn off the Sentry in TEST mode.Revolt

© 2022 - 2024 — McMap. All rights reserved.