From 7ad03027fb027465d59d34469f7b4fd5e8c5c7aa Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 30 May 2023 16:03:50 -0700 Subject: [PATCH] chore: remove output dir before each test (#23380) --- packages/playwright-test/src/runner/runner.ts | 2 -- packages/playwright-test/src/runner/tasks.ts | 2 ++ .../playwright-test/src/worker/workerMain.ts | 4 ++- tests/playwright-test/list-files.spec.ts | 4 --- .../playwright-test/ui-mode-test-run.spec.ts | 34 +++++++++++++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/playwright-test/src/runner/runner.ts b/packages/playwright-test/src/runner/runner.ts index f42910c6c4..c2fc15e7fd 100644 --- a/packages/playwright-test/src/runner/runner.ts +++ b/packages/playwright-test/src/runner/runner.ts @@ -30,7 +30,6 @@ import { InternalReporter } from '../reporters/internalReporter'; type ProjectConfigWithFiles = { name: string; testDir: string; - outputDir: string; use: { testIdAttribute?: string }; files: string[]; }; @@ -55,7 +54,6 @@ export class Runner { report.projects.push({ name: project.project.name, testDir: project.project.testDir, - outputDir: project.project.outputDir, use: { testIdAttribute: project.project.use.testIdAttribute }, files: await collectFilesForProject(project) }); diff --git a/packages/playwright-test/src/runner/tasks.ts b/packages/playwright-test/src/runner/tasks.ts index 1e9e112230..b4d0f28789 100644 --- a/packages/playwright-test/src/runner/tasks.ts +++ b/packages/playwright-test/src/runner/tasks.ts @@ -146,6 +146,8 @@ function createGlobalSetupTask(): Task { function createRemoveOutputDirsTask(): Task { return async ({ config }) => { + if (process.env.PW_TEST_NO_REMOVE_OUTPUT_DIRS) + return; const outputDirs = new Set(); for (const p of config.projects) { if (!config.cliProjectFilter || config.cliProjectFilter.includes(p.project.name)) diff --git a/packages/playwright-test/src/worker/workerMain.ts b/packages/playwright-test/src/worker/workerMain.ts index b2fc4938d5..827cd20a55 100644 --- a/packages/playwright-test/src/worker/workerMain.ts +++ b/packages/playwright-test/src/worker/workerMain.ts @@ -318,6 +318,8 @@ export class WorkerMain extends ProcessRunner { return; } + await removeFolderAsync(testInfo.outputDir).catch(() => {}); + let testFunctionParams: object | null = null; await testInfo._runAsStep({ category: 'hook', title: 'Before Hooks' }, async step => { testInfo._beforeHooksStep = step; @@ -471,7 +473,7 @@ export class WorkerMain extends ProcessRunner { const preserveOutput = this._config.config.preserveOutput === 'always' || (this._config.config.preserveOutput === 'failures-only' && testInfo._isFailure()); if (!preserveOutput) - await removeFolderAsync(testInfo.outputDir).catch(e => {}); + await removeFolderAsync(testInfo.outputDir).catch(() => {}); } private async _runModifiersForSuite(suite: Suite, testInfo: TestInfoImpl, scope: 'worker' | 'test', timeSlot: TimeSlot | undefined, extraAnnotations?: Annotation[]) { diff --git a/tests/playwright-test/list-files.spec.ts b/tests/playwright-test/list-files.spec.ts index 00038dbe0c..263d11a226 100644 --- a/tests/playwright-test/list-files.spec.ts +++ b/tests/playwright-test/list-files.spec.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import path from 'path'; import { test, expect } from './playwright-test-fixtures'; test('should list files', async ({ runListFiles }) => { @@ -32,7 +31,6 @@ test('should list files', async ({ runListFiles }) => { { name: 'foo', testDir: expect.stringContaining('list-files-should-list-files-playwright-test'), - outputDir: expect.stringContaining(path.join('list-files-should-list-files-playwright-test', 'test-results')), use: {}, files: [ expect.stringContaining('a.test.js') @@ -41,7 +39,6 @@ test('should list files', async ({ runListFiles }) => { { name: 'bar', testDir: expect.stringContaining('list-files-should-list-files-playwright-test'), - outputDir: expect.stringContaining(path.join('list-files-should-list-files-playwright-test', 'test-results')), use: {}, files: [ expect.stringContaining('a.test.js') @@ -68,7 +65,6 @@ test('should include testIdAttribute', async ({ runListFiles }) => { { name: '', testDir: expect.stringContaining('list-files-should-include-testIdAttribute-playwright-test'), - outputDir: expect.stringContaining(path.join('list-files-should-include-testIdAttribute-playwright-test', 'test-results')), use: { testIdAttribute: 'myid' }, diff --git a/tests/playwright-test/ui-mode-test-run.spec.ts b/tests/playwright-test/ui-mode-test-run.spec.ts index b52283bce8..a8e1f8842e 100644 --- a/tests/playwright-test/ui-mode-test-run.spec.ts +++ b/tests/playwright-test/ui-mode-test-run.spec.ts @@ -360,3 +360,37 @@ test('should ignore repeatEach', async ({ runUITest }) => { await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); }); + +test('should remove output folder before test run', async ({ runUITest }) => { + const { page } = await runUITest({ + 'playwright.config.ts': ` + import { defineConfig } from '@playwright/test'; + `, + 'a.test.ts': ` + import fs from 'fs'; + import { test, expect } from '@playwright/test'; + test('should pass', () => { + const path = test.info().outputPath('a.txt'); + expect(fs.existsSync(path)).toBe(false); + fs.writeFileSync(path, 'dirty'); + }); + `, + }); + await expect.poll(dumpTestTree(page)).toContain(` + ▼ ◯ a.test.ts + `); + + await page.getByTitle('Run all').click(); + await expect.poll(dumpTestTree(page)).toBe(` + ▼ ✅ a.test.ts + ✅ should pass + `); + await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); + + await page.getByTitle('Run all').click(); + await expect.poll(dumpTestTree(page)).toBe(` + ▼ ✅ a.test.ts + ✅ should pass + `); + await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); +});