cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.09k stars 3.19k forks source link

Chrome browser hangs due to potential memory leak caused by failing tests #30664

Open FrancisBourgault opened 13 hours ago

FrancisBourgault commented 13 hours ago

Test code to reproduce

Command to run the tests

set DEBUG=cypress*memory && set CYPRESS_INTERNAL_MEMORY_SAVE_STATS=1 && set CYPRESS_NO_COMMAND_LOG=1 && tsc && cypress run --config-file cypress.dev.config.js --browser chrome --headless

Configuration file

// cypress.dev.config.ts
import { defineConfig } from 'cypress'

module.exports = defineConfig({
  video: false,
  e2e: {
    screenshotOnRunFailure: false,
    numTestsKeptInMemory: 0,
    testIsolation: true,
    experimentalMemoryManagement: true,
    baseUrl: "http://localhost:5173",
    supportFile: "./cypress/support/e2e.js",
    specPattern: "./cypress/e2e/**/*.cy.js"
  },
});

// Test file

// testFile.cy.ts
describe('Test File', () => {

    context("Test Suite", { viewportWidth: 1920, viewportHeight: 1080 }, function() {

        function setUpAndNavigate() {
            cy.visit("http://localhost:5173");

            // May not be necessary
            return cy.wait(500);
        }

        // Depending on your available memory, it may take more or less tests to crash the browser. Adjust the number of loops in the code if necessary.
        for (let i = 0; i < 1000; ++i) {

            it(`Success Test number ${i}`, function() {
                setUpAndNavigate();

                cy.get("#root h1").should("be.visible");
            });

            it(`Fail Test number ${i}`, function() {
                setUpAndNavigate();

                cy.get("#root h1", { timeout: 1}).should("not.exist", { timeout: 1});
            });
        }
    });
});

Cypress Mode

cypress run

Cypress Version

13.6.0

Browser Version

Chrome 131.0.6778.71

Node version

20.9.0

Operating System

Windows 11

Memory Debug Logs

See 2nd comment (logs too big to be uploaded in this section)

Other

More details on the issue

Every time a test fails, the memory usage increases. If you comment the fail tests, and run only the success tests (you can double the amount of loops so the number of tests stays the same), the memory usage won't increase.

This impacts CI run that contains bigger spec files. If any of the big spec file freeze due to that memory issue, none of the other remaining spec files will be run.

This can happen for a number of different reasons.

If you're running lots of tests on a memory intense application.

You can learn more here:

https://on.cypress.io/renderer-process-crashed

- This issue happens on 3 different type of applications (Craco React, Vite React, Angular), so the type of app does not matter.
- Replacing the "for" loop with copies of the test also cause the issue
- This issue happens whether testIsolation is set to true or false.
- This issue happens whether experimentalMemoryManagement is set to true or false.
- This issue happens whether CYPRESS_NO_COMMAND_LOG is set to 1 or 0

# Setting up a small app to test this issue
- To create a small app to quickly test this issue, run this command:

npm create vite@latest Project name: Select a framework: React Select a variant: TypeScript


- Run "npm run dev" to launch the Vite-React application. Set the url into your cypress.config.ts file.
FrancisBourgault commented 13 hours ago

Partial memory logs here (include performance in the middle of the test run, as well as the last test):

CypressMemoryLog.txt