Jest --detectleaks shows memory leak while using SuperTest with Express
See original GitHub issueHey everyone,
I have been experiencing memory leaks in any test files that require supertest when using Jest with the --detectLeaks
option on an express server.
Versions
node: 12.16.1 node: 6.14.2
Modules
jest: 25.1.0
supertest: 4.0.2
weak-napi: 1.0.3 (jest complains without this module available when using detectLeaks
)
express: 4.17.1
To isolate the issue from any custom development code in my particular application, I have created this repository to highlight the problem:
https://github.com/fakekamrankhan/express-supertest-jest-detect-memory-leak
If you run npm test
you’ll see the memory leak warning. Removing supertest gets rid of the memory leak flag. The test.js file in question:
const express = require('express');
const request = require('supertest');
describe('leak', () => {
let port;
let server;
beforeAll(() => {
port = 3000;
server = new express().listen(port);
});
afterAll(() => {
server.close();
})
test('leak test', async () => {
let res = await request(server).get('/');
expect(res.status).toBe(404);
});
});
The error:
EXPERIMENTAL FEATURE!
Your test suite is leaking memory. Please ensure all references are cleaned.
There is a number of things that can leak memory:
- Async operations that have not finished (e.g. fs.readFile).
- Timers not properly mocked (e.g. setInterval, setTimeout).
- Keeping references to the global scope.
at onResult (node_modules/@jest/core/build/TestScheduler.js:188:18)
I have been wracking by brain for the last four days trying to pinpoint the issue. Does anyone know if this could be anything in the test or if this is a bug in superest or jest?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:18
- Comments:13
Top GitHub Comments
Still an issue in 2021 👎
I don’t think this is the bottom of it. I’ve tried the most basic setup (empty project with
express
,jest
andsupertest
) and--detectLeaks
fails for me still. See https://github.com/visionmedia/supertest/issues/520#issuecomment-840707336.