cherry-pick(#12291): fix(list mode): keep outputDir intact (#12293)

This commit is contained in:
Dmitry Gozman 2022-02-22 13:39:56 -08:00 committed by GitHub
parent 5b17ca9d56
commit 2e4167027b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 9 deletions

View file

@ -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<void>) | undefined;

View file

@ -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);
});