Skip to content

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

Description

@kamrankhanwastaken

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions