Skip to content

Commit

Permalink
feat(testing): add metadata to playwright targets
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 10, 2024
1 parent 4cd1808 commit a12d457
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 19 deletions.
65 changes: 65 additions & 0 deletions packages/playwright/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe('@nx/playwright/plugin', () => {
expect(projects).toMatchInlineSnapshot(`
{
".": {
"metadata": {
"targetGroups": {
"E2E (CI)": [
"e2e-ci",
],
},
},
"root": ".",
"targets": {
"e2e": {
Expand All @@ -55,6 +62,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests",
"technologies": [
"playwright",
],
},
"options": {
"cwd": "{projectRoot}",
},
Expand All @@ -70,6 +83,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests in CI",
"technologies": [
"playwright",
],
},
"outputs": [
"{projectRoot}/test-results",
],
Expand Down Expand Up @@ -99,6 +118,13 @@ describe('@nx/playwright/plugin', () => {
expect(projects).toMatchInlineSnapshot(`
{
".": {
"metadata": {
"targetGroups": {
"E2E (CI)": [
"e2e-ci",
],
},
},
"root": ".",
"targets": {
"e2e": {
Expand All @@ -108,6 +134,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests",
"technologies": [
"playwright",
],
},
"options": {
"cwd": "{projectRoot}",
},
Expand All @@ -126,6 +158,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests in CI",
"technologies": [
"playwright",
],
},
"outputs": [
"{projectRoot}/playwright-report",
"{projectRoot}/test-results/report.json",
Expand Down Expand Up @@ -164,6 +202,15 @@ describe('@nx/playwright/plugin', () => {
context
);
const { targets } = projects['.'];
expect(projects['.'].metadata.targetGroups).toMatchInlineSnapshot(`
{
"E2E (CI)": [
"e2e-ci--tests/run-me-2.spec.ts",
"e2e-ci--tests/run-me.spec.ts",
"e2e-ci",
],
}
`);
expect(targets['e2e-ci']).toMatchInlineSnapshot(`
{
"cache": true,
Expand All @@ -184,6 +231,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests in CI",
"technologies": [
"playwright",
],
},
"outputs": [
"{projectRoot}/test-results",
],
Expand All @@ -197,6 +250,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests in tests/run-me.spec.ts in CI",
"technologies": [
"playwright",
],
},
"options": {
"cwd": "{projectRoot}",
},
Expand All @@ -213,6 +272,12 @@ describe('@nx/playwright/plugin', () => {
"default",
"^production",
],
"metadata": {
"description": "Runs Playwright Tests in tests/run-me-2.spec.ts in CI",
"technologies": [
"playwright",
],
},
"options": {
"cwd": "{projectRoot}",
},
Expand Down
53 changes: 34 additions & 19 deletions packages/playwright/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
detectPackageManager,
joinPathFragments,
normalizePath,
ProjectConfiguration,
readJsonFile,
TargetConfiguration,
writeJsonFile,
Expand Down Expand Up @@ -36,21 +37,15 @@ const cachePath = join(projectGraphCacheDirectory, 'playwright.hash');

const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};

const calculatedTargets: Record<
string,
Record<string, TargetConfiguration>
> = {};
type PlaywrightTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;

function readTargetsCache(): Record<
string,
Record<string, TargetConfiguration>
> {
const calculatedTargets: Record<string, PlaywrightTargets> = {};

function readTargetsCache(): Record<string, PlaywrightTargets> {
return readJsonFile(cachePath);
}

function writeTargetsToCache(
targets: Record<string, Record<string, TargetConfiguration>>
) {
function writeTargetsToCache(targets: Record<string, PlaywrightTargets>) {
writeJsonFile(cachePath, targets);
}

Expand Down Expand Up @@ -79,7 +74,7 @@ export const createNodes: CreateNodes<PlaywrightPluginOptions> = [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);

const targets =
const { targets, metadata } =
targetsCache[hash] ??
(await buildPlaywrightTargets(
configFilePath,
Expand All @@ -88,13 +83,14 @@ export const createNodes: CreateNodes<PlaywrightPluginOptions> = [
context
));

calculatedTargets[hash] = targets;
calculatedTargets[hash] = { targets, metadata };

return {
projects: {
[projectRoot]: {
root: projectRoot,
targets,
metadata,
},
},
};
Expand All @@ -106,7 +102,7 @@ async function buildPlaywrightTargets(
projectRoot: string,
options: NormalizedOptions,
context: CreateNodesContext
) {
): Promise<PlaywrightTargets> {
// Playwright forbids importing the `@playwright/test` module twice. This would affect running the tests,
// but we're just reading the config so let's delete the variable they are using to detect this.
// See: https://github.com/microsoft/playwright/pull/11218/files
Expand All @@ -118,13 +114,18 @@ async function buildPlaywrightTargets(

const namedInputs = getNamedInputs(projectRoot, context);

const targets: Record<string, TargetConfiguration<unknown>> = {};
const targets: ProjectConfiguration['targets'] = {};
let metadata: ProjectConfiguration['metadata'];

const baseTargetConfig: TargetConfiguration = {
command: 'playwright test',
options: {
cwd: '{projectRoot}',
},
metadata: {
technologies: ['playwright'],
description: 'Runs Playwright Tests',
},
};

targets[options.targetName] = {
Expand All @@ -148,6 +149,10 @@ async function buildPlaywrightTargets(
outputs: getOutputs(projectRoot, playwrightConfig),
};

const groupName = 'E2E (CI)';
metadata = { targetGroups: { [groupName]: [] } };
const ciTargetGroup = metadata.targetGroups[groupName];

const testDir = playwrightConfig.testDir
? joinPathFragments(projectRoot, playwrightConfig.testDir)
: projectRoot;
Expand All @@ -158,13 +163,18 @@ async function buildPlaywrightTargets(
const dependsOn: TargetConfiguration['dependsOn'] = [];
forEachTestFile(
(testFile) => {
const relativeToProjectRoot = normalizePath(
const relativeSpecFilePath = normalizePath(
relative(projectRoot, testFile)
);
const targetName = `${options.ciTargetName}--${relativeToProjectRoot}`;
const targetName = `${options.ciTargetName}--${relativeSpecFilePath}`;
ciTargetGroup.push(targetName);
targets[targetName] = {
...ciBaseTargetConfig,
command: `${baseTargetConfig.command} ${relativeToProjectRoot}`,
command: `${baseTargetConfig.command} ${relativeSpecFilePath}`,
metadata: {
technologies: ['playwright'],
description: `Runs Playwright Tests in ${relativeSpecFilePath} in CI`,
},
};
dependsOn.push({
target: targetName,
Expand All @@ -187,10 +197,15 @@ async function buildPlaywrightTargets(
inputs: ciBaseTargetConfig.inputs,
outputs: ciBaseTargetConfig.outputs,
dependsOn,
metadata: {
technologies: ['playwright'],
description: 'Runs Playwright Tests in CI',
},
};
ciTargetGroup.push(options.ciTargetName);
}

return targets;
return { targets, metadata };
}

async function forEachTestFile(
Expand Down

0 comments on commit a12d457

Please sign in to comment.