Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kand opened this issue Jan 4, 2018 · 5 comments · Fixed by #5279
Closed

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

kand opened this issue Jan 4, 2018 · 5 comments · Fixed by #5279

Comments

@kand
Copy link

kand commented Jan 4, 2018

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.

jest@22.04
yarn@1.0.2
npm@5.5.1
node@8.9.0
macOS Sierra (version 10.12.6)

@rickhanlonii
Copy link
Member

Note that this fails for both jsdom and node environments and for all Jest 22 releases

Fix is in the above PR

@dylanwulf
Copy link

This is happening to me in jest v25

@dav1app
Copy link

dav1app commented Feb 3, 2020

Also with me in Jest v25. Mine was even smaller:

describe('MemoryLeakTest', function () {
  it('should do nothing', function () {
    expect(true).toBeTruthy()
  })
})
./node_modules/jest/bin/jest.js --detectLeaks

@dylanwulf
Copy link

I submitted a new issue for this problem in v25: #9507

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants