Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test(cli): setup files are in list-files and test --list output (#18890)

Yury Semikhatsky0f4b67bc6dd6
diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts
index f376e71352..0847c9597b 100644
--- a/packages/playwright-test/src/runner.ts
+++ b/packages/playwright-test/src/runner.ts
@@ -203,7 +203,7 @@ export class Runner {
for (const [project, files] of filesByProject) {
report.projects.push({
...sanitizeConfigForJSON(project, new Set()),
- files: files,
+ files
});
}
return report;
diff --git a/tests/playwright-test/playwright-test-fixtures.ts b/tests/playwright-test/playwright-test-fixtures.ts
index b32d5063d8..e925da62c2 100644
--- a/tests/playwright-test/playwright-test-fixtures.ts
+++ b/tests/playwright-test/playwright-test-fixtures.ts
@@ -30,6 +30,11 @@ import { test as base } from './stable-test-runner';
const removeFolderAsync = promisify(rimraf);
+export type CliRunResult = {
+ exitCode: number,
+ output: string,
+};
+
export type RunResult = {
exitCode: number,
output: string,
@@ -113,7 +118,7 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
}
const outputDir = path.join(baseDir, 'test-results');
const reportFile = path.join(outputDir, 'report.json');
- const args = ['node', cliEntrypoint, 'test'];
+ const args = ['test'];
if (!options.usesCustomOutputDir)
args.push('--output=' + outputDir);
if (!options.usesCustomReporters)
@@ -124,52 +129,20 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
);
if (options.additionalArgs)
args.push(...options.additionalArgs);
- const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'playwright-test-cache-'));
- const testProcess = childProcess({
- command: args,
- env: {
- ...process.env,
- PLAYWRIGHT_JSON_OUTPUT_NAME: reportFile,
- PWTEST_CACHE_DIR: cacheDir,
- // BEGIN: Reserved CI
- CI: undefined,
- BUILD_URL: undefined,
- CI_COMMIT_SHA: undefined,
- CI_JOB_URL: undefined,
- CI_PROJECT_URL: undefined,
- GITHUB_REPOSITORY: undefined,
- GITHUB_RUN_ID: undefined,
- GITHUB_SERVER_URL: undefined,
- GITHUB_SHA: undefined,
- // END: Reserved CI
- PW_TEST_HTML_REPORT_OPEN: undefined,
- PW_TEST_REPORTER: undefined,
- PW_TEST_REPORTER_WS_ENDPOINT: undefined,
- PW_TEST_SOURCE_TRANSFORM: undefined,
- PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined,
- PW_OUT_OF_PROCESS_DRIVER: undefined,
- NODE_OPTIONS: undefined,
- ...env,
- },
- cwd: options.cwd ? path.resolve(baseDir, options.cwd) : baseDir,
- });
- let didSendSigint = false;
- testProcess.onOutput = () => {
- if (options.sendSIGINTAfter && !didSendSigint && countTimes(testProcess.output, '%%SEND-SIGINT%%') >= options.sendSIGINTAfter) {
- didSendSigint = true;
- process.kill(testProcess.process.pid!, 'SIGINT');
- }
- };
- const { exitCode } = await testProcess.exited;
- await removeFolderAsync(cacheDir);
- const outputString = testProcess.output.toString();
+ const cwd = options.cwd ? path.resolve(baseDir, options.cwd) : baseDir;
+ // eslint-disable-next-line prefer-const
+ let { exitCode, output } = await runPlaywrightCommand(childProcess, cwd, args, {
+ PLAYWRIGHT_JSON_OUTPUT_NAME: reportFile,
+ ...env,
+ }, options.sendSIGINTAfter);
+
const summary = (re: RegExp) => {
let result = 0;
- let match = re.exec(outputString);
+ let match = re.exec(output);
while (match) {
result += (+match[1]);
- match = re.exec(outputString);
+ match = re.exec(output);
}
return result;
};
@@ -182,7 +155,7 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
try {
report = JSON.parse(fs.readFileSync(reportFile).toString());
} catch (e) {
- testProcess.output += '\n' + e.toString();
+ output += '\n' + e.toString();
}
const results: JSONReportTestResult[] = [];
@@ -202,7 +175,7 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
return {
exitCode,
- output: testProcess.output,
+ output,
passed,
failed,
flaky,
@@ -213,6 +186,51 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
};
}
+async function runPlaywrightCommand(childProcess: CommonFixtures['childProcess'], cwd: string, commandWithArguments: string[], env: Env, sendSIGINTAfter?: number): Promise<CliRunResult> {
+ const command = ['node', cliEntrypoint];
+ command.push(...commandWithArguments);
+ const cacheDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-test-cache-'));
+ const testProcess = childProcess({
+ command,
+ env: {
+ ...process.env,
+ PWTEST_CACHE_DIR: cacheDir,
+ // BEGIN: Reserved CI
+ CI: undefined,
+ BUILD_URL: undefined,
+ CI_COMMIT_SHA: undefined,
+ CI_JOB_URL: undefined,
+ CI_PROJECT_URL: undefined,
+ GITHUB_REPOSITORY: undefined,
+ GITHUB_RUN_ID: undefined,
+ GITHUB_SERVER_URL: undefined,
+ GITHUB_SHA: undefined,
+ // END: Reserved CI
+ PW_TEST_HTML_REPORT_OPEN: undefined,
+ PW_TEST_REPORTER: undefined,
+ PW_TEST_REPORTER_WS_ENDPOINT: undefined,
+ PW_TEST_SOURCE_TRANSFORM: undefined,
+ PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined,
+ PW_OUT_OF_PROCESS_DRIVER: undefined,
+ NODE_OPTIONS: undefined,
+ ...env,
+ },
+ cwd,
+ });
+ let didSendSigint = false;
+ testProcess.onOutput = () => {
+ if (sendSIGINTAfter && !didSendSigint && countTimes(testProcess.output, '%%SEND-SIGINT%%') >= sendSIGINTAfter) {
+ didSendSigint = true;
+ process.kill(testProcess.process.pid!, 'SIGINT');
+ }
+ };
+ const { exitCode } = await testProcess.exited;
+ await removeFolderAsync(cacheDir);
+
+ return { exitCode, output: testProcess.output.toString() };
+}
+
+
type RunOptions = {
sendSIGINTAfter?: number;
usesCustomOutputDir?: boolean;
@@ -226,6 +244,7 @@ type Fixtures = {
runTSC: (files: Files) => Promise<TSCResult>;
nodeVersion: { major: number, minor: number, patch: number };
runGroups: (files: Files, params?: Params, env?: Env, options?: RunOptions) => Promise<{ timeline: { titlePath: string[], event: 'begin' | 'end' }[] } & RunResult>;
+ runCommand: (files: Files, args: string[]) => Promise<CliRunResult>;
};
export const test = base
@@ -245,6 +264,13 @@ export const test = base
});
},
+ runCommand: async ({ childProcess }, use, testInfo: TestInfo) => {
+ await use(async (files: Files, args: string[]) => {
+ const baseDir = await writeFiles(testInfo, files);
+ return await runPlaywrightCommand(childProcess, baseDir, args, { });
+ });
+ },
+
runTSC: async ({ childProcess }, use, testInfo) => {
await use(async files => {
const baseDir = await writeFiles(testInfo, { 'tsconfig.json': JSON.stringify(TSCONFIG), ...files });
diff --git a/tests/playwright-test/project-setup.spec.ts b/tests/playwright-test/project-setup.spec.ts
index 55044ed13d..fe5c818afa 100644
--- a/tests/playwright-test/project-setup.spec.ts
+++ b/tests/playwright-test/project-setup.spec.ts
@@ -328,3 +328,100 @@ test('same file cannot be a setup and a test in different projects', async ({ ru
expect(exitCode).toBe(1);
expect(output).toContain(`a.test.ts" matches 'setup' filter in project "p1" and 'testMatch' filter in project "p2"`);
});
+
+test('list-files should enumerate setup files in same group', async ({ runCommand }, testInfo) => {
+ const files = {
+ 'playwright.config.ts': `
+ module.exports = {
+ projects: [
+ {
+ name: 'p1',
+ setup: /.*a..setup.ts$/,
+ testMatch: /.*a.test.ts$/,
+ },
+ {
+ name: 'p2',
+ setup: /.*b.setup.ts$/,
+ testMatch: /.*b.test.ts$/
+ },
+ ]
+ };`,
+ 'a1.setup.ts': `
+ const { test } = pwt;
+ test('test1', async () => { });
+ `,
+ 'a2.setup.ts': `
+ const { test } = pwt;
+ test('test1', async () => { });
+ `,
+ 'a.test.ts': `
+ const { test } = pwt;
+ test('test2', async () => { });
+ `,
+ 'b.setup.ts': `
+ const { test } = pwt;
+ test('test3', async () => { });
+ `,
+ 'b.test.ts': `
+ const { test } = pwt;
+ test('test4', async () => { });
+ `,
+ };
+
+ const { exitCode, output } = await runCommand(files, ['list-files']);
+ expect(exitCode).toBe(0);
+ const json = JSON.parse(output);
+ expect(json.projects.map(p => p.name)).toEqual(['p1', 'p2']);
+ expect(json.projects[0].files.map(f => path.basename(f))).toEqual(['a.test.ts', 'a1.setup.ts', 'a2.setup.ts']);
+ expect(json.projects[1].files.map(f => path.basename(f))).toEqual(['b.setup.ts', 'b.test.ts']);
+});
+
+test('test --list should enumerate setup tests as regular ones', async ({ runCommand }, testInfo) => {
+ const files = {
+ 'playwright.config.ts': `
+ module.exports = {
+ projects: [
+ {
+ name: 'p1',
+ setup: /.*a..setup.ts$/,
+ testMatch: /.*a.test.ts$/,
+ },
+ {
+ name: 'p2',
+ setup: /.*b.setup.ts$/,
+ testMatch: /.*b.test.ts$/
+ },
+ ]
+ };`,
+ 'a1.setup.ts': `
+ const { test } = pwt;
+ test('test1', async () => { });
+ `,
+ 'a2.setup.ts': `
+ const { test } = pwt;
+ test('test1', async () => { });
+ `,
+ 'a.test.ts': `
+ const { test } = pwt;
+ test('test2', async () => { });
+ `,
+ 'b.setup.ts': `
+ const { test } = pwt;
+ test('test3', async () => { });
+ `,
+ 'b.test.ts': `
+ const { test } = pwt;
+ test('test4', async () => { });
+ `,
+ };
+
+ const { exitCode, output } = await runCommand(files, ['test', '--list']);
+ expect(exitCode).toBe(0);
+ expect(output).toContain(`Listing tests:
+ [p1] › a.test.ts:6:7 › test2
+ [p1] › a1.setup.ts:5:7 › test1
+ [p1] › a2.setup.ts:5:7 › test1
+ [p2] › b.setup.ts:5:7 › test3
+ [p2] › b.test.ts:6:7 › test4
+Total: 5 tests in 5 files`);
+});