From 114edecd3a8fd7d3e777219af9c31291fb94f404 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Tue, 22 Feb 2022 12:50:26 -0800 Subject: [PATCH] fix(list mode): keep outputDir intact (#12291) --- packages/playwright-test/src/runner.ts | 20 +++++++------- tests/playwright-test/list-mode.spec.ts | 35 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts index 5b6546a20d..94e40a0a7e 100644 --- a/packages/playwright-test/src/runner.ts +++ b/packages/playwright-test/src/runner.ts @@ -302,12 +302,7 @@ export class Runner { if (!total) fatalErrors.push(createNoTestsError()); - // 8. Fail when output fails. - await Promise.all(Array.from(outputDirs).map(outputDir => removeFolderAsync(outputDir).catch(e => { - fatalErrors.push(serializeError(e)); - }))); - - // 9. Compute shards. + // 8. Compute shards. let testGroups = createTestGroups(rootSuite); const shard = config.shard; @@ -341,20 +336,27 @@ export class Runner { } (config as any).__testGroupsCount = testGroups.length; - // 10. Report begin + // 9. Report begin this._reporter.onBegin?.(config, rootSuite); - // 11. Bail out on errors prior to running global setup. + // 10. Bail out on errors prior to running global setup. if (fatalErrors.length) { for (const error of fatalErrors) this._reporter.onError?.(error); return { status: 'failed' }; } - // 12. Bail out if list mode only, don't do any work. + // 11. Bail out if list mode only, don't do any work. if (list) return { status: 'passed' }; + // 12. Remove output directores. + try { + await Promise.all(Array.from(outputDirs).map(outputDir => removeFolderAsync(outputDir))); + } catch (e) { + this._reporter.onError?.(serializeError(e)); + return { status: 'failed' }; + } // 13. Run Global setup. let globalTearDown: (() => Promise) | undefined; diff --git a/tests/playwright-test/list-mode.spec.ts b/tests/playwright-test/list-mode.spec.ts index b2bbe5098b..c6eaac536d 100644 --- a/tests/playwright-test/list-mode.spec.ts +++ b/tests/playwright-test/list-mode.spec.ts @@ -15,6 +15,8 @@ */ import { test, expect } from './playwright-test-fixtures'; +import path from 'path'; +import fs from 'fs'; test('should list tests', async ({ runInlineTest }) => { const result = await runInlineTest({ @@ -105,3 +107,36 @@ test('globalSetup and globalTeardown should not run', async ({ runInlineTest }) `Total: 2 tests in 2 files`, ].join('\n')); }); + +test('outputDir should not be removed', async ({ runInlineTest }, testInfo) => { + const outputDir = testInfo.outputPath('dummy-output-dir'); + + const result1 = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { outputDir: ${JSON.stringify(outputDir)} }; + `, + 'a.test.js': ` + const { test } = pwt; + test('my test', async ({}, testInfo) => { + console.log(testInfo.outputDir); + require('fs').writeFileSync(testInfo.outputPath('myfile.txt'), 'hello'); + }); + `, + }, {}, {}, { usesCustomOutputDir: true }); + expect(result1.exitCode).toBe(0); + expect(fs.existsSync(path.join(outputDir, 'a-my-test', 'myfile.txt'))).toBe(true); + + const result2 = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { outputDir: ${JSON.stringify(outputDir)} }; + `, + 'a.test.js': ` + const { test } = pwt; + test('my test', async ({}, testInfo) => { + console.log(testInfo.outputDir); + }); + `, + }, { list: true }, {}, { usesCustomOutputDir: true }); + expect(result2.exitCode).toBe(0); + expect(fs.existsSync(path.join(outputDir, 'a-my-test', 'myfile.txt'))).toBe(true); +});