JavaScript heap out of memory after upgrade to Jest 26
  • 04-Jun-2023
Lightrun Team
Author Lightrun Team
JavaScript heap out of memory after upgrade to Jest 26

JavaScript heap out of memory after upgrade to Jest 26

Lightrun Team
Lightrun Team

Explanation of the problem

  1. After upgrading from version 24.X to 26.0.0 of an unspecified software, a test that was previously passing now fails, and the test execution takes a significantly longer time to complete. The error message accompanying the failure is not provided. To reproduce the issue, a specific test case is shared, which involves a describe block with several it blocks. In the beforeEach hook, certain functions are mocked, and the setItemsToBeReviewed function is called to set item IDs in sessionStorage. The failing test cases include ‘initial fetch’, ‘fetch more while no more’, and ‘fetch more while more’.Here is an excerpt of the code related to the test scenario:


describe('when item ids are in sessionStorage', () => {
  const itemIds = [333, 222, 111];

  beforeEach(() => {
    // Mocking functions
    parseLocationToQueries.mockImplementation(() => ({
      queue_id: testQueueId

  it('initial fetch', () => {
    const wrapper = tf.render();
    expect('/review/items', { item_ids: itemIds });

  // More test cases follow...


The above code snippet shows a test suite where item IDs are stored in sessionStorage. The beforeEach hook is responsible for mocking certain functions and setting the item IDs using the setItemsToBeReviewed function. The failing test cases, such as ‘initial fetch’, make assertions about the state of the component and expected API calls using axios.


Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: JavaScript heap out of memory after upgrade to Jest 26

To solve the problem of the failing test case and the extended test execution time after upgrading to version 26.0.0, several steps can be taken:

  1. Investigate the Error and Execution Time:
    • The error message accompanying the failed test case is missing. It’s essential to obtain the specific error message and analyze the stack trace or error logs to understand the root cause of the failure.
    • Measure and compare the test execution time before and after the upgrade to determine the extent of the performance impact. This can be done by using appropriate tools or frameworks to profile the test execution.
  2. Review the Changelog and Release Notes:
    • Examine the changelog and release notes of the upgraded version (26.0.0) to identify any relevant changes or known issues that could affect the test case or impact performance.
    • Pay special attention to changes related to the test framework, testing libraries, dependencies, or any changes that might impact the behavior of the software being tested.
  3. Debug and Fix the Test Case:
    • Analyze the failing test case in detail and review the test assertions, test setup, and the components involved.
    • Ensure that the mocked functions and dependencies are correctly set up and that they provide the expected behavior during the test execution.
    • If necessary, modify the test case to accommodate any changes or updates introduced in version 26.0.0.
    • Debug any issues by using console.log statements, debugging tools, or relevant logging mechanisms to trace the flow of execution and identify potential issues.
  4. Validate Dependencies:
    • Verify that all relevant dependencies, including the testing framework, testing libraries, and any other related packages, are updated to their compatible versions as recommended by the upgraded version’s documentation.
    • Check for any reported issues or conflicts between the upgraded version and the dependencies being used. Resolve any conflicts by updating the dependencies or finding alternative solutions.
  5. Consider Performance Optimization:
    • If the extended test execution time is a concern, explore potential performance optimizations specific to the testing framework, such as optimizing test setup, reducing unnecessary network calls, or employing techniques like test parallelization or test data caching to improve test execution speed.

Remember to document any steps taken, including changes made to the test case or dependencies, to ensure clear communication and reproducibility. It’s also advisable to consult relevant documentation, community forums, or reach out to the project’s maintainers for assistance in resolving the issue, as they may provide valuable insights or bug fixes specific to the upgraded version.


Other popular problems with Jest

Problem 1: Test Fails due to Timing Issues

Description: One common problem encountered when using Jest is test failures caused by timing issues. Jest executes tests asynchronously, which can lead to race conditions when dealing with asynchronous code, such as promises, timeouts, or API calls. These timing issues can result in flaky tests, where a test may pass or fail inconsistently.

Solution: To address timing-related test failures, Jest provides mechanisms to handle asynchronous operations properly. One approach is to use async/await or return Promise syntax in test functions to ensure the tests wait for asynchronous operations to complete before making assertions. Additionally, Jest provides functions like waitFor, waitForElement, or findBy methods for testing components with asynchronous rendering or API calls. These functions help synchronize test execution and ensure that assertions are made at the appropriate time.



test('asynchronous test using async/await', async () => {
  // Perform asynchronous operations
  await doAsyncOperation();

  // Make assertions after the asynchronous operations complete


Problem 2: Mocking External Dependencies

Description: When writing tests, it’s often necessary to mock external dependencies, such as API calls, databases, or third-party libraries. Jest provides powerful mocking capabilities, but configuring mocks for complex dependencies or handling specific scenarios can be challenging.

Solution: Jest offers different mocking techniques, such as manual mocking using the jest.mock function, automatic mocking using the __mocks__ directory, and module mocking with jest.fn or jest.spyOn. These techniques allow developers to replace actual dependencies with mock implementations or stubs, ensuring controlled test environments.



// Mocking an API call
const axios = require('axios');
axios.get.mockResolvedValue({ data: { id: 1, name: 'Mocked Data' } });

test('testing API call', async () => {
  const response = await fetchDataFromAPI();
  expect(response).toEqual({ id: 1, name: 'Mocked Data' });


Problem 3: Code Coverage and Test Reporting

Description: Ensuring adequate code coverage and generating comprehensive test reports are crucial aspects of testing. However, configuring Jest to collect code coverage information and generate detailed reports can be challenging, especially in complex projects.

Solution: Jest provides built-in support for code coverage and offers various configuration options to customize the coverage reports. By setting the collectCoverage option to true in the Jest configuration, Jest will collect coverage information during test execution. Additionally, Jest supports generating coverage reports in different formats, such as HTML, text, or JSON, using plugins like jest-html-reporter or jest-junit. These reports provide insights into which parts of the code are covered by tests and can help identify areas that need additional test coverage.



// jest.config.js
module.exports = {
  // Enable code coverage collection
  collectCoverage: true,

  // Configure coverage report
  coverageReporters: ['html', 'text'],

  // Configure additional plugins or reporters
  reporters: ['default', 'jest-html-reporter'],


A brief introduction to Jest

Jest is a popular JavaScript testing framework developed by Facebook. It provides a comprehensive and intuitive testing solution for JavaScript applications, enabling developers to write unit tests, integration tests, and snapshot tests with ease. Jest is designed to be fast, reliable, and easy to set up, making it a preferred choice for many developers.

One of the key features of Jest is its support for automatic mocking. Jest allows developers to easily mock dependencies and external modules, enabling isolated testing of individual components or functions. With Jest’s mocking capabilities, developers can simulate behavior and control the inputs and outputs of mocked functions, ensuring predictable and controlled test environments.

Another notable feature of Jest is its snapshot testing functionality. Snapshot testing allows developers to capture the current state of a component or data structure and compare it against a previously stored snapshot. This enables quick and efficient regression testing, ensuring that changes in the codebase do not unintentionally modify the expected output. Jest’s snapshot testing feature simplifies the process of detecting unexpected changes in the application’s UI or data structures, helping to identify potential bugs or regressions early in the development cycle.

Overall, Jest provides a comprehensive and powerful testing framework for JavaScript applications. With its intuitive API, robust mocking capabilities, and snapshot testing functionality, Jest simplifies the process of writing and maintaining tests, promoting code quality and reliability in JavaScript projects.


Most popular use cases for Jest

  1. Unit Testing: Jest excels in unit testing JavaScript code. It provides a rich set of assertion functions, such as expect and toMatch, to verify the behavior and output of functions, components, or modules. Developers can write focused unit tests to validate individual units of code and ensure that they perform as expected. Here’s an example of a unit test using Jest:


function add(a, b) {
  return a + b;

test('add function correctly adds two numbers', () => {
  expect(add(2, 3)).toBe(5);
  expect(add(-1, 7)).toBe(6);


  1. Integration Testing: Jest is also suitable for integration testing, where multiple components or modules are tested together to ensure their seamless interaction. With Jest’s test runner and powerful mocking capabilities, developers can simulate the behavior of dependencies and external APIs, enabling thorough testing of the integration points. This allows for the detection of potential issues that may arise from the interaction between different parts of the application.
  2. Snapshot Testing: Jest provides snapshot testing functionality, which enables developers to capture the rendered output of components, data structures, or JSON objects and compare them against stored snapshots. This is useful for detecting unintended changes in the rendered UI or data over time. Here’s an example of snapshot testing with Jest:


test('renders a button component correctly', () => {
  const button = render(<Button label="Click me" />);


In this test, Jest captures the rendered output of the Button component and compares it with the stored snapshot. If any changes are detected, Jest will notify the developer, helping to prevent unexpected regressions.

Overall, Jest is a versatile testing framework that can be used for unit testing, integration testing, and snapshot testing in JavaScript applications. Its extensive feature set, ease of use, and robust testing capabilities make it a popular choice among developers.



It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.