Skip to content

Commit

Permalink
cherry-pick(#12291): fix(list mode): keep outputDir intact (#12293)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Feb 22, 2022
1 parent 5b17ca9 commit 2e41670
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/playwright-test/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<void>) | undefined;
Expand Down
35 changes: 35 additions & 0 deletions tests/playwright-test/list-mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
});

0 comments on commit 2e41670

Please sign in to comment.