Skip to content

--detectLeaks reporting leaks on a very minimal test environment #5234

Closed
@kand

Description

@kand

Do you want to request a feature or report a bug?

bug

What is the current behavior?

Jest --detectLeaks is reporting leaks even on very minimal test suites.

I've set up an example repo here: https://github.com/kand/jest-22-mem-leak

There are two test files:

describe('memory leak suite 1', () => {

  it('happens', () => {
    console.log('suite 1 > test 1 > ran test');
  });
});
describe('memory leak suite 2', () => {

  it('happens', () => {
    console.log('suite 2 > test 1 > ran test');
  });
});

As well as some added console.log statements in node_modules/jest-leak-detector/build/index.js:

// line 51
    console.log('[IN JEST (line 52)] attaching GC listener to', value.constructor.name);
    weak(value, () => {
      console.log('[IN JEST (line 54)] garbage collected value');
      this._isReferenceBeingHeld = false
    });
    this._isReferenceBeingHeld = true;

    // Ensure value is not leaked by the closure created by the "weak" callback.
    value = null;
  }

  isLeaking() {
    this._runGarbageCollector();

    console.log('[IN JEST (line 66)] checking leak');
    return this._isReferenceBeingHeld;
  }
// line 69

Which produces the following output when tests are run:

yarn test v1.0.2
$ jest --detectLeaks --runInBand
 FAIL  ./file-1.test.js
  ● Test suite failed to run

    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 node_modules/jest-cli/build/test_scheduler.js:115:22

[IN JEST (line 52)] attaching GC listener to JSDOMEnvironment
[IN JEST (line 66)] checking leak
[IN JEST (line 52)] attaching GC listener to JSDOMEnvironment
 FAIL  ./file-2.test.js
  ● Test suite failed to run

    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 node_modules/jest-cli/build/test_scheduler.js:115:22

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.734s
Ran all test suites.
[IN JEST (line 54)] garbage collected value
[IN JEST (line 66)] checking leak
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

It doesn't appear that either of these tests should be producing a memory leak. Memory leaks are also reported when --runInBand is not used. It looks like the leak detector is checking for memory leaks before the JSDOMEnvironment object is cleaned up, which causes jest to report a memory leak, though I'm sure this is not the root cause.

I believe these issues are related:

#1893
jsdom/jsdom#1682
jsdom/jsdom#1682

But 2 are closed and the open one hasn't had activity in a while.

What is the expected behavior?

No memory leaks are reported.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

[email protected]
[email protected]
[email protected]
[email protected]
macOS Sierra (version 10.12.6)

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