diff --git a/test/playwright/localTest.ts b/test/playwright/localTest.ts index a519be25529..9950d8c63e1 100644 --- a/test/playwright/localTest.ts +++ b/test/playwright/localTest.ts @@ -10,7 +10,7 @@ import archiver from 'archiver'; import * as url from 'url'; import getPort from 'get-port'; import * as execa from 'execa'; -import { PageScreenshotOptions, test as baseTest } from './test'; +import { PageScreenshotOptions, WorkerInfo, test as baseTest } from './test'; import { waitForMatch } from '../utils/streams'; import { asyncDisposeSymbol, using } from '../utils/resources'; @@ -58,9 +58,23 @@ interface LocalServerConfig { env?: Record; base?: string; } +export async function getTemporaryDir() { + const tmpDir = await fs.mkdtemp(path.resolve(currentDirectory, './tmp-')); -export async function getTemporaryDir({ template, setup }: ProjectConfig = {}) { - const tmpTestDir = await fs.mkdtemp(path.resolve(currentDirectory, './tmp-')); + return { + path: tmpDir, + [asyncDisposeSymbol]: async () => { + await fs.rm(tmpDir, { recursive: true, maxRetries: 3, retryDelay: 1000 }); + }, + }; +} + +async function getTestFixtureTempDir( + workerInfo: WorkerInfo, + { template, setup }: ProjectConfig = {}, +) { + const tmpTestDir = path.resolve(currentDirectory, `./.tmp-test-dir-${workerInfo.parallelIndex}`); + await fs.mkdir(tmpTestDir); // Each test runs in its own temporary folder to avoid race conditions when running tests in parallel. // It also avoids mutating the source code of the fixture while running the test. const projectDir = path.resolve(tmpTestDir, './fixture'); @@ -306,8 +320,8 @@ const test = baseTest.extend< customServerConfig: [undefined, { option: true, scope: 'worker' }], projectConfig: [undefined, { option: true, scope: 'worker' }], projectDir: [ - async ({ projectConfig }, use) => { - await using(await getTemporaryDir(projectConfig), async (projectDir) => { + async ({ projectConfig }, use, workerInfo) => { + await using(await getTestFixtureTempDir(workerInfo, projectConfig), async (projectDir) => { await use(projectDir); }); },