Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec(playwright): Add test generator #2141

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 54 additions & 7 deletions src/playwright.ts
@@ -1,7 +1,50 @@
const testsGenerator: Fig.Generator = {
custom: async (tokens, executeShellCommand) => {
// TODO: load the list of test files specified in testDir on playwright.config file
return [] as Fig.Suggestion[];
custom: async (token, executeShellCommand) => {
const basePathSearch = await executeShellCommand({
command: "bash",
args: ["-c", "cat playwright.config.ts | grep testDir"],
});

const dir = basePathSearch.stdout.match(/(['"])([./\w]+)(['"])/);
const baseDir = dir ? dir[2] : "tests";

// TODO: ideally do something along the lines of "if bunx exists use that otherwise use npx"
const tests = await executeShellCommand({
command: "npx",
args: ["playwright", "test", "--list"],
});

if (tests.status !== 0) {
return [];
}

const lines = tests.stdout
.split("\n")
.slice(1, -1)
.map((line) => line.split(" › ").slice(1))
.reduce<Record<string, Fig.Suggestion>>((acc, curr) => {
const [testPath, testLabel] = curr;
const withoutSpecifier = testPath.split(":", 1)[0];
if (!acc[withoutSpecifier]) {
acc[withoutSpecifier] = {
priority: 80,
name: `${baseDir}/${withoutSpecifier}`,
displayName: withoutSpecifier,
description: `Run all tests in ${withoutSpecifier}`,
};
}

acc[testPath] = {
priority: 80,
name: `${baseDir}/${testPath}`,
displayName: `${testPath} - ${testLabel}`,
description: `Run ${testLabel}`,
};

return acc;
}, {});

return Object.values(lines);
},
};

Expand Down Expand Up @@ -48,7 +91,9 @@ const helpOption: Fig.Option = {

const completionSpec: Fig.Spec = {
name: "playwright",
description: "",
icon: "https://playwright.dev/img/playwright-logo.svg",
description:
"Playwright enables reliable end-to-end testing for modern web apps",
subcommands: [
{
name: "test",
Expand All @@ -58,7 +103,7 @@ const completionSpec: Fig.Spec = {
description: "Test files to run",
isOptional: true,
isVariadic: true,
template: ["filepaths", "folders"],
generators: [{ template: ["filepaths", "folders"] }, testsGenerator],
},
options: [
{
Expand All @@ -72,7 +117,10 @@ const completionSpec: Fig.Spec = {
name: "--headed",
description: "Run tests in headed browsers",
},
helpOption,
{
name: "--list",
description: "List all tests in the project",
},
],
},
{
Expand All @@ -90,7 +138,6 @@ const completionSpec: Fig.Spec = {
name: "--with-deps",
description: "Install system dependencies for browsers",
},
helpOption,
],
},
],
Expand Down