From 71fafb9741ca8e1daad431cf100366656df54bdb Mon Sep 17 00:00:00 2001
From: Max Schmitt <max@schmitt.mx>
Date: Tue, 9 Apr 2024 21:54:35 +0200
Subject: [PATCH] test: unflake 'should not include trace resources from the
 previous chunks' test

---
 .../playwright-core/src/client/browserContext.ts     |  1 +
 packages/playwright-core/src/client/tracing.ts       | 12 ++++++++----
 tests/library/browsercontext-reuse.spec.ts           | 10 ++++++----
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts
index 5747c822e19a5b..9c9c389cba8224 100644
--- a/packages/playwright-core/src/client/browserContext.ts
+++ b/packages/playwright-core/src/client/browserContext.ts
@@ -432,6 +432,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
       this._browser._contexts.delete(this);
     this._browserType?._contexts?.delete(this);
     this._disposeHarRouters();
+    this.tracing._resetStackCounterIfNeeded();
     this.emit(Events.BrowserContext.Close, this);
   }
 
diff --git a/packages/playwright-core/src/client/tracing.ts b/packages/playwright-core/src/client/tracing.ts
index c90cdfcee61258..e466dea8720317 100644
--- a/packages/playwright-core/src/client/tracing.ts
+++ b/packages/playwright-core/src/client/tracing.ts
@@ -76,10 +76,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
   }
 
   private async _doStopChunk(filePath: string | undefined) {
-    if (this._isTracing) {
-      this._isTracing = false;
-      this._connection.setIsTracing(false);
-    }
+    this._resetStackCounterIfNeeded();
 
     if (!filePath) {
       // Not interested in artifacts.
@@ -113,4 +110,11 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
 
     await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, includeSources: this._includeSources });
   }
+
+  _resetStackCounterIfNeeded() {
+    if (this._isTracing) {
+      this._isTracing = false;
+      this._connection.setIsTracing(false);
+    }
+  }
 }
diff --git a/tests/library/browsercontext-reuse.spec.ts b/tests/library/browsercontext-reuse.spec.ts
index 057bc1d223b5b3..0ed77b543a3f69 100644
--- a/tests/library/browsercontext-reuse.spec.ts
+++ b/tests/library/browsercontext-reuse.spec.ts
@@ -17,17 +17,19 @@
 import { browserTest, expect } from '../config/browserTest';
 import type { BrowserContext, BrowserContextOptions } from '@playwright/test';
 
-const test = browserTest.extend<{ reusedContext: (options?: BrowserContextOptions) => Promise<BrowserContext> }>({
-  reusedContext: async ({ browserType, browser }, use) => {
+const test = browserTest.extend<{}, { reusedContext: (options?: BrowserContextOptions) => Promise<BrowserContext> }>({
+  reusedContext: [async ({ browserType, browser }, use) => {
+    let context;
     await use(async (options: BrowserContextOptions = {}) => {
       const defaultContextOptions = (browserType as any)._defaultContextOptions;
-      const context = await (browser as any)._newContextForReuse({
+      context = await (browser as any)._newContextForReuse({
         ...defaultContextOptions,
         ...options,
       });
       return context;
     });
-  },
+    // await context.close();
+  }, { scope: 'worker' }],
 });
 
 test('should re-add binding after reset', async ({ reusedContext }) => {