From 7268a7dc3856ab8a71b1d3d08ee940b37af583a1 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Tue, 16 Jul 2024 18:53:55 +0200 Subject: [PATCH] run postVisit on failures as well --- .../__snapshots__/transformCsf.test.ts.snap | 75 +++- src/playwright/hooks.ts | 1 + src/playwright/transformPlaywright.test.ts | 375 +++++++++++++----- src/playwright/transformPlaywright.ts | 17 +- .../transformPlaywrightJson.test.ts | 275 ++++++++++--- 5 files changed, 566 insertions(+), 177 deletions(-) diff --git a/src/csf/__snapshots__/transformCsf.test.ts.snap b/src/csf/__snapshots__/transformCsf.test.ts.snap index e04d4ddc..a51ae2f3 100644 --- a/src/csf/__snapshots__/transformCsf.test.ts.snap +++ b/src/csf/__snapshots__/transformCsf.test.ts.snap @@ -29,12 +29,25 @@ if (!require.main) { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "button--primary" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "button--primary" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -96,12 +109,25 @@ if (!require.main) { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "button--primary" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "button--primary" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -149,12 +175,25 @@ if (!require.main) { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "button--primary" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "button--primary" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } diff --git a/src/playwright/hooks.ts b/src/playwright/hooks.ts index 2beac9d2..df693cb5 100644 --- a/src/playwright/hooks.ts +++ b/src/playwright/hooks.ts @@ -5,6 +5,7 @@ export type TestContext = { id: string; title: string; name: string; + hasFailure?: boolean; }; export type PrepareContext = { diff --git a/src/playwright/transformPlaywright.test.ts b/src/playwright/transformPlaywright.test.ts index 4c887fc4..60fe99d2 100644 --- a/src/playwright/transformPlaywright.test.ts +++ b/src/playwright/transformPlaywright.test.ts @@ -70,12 +70,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -115,12 +128,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--b" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--b" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -178,12 +204,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--b" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--b" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -241,12 +280,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -286,12 +338,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--b" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--b" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -362,12 +427,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--b" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--b" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -407,12 +485,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--c" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--c" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -452,12 +543,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--d" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--d" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -497,12 +601,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--e" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--e" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -566,12 +683,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -611,12 +741,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--c" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--c" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -676,12 +819,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -753,12 +909,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -814,12 +983,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-foo-bar--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-foo-bar--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -875,12 +1057,25 @@ describe('Playwright', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-header--a" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-header--a" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } diff --git a/src/playwright/transformPlaywright.ts b/src/playwright/transformPlaywright.ts index fce02b79..52f2b22f 100644 --- a/src/playwright/transformPlaywright.ts +++ b/src/playwright/transformPlaywright.ts @@ -25,9 +25,20 @@ export const testPrefixer: TestPrefixer = (context) => { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ id, hasPlayFn }) => __test(id, hasPlayFn), { - id: %%id%%, - }); + let result; + try { + result = await page.evaluate(({ id, hasPlayFn }) => __test(id, hasPlayFn), { + id: %%id%%, + }); + } catch(err) { + if(err.toString().includes('Execution context was destroyed')) { + // Retry the test, possible Vite dep optimization flake + throw err; + } else { + await globalThis.__sbPostVisit(page, {...context, hasFailure: true }); + throw err; + } + } if(globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); diff --git a/src/playwright/transformPlaywrightJson.test.ts b/src/playwright/transformPlaywrightJson.test.ts index e3712d82..1be7b855 100644 --- a/src/playwright/transformPlaywrightJson.test.ts +++ b/src/playwright/transformPlaywrightJson.test.ts @@ -55,12 +55,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-header--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-header--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -100,12 +113,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-header--logged-out" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-header--logged-out" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -147,12 +173,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-page--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-page--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -241,12 +280,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-b" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-b" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -288,12 +340,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-c" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-c" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -358,12 +423,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-page--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-page--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -449,12 +527,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-header--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-header--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -494,12 +585,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-header--logged-out" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-header--logged-out" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -541,12 +645,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-page--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-page--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -620,12 +737,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-page--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-page--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); } @@ -690,12 +820,25 @@ describe('Playwright Json', () => { if (globalThis.__sbPreVisit) { await globalThis.__sbPreVisit(page, context); } - const result = await page.evaluate(({ - id, - hasPlayFn - }) => __test(id, hasPlayFn), { - id: "example-page--logged-in" - }); + let result; + try { + result = await page.evaluate(({ + id, + hasPlayFn + }) => __test(id, hasPlayFn), { + id: "example-page--logged-in" + }); + } catch (err) { + if (err.toString().includes('Execution context was destroyed')) { + throw err; + } else { + await globalThis.__sbPostVisit(page, { + ...context, + hasFailure: true + }); + throw err; + } + } if (globalThis.__sbPostVisit) { await globalThis.__sbPostVisit(page, context); }