question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest --detectleaks shows memory leak while using SuperTest with Express

See original GitHub issue

Hey 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:open
  • Created 4 years ago
  • Reactions:18
  • Comments:13

github_iconTop GitHub Comments

23reactions
imjordanxdcommented, Feb 4, 2021

Still an issue in 2021 👎

8reactions
nfantonecommented, May 13, 2021

In case you guys haven’t looked in the jest issues, it looks like this is occurring because one of the modules you’re using is mutating a native node module, eg. http, https, etc. I drove myself crazy hunting down this issue in my integration tests; the culprit turned out to a library called follow-redirects. It was mutating the http and https modules.

I don’t think this is the bottom of it. I’ve tried the most basic setup (empty project with express, jest and supertest) and --detectLeaks fails for me still. See https://github.com/visionmedia/supertest/issues/520#issuecomment-840707336.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest --detectleaks shows memory leak while using SuperTest ...
I have been experiencing memory leaks in any test files that require supertest when using Jest with the --detectLeaks option on an express...
Read more >
Cannot find memory leak in my Express.js Jest tests
Yes, the buildDocument function usually builds some Document object and calls .save() on it (the mongoose save() function). It does require ...
Read more >
Your Jest Tests are Leaking Memory
In this article, we'll walk through why it's so easy for Jest to leak memory, how to tell if your tests have a...
Read more >
Finding the cause of a memory leak in Jest tests
Read on! Running tests with heap usage recording. We tried increasing memory allocation to see if the tests just take too much memory,...
Read more >
Jest Memory Leaks - Some Lessons Learnt - Lukas Spiss
Recently I was facing some memory leaks in the test suits of one of my projects. ... Nest.JS v8; Node.JS v14; ~800 unit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found