Repository: microsoft/playwright
File: tests/playwright-test/playwright.trace.spec.ts (TypeScript)

Code before:
  const trace = await parseTrace(tracePath);
  expect(trace.model.renderActionTree()).toContain('GET "/empty.html"');
  expect(result.failed).toBe(1);
});

test('trace:retain-on-failure-and-retries should keep all traces when test is flaky', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test, expect } from '@playwright/test';
      test('flaky', async ({ page }) => {
        await page.goto('about:blank');
        expect(test.info().retry).toBe(1);
      });
    `,
  }, { trace: 'retain-on-failure-and-retries', retries: 1 });

  expect(result.exitCode).toBe(0);
  expect(result.flaky).toBe(1);

  const firstRunTracePath = testInfo.outputPath('test-results', 'a-flaky', 'trace.zip');
  expect(fs.existsSync(firstRunTracePath)).toBeTruthy();
  const retryTracePath = testInfo.outputPath('test-results', 'a-flaky-retry1', 'trace.zip');
  expect(fs.existsSync(retryTracePath)).toBeTruthy();
});

test('trace:retain-on-failure-and-retries should not create trace if test passes', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test, expect } from '@playwright/test';
      test('passing', async ({ page }) => {
        await page.goto('about:blank');
      });
    `,
  }, { trace: 'retain-on-failure-and-retries' });

  expect(result.exitCode).toBe(0);
  expect(result.passed).toBe(1);

  const tracePath = testInfo.outputPath('test-results', 'a-passing', 'trace.zip');
  expect(fs.existsSync(tracePath)).toBeFalsy();
});

test('trace:retain-on-failure-and-retries should keep all traces when test fails on retries', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test, expect } from '@playwright/test';
      test('fail', async ({ page }) => {
        await page.goto('about:blank');
        expect(true).toBe(false);
      });
    `,

Review comment (refactor):
remove this test, this case is already covered by test(`trace:${mode} should not create trace zip artifact if page test passed` above

Code after:
  const trace = await parseTrace(tracePath);
  expect(trace.model.renderActionTree()).toContain('GET "/empty.html"');
  expect(result.failed).toBe(1);
});

test('trace:retain-on-failure-and-retries should keep all traces when test is flaky', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test, expect } from '@playwright/test';
      test('flaky', async ({ page }) => {
        await page.goto('about:blank');
        expect(test.info().retry).toBe(1);
      });
    `,
  }, { trace: 'retain-on-failure-and-retries', retries: 1 });

  expect(result.exitCode).toBe(0);
  expect(result.flaky).toBe(1);

  const firstRunTracePath = testInfo.outputPath('test-results', 'a-flaky', 'trace.zip');
  expect(fs.existsSync(firstRunTracePath)).toBeTruthy();
  const retryTracePath = testInfo.outputPath('test-results', 'a-flaky-retry1', 'trace.zip');
  expect(fs.existsSync(retryTracePath)).toBeTruthy();
});

test('trace:retain-on-failure-and-retries should keep all traces when test fails on retries', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test, expect } from '@playwright/test';
      test('fail', async ({ page }) => {
        await page.goto('about:blank');
        expect(true).toBe(false);
      });
    `,
  }, { trace: 'retain-on-failure-and-retries', retries: 1 });

  expect(result.exitCode).toBe(1);
  expect(result.failed).toBe(1);

  const firstRunTracePath = testInfo.outputPath('test-results', 'a-fail', 'trace.zip');
  expect(fs.existsSync(firstRunTracePath)).toBeTruthy();
  const retryTracePath = testInfo.outputPath('test-results', 'a-fail-retry1', 'trace.zip');
  expect(fs.existsSync(retryTracePath)).toBeTruthy();
});

test('should not corrupt actions when no library trace is present', async ({ runInlineTest }) => {
  const result = await runInlineTest({
    'a.spec.ts': `
      import { test as base, expect } from '@playwright/test';
      const test = base.extend({
        foo: async ({}, use) => {