From a21ccb71ce5beb1ded9ab9398a01e40905186c73 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:42:04 -0600 Subject: [PATCH] feat(playwright/worker): extract should capture trace logic into private method. --- packages/playwright/src/worker/testTracing.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/playwright/src/worker/testTracing.ts b/packages/playwright/src/worker/testTracing.ts index 579bf8bc87..93cf5daad5 100644 --- a/packages/playwright/src/worker/testTracing.ts +++ b/packages/playwright/src/worker/testTracing.ts @@ -19,11 +19,10 @@ import type * as trace from '@trace/trace'; import type EventEmitter from 'events'; import fs from 'fs'; import path from 'path'; -import { ManualPromise, calculateSha1, monotonicTime, createGuid } from 'playwright-core/lib/utils'; +import { ManualPromise, calculateSha1, createGuid, monotonicTime } from 'playwright-core/lib/utils'; import { yauzl, yazl } from 'playwright-core/lib/zipBundle'; -import type { TestInfo, TestInfoError } from '../../types/test'; +import type { PlaywrightWorkerOptions, TestInfo, TestInfoError, TraceMode } from '../../types/test'; import { filteredStackTrace } from '../util'; -import type { TraceMode, PlaywrightWorkerOptions } from '../../types/test'; import type { TestInfoImpl } from './testInfo'; export type Attachment = TestInfo['attachments'][0]; @@ -48,8 +47,20 @@ export class TestTracing { this._tracesDir = path.join(this._artifactsDir, 'traces'); } + private shouldCaptureTrace() { + let capture = false; + + if (this._options?.mode === 'on') capture = true; + if (this._options?.mode === 'retain-on-failure') capture = true; + if (this._options?.mode === 'on-first-retry' && this._testInfo.retry === 1) capture = true; + if (this._options?.mode === 'on-all-retries' && this._testInfo.retry > 0) capture = true; + + return capture && !process.env.PW_TEST_DISABLE_TRACING; + } + async startIfNeeded(value: TraceFixtureValue) { const defaultTraceOptions: TraceOptions = { screenshots: true, snapshots: true, sources: true, attachments: true, _live: false, mode: 'off' }; + if (!value) { this._options = defaultTraceOptions; } else if (typeof value === 'string') { @@ -59,9 +70,7 @@ export class TestTracing { this._options = { ...defaultTraceOptions, ...value, mode: (mode as string) === 'retry-with-trace' ? 'on-first-retry' : mode }; } - let shouldCaptureTrace = this._options.mode === 'on' || this._options.mode === 'retain-on-failure' || (this._options.mode === 'on-first-retry' && this._testInfo.retry === 1) || (this._options.mode === 'on-all-retries' && this._testInfo.retry > 0); - shouldCaptureTrace = shouldCaptureTrace && !process.env.PW_TEST_DISABLE_TRACING; - if (!shouldCaptureTrace) { + if (this.shouldCaptureTrace() === false) { this._options = undefined; return; }