Commit 21c1852f47 for woocommerce

commit 21c1852f47f36da7fca6cd526a8b8a7071763350
Author: Copilot <198982749+Copilot@users.noreply.github.com>
Date:   Tue Dec 9 17:15:52 2025 +0000

    Fix flaky e2e test by handling page closure in localStorage cleanup (#61950)

    * Initial plan

    * Wrap localStorage.clear() in try/catch to prevent race condition

    Co-authored-by: opr <5656702+opr@users.noreply.github.com>

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Add error logging to localStorage cleanup for debugging

    Co-authored-by: opr <5656702+opr@users.noreply.github.com>

    ---------

    Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
    Co-authored-by: opr <5656702+opr@users.noreply.github.com>
    Co-authored-by: github-actions <github-actions@github.com>

diff --git a/plugins/woocommerce/changelog/61950-copilot-fix-flaky-test-cart-item-count b/plugins/woocommerce/changelog/61950-copilot-fix-flaky-test-cart-item-count
new file mode 100644
index 0000000000..2efefa473d
--- /dev/null
+++ b/plugins/woocommerce/changelog/61950-copilot-fix-flaky-test-cart-item-count
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Fix flaky mini-cart e2e test by handling page closure during localStorage cleanup
\ No newline at end of file
diff --git a/plugins/woocommerce/client/blocks/tests/e2e/utils/test.ts b/plugins/woocommerce/client/blocks/tests/e2e/utils/test.ts
index 7b7ad1718d..95ad62c09a 100644
--- a/plugins/woocommerce/client/blocks/tests/e2e/utils/test.ts
+++ b/plugins/woocommerce/client/blocks/tests/e2e/utils/test.ts
@@ -130,9 +130,15 @@ const test = base.extend<
 		await use( page );

 		// Clear local storage after each test.
-		await page.evaluate( () => {
-			window.localStorage.clear();
-		} );
+		try {
+			await page.evaluate( () => {
+				window.localStorage.clear();
+			} );
+		} catch ( error ) {
+			// Ignore errors if page is already closed/navigated away
+			// eslint-disable-next-line no-console
+			console.log( 'Failed to clear localStorage:', error.message );
+		}

 		// Dispose the current APIRequestContext to free up resources.
 		await page.request.dispose();