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

[Bug]: Jest runs out of memory when trying to print wide objects #12364

Closed
ryanwilsonperkin opened this issue Feb 10, 2022 · 4 comments · Fixed by #12402
Closed

[Bug]: Jest runs out of memory when trying to print wide objects #12364

ryanwilsonperkin opened this issue Feb 10, 2022 · 4 comments · Fixed by #12402

Comments

@ryanwilsonperkin
Copy link
Contributor

ryanwilsonperkin commented Feb 10, 2022

Version

27.5.1

Steps to reproduce

  1. Clone my repo at https://github.com/ryanwilsonperkin/jest-issue-12364
  2. yarn install
  3. yarn test deep-array.test.js you should expect this to fail with an appropriate error message
  4. yarn test wide-array.test.js you should expect this to crash

Expected behavior

I expect that when Jest has found that two objects don't match, and one of them is an array/objects with many (potentially deeply nested) elements, it should be able to print a message to the screen, even if it requires the array/object to be truncated.

This is currently the case for very deep elements, as shown by the deep-array.test.js test case, which simulates an infinitely deep array, and Jest correctly fails quickly with an error message that is limited in scope but does not try to print to infinity.

Actual behavior

Jest will run out of memory when trying to print wide arrays. Objects are passed to the pretty-format which has a maxDepth parameter to prevent printing very deep, but it does not have a maxWidth parameter (or similar) to prevent printing very wide.

This can be easily simulated with a test such as:

it('will crash', () => {
    const o = Array(1_000_000_000);
    expect(o).toBeNull();
});

Even though the runtime can very easily create an empty array of length 1 Billion, when we try to compare it against something, Jest will try to print the entire width of it.

This can also be an issue for objects that aren't extremely wide, but the combination of their widths at each depth result in the same. For example, a nested array that is 10 levels deep, but is 10 elements wide at each level contains 10^10 elements.

Additional context

No response

Environment

System:
    OS: macOS 12.2
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 17.2.0 - /opt/homebrew/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.1.4 - /opt/homebrew/bin/npm
  npmPackages:
    jest: 27.5.1 => 27.5.1
@SimenB
Copy link
Member

SimenB commented Feb 15, 2022

Happy to take a PR that fixes this (either maxWidth like you suggest, or just make maxDepth impact the width as well) 👍

@ryanwilsonperkin
Copy link
Contributor Author

Thanks @SimenB , I've opened #12402 to address this, let me know what you think

@SimenB
Copy link
Member

SimenB commented Feb 16, 2022

https://github.com/facebook/jest/releases/tag/v28.0.0-alpha.2

@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 Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants