Skip to content

Commit

Permalink
feat(core): add metadata to targets (#22655)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Apr 5, 2024
1 parent fc8d5ba commit 21f90ca
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 120 deletions.
11 changes: 3 additions & 8 deletions docs/generated/devkit/ProjectConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Project configuration

- [generators](../../devkit/documents/ProjectConfiguration#generators): Object
- [implicitDependencies](../../devkit/documents/ProjectConfiguration#implicitdependencies): string[]
- [metadata](../../devkit/documents/ProjectConfiguration#metadata): Object
- [metadata](../../devkit/documents/ProjectConfiguration#metadata): ProjectMetadata
- [name](../../devkit/documents/ProjectConfiguration#name): string
- [namedInputs](../../devkit/documents/ProjectConfiguration#namedinputs): Object
- [projectType](../../devkit/documents/ProjectConfiguration#projecttype): ProjectType
Expand Down Expand Up @@ -56,14 +56,9 @@ List of projects which are added as a dependency

### metadata

`Optional` **metadata**: `Object`
`Optional` **metadata**: `ProjectMetadata`

#### Type declaration

| Name | Type |
| :-------------- | :------------------------------- |
| `targetGroups?` | `Record`\<`string`, `string`[]\> |
| `technologies?` | `string`[] |
Metadata about the project

---

Expand Down
9 changes: 9 additions & 0 deletions docs/generated/devkit/TargetConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Target's configuration
- [dependsOn](../../devkit/documents/TargetConfiguration#dependson): (string | TargetDependencyConfig)[]
- [executor](../../devkit/documents/TargetConfiguration#executor): string
- [inputs](../../devkit/documents/TargetConfiguration#inputs): (string | InputDefinition)[]
- [metadata](../../devkit/documents/TargetConfiguration#metadata): TargetMetadata
- [options](../../devkit/documents/TargetConfiguration#options): T
- [outputs](../../devkit/documents/TargetConfiguration#outputs): string[]

Expand Down Expand Up @@ -86,6 +87,14 @@ This describes filesets, runtime dependencies and other inputs that a target dep

---

### metadata

`Optional` **metadata**: `TargetMetadata`

Metadata about the target

---

### options

`Optional` **options**: `T`
Expand Down
91 changes: 67 additions & 24 deletions packages/cypress/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from 'cypress';

import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { resetWorkspaceContext } from 'nx/src/utils/workspace-context';
import { join } from 'path';
import { nxE2EPreset } from '../../plugins/cypress-preset';

Expand All @@ -16,9 +17,9 @@ describe('@nx/cypress/plugin', () => {

await tempFs.createFiles({
'package.json': '{}',
'cypress.config.js': '',
'src/test.cy.ts': '',
});
process.chdir(tempFs.tempDir);
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
Expand All @@ -41,6 +42,11 @@ describe('@nx/cypress/plugin', () => {
afterEach(() => {
jest.resetModules();
tempFs.cleanup();
tempFs = null;
});

afterAll(() => {
resetWorkspaceContext();
});

it('should add a target for e2e', async () => {
Expand Down Expand Up @@ -70,11 +76,7 @@ describe('@nx/cypress/plugin', () => {
{
"projects": {
".": {
"metadata": {
"technologies": [
"cypress",
],
},
"metadata": undefined,
"projectType": "application",
"targets": {
"e2e": {
Expand All @@ -94,6 +96,12 @@ describe('@nx/cypress/plugin', () => {
],
},
],
"metadata": {
"description": "Runs Cypress Tests",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
Expand All @@ -104,6 +112,12 @@ describe('@nx/cypress/plugin', () => {
},
"open-cypress": {
"command": "cypress open",
"metadata": {
"description": "Opens Cypress",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
Expand Down Expand Up @@ -140,11 +154,7 @@ describe('@nx/cypress/plugin', () => {
{
"projects": {
".": {
"metadata": {
"technologies": [
"cypress",
],
},
"metadata": undefined,
"projectType": "application",
"targets": {
"component-test": {
Expand All @@ -159,6 +169,12 @@ describe('@nx/cypress/plugin', () => {
],
},
],
"metadata": {
"description": "Runs Cypress Component Tests",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
Expand All @@ -169,6 +185,12 @@ describe('@nx/cypress/plugin', () => {
},
"open-cypress": {
"command": "cypress open",
"metadata": {
"description": "Opens Cypress",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
Expand All @@ -184,16 +206,16 @@ describe('@nx/cypress/plugin', () => {
mockCypressConfig(
defineConfig({
e2e: {
specPattern: '**/*.cy.ts',
videosFolder: './dist/videos',
screenshotsFolder: './dist/screenshots',
...nxE2EPreset('.', {
...nxE2EPreset(join(tempFs.tempDir, 'cypress.config.js'), {
webServerCommands: {
default: 'my-app:serve',
production: 'my-app:serve:production',
},
ciWebServerCommand: 'my-app:serve-static',
}),
specPattern: '**/*.cy.ts',
videosFolder: './dist/videos',
screenshotsFolder: './dist/screenshots',
},
})
);
Expand All @@ -216,9 +238,6 @@ describe('@nx/cypress/plugin', () => {
"e2e-ci",
],
},
"technologies": [
"cypress",
],
},
"projectType": "application",
"targets": {
Expand All @@ -239,12 +258,18 @@ describe('@nx/cypress/plugin', () => {
],
},
],
"metadata": {
"description": "Runs Cypress Tests",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
"outputs": [
"{projectRoot}/dist/cypress/videos",
"{projectRoot}/dist/cypress/screenshots",
"{projectRoot}/dist/videos",
"{projectRoot}/dist/screenshots",
],
},
"e2e-ci": {
Expand All @@ -266,9 +291,15 @@ describe('@nx/cypress/plugin', () => {
],
},
],
"metadata": {
"description": "Runs Cypress Tests in CI",
"technologies": [
"cypress",
],
},
"outputs": [
"{projectRoot}/dist/cypress/videos",
"{projectRoot}/dist/cypress/screenshots",
"{projectRoot}/dist/videos",
"{projectRoot}/dist/screenshots",
],
},
"e2e-ci--src/test.cy.ts": {
Expand All @@ -283,16 +314,28 @@ describe('@nx/cypress/plugin', () => {
],
},
],
"metadata": {
"description": "Runs Cypress Tests in src/test.cy.ts in CI",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
"outputs": [
"{projectRoot}/dist/cypress/videos",
"{projectRoot}/dist/cypress/screenshots",
"{projectRoot}/dist/videos",
"{projectRoot}/dist/screenshots",
],
},
"open-cypress": {
"command": "cypress open",
"metadata": {
"description": "Opens Cypress",
"technologies": [
"cypress",
],
},
"options": {
"cwd": ".",
},
Expand Down
47 changes: 28 additions & 19 deletions packages/cypress/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const createNodes: CreateNodes<CypressPluginOptions> = [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);

const { targets, targetGroups } = targetsCache[hash]
const { targets, metadata } = targetsCache[hash]
? targetsCache[hash]
: await buildCypressTargets(
configFilePath,
Expand All @@ -76,20 +76,14 @@ export const createNodes: CreateNodes<CypressPluginOptions> = [
context
);

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

const project: Omit<ProjectConfiguration, 'root'> = {
projectType: 'application',
targets,
metadata: {
technologies: ['cypress'],
},
metadata,
};

if (targetGroups) {
project.metadata.targetGroups = targetGroups;
}

return {
projects: {
[projectRoot]: project,
Expand Down Expand Up @@ -146,10 +140,7 @@ function getOutputs(
return outputs;
}

interface CypressTargets {
targets: Record<string, TargetConfiguration>;
targetGroups: Record<string, string[]> | null;
}
type CypressTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;

async function buildCypressTargets(
configFilePath: string,
Expand All @@ -173,7 +164,7 @@ async function buildCypressTargets(
const namedInputs = getNamedInputs(projectRoot, context);

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

if ('e2e' in cypressConfig) {
targets[options.targetName] = {
Expand All @@ -182,6 +173,10 @@ async function buildCypressTargets(
cache: true,
inputs: getInputs(namedInputs),
outputs: getOutputs(projectRoot, cypressConfig, 'e2e'),
metadata: {
technologies: ['cypress'],
description: 'Runs Cypress Tests',
},
};

if (webServerCommands?.default) {
Expand Down Expand Up @@ -222,8 +217,8 @@ async function buildCypressTargets(
const inputs = getInputs(namedInputs);

const groupName = 'E2E (CI)';
targetGroups = { [groupName]: [] };
const ciTargetGroup = targetGroups[groupName];
metadata = { targetGroups: { [groupName]: [] } };
const ciTargetGroup = metadata.targetGroups[groupName];
for (const file of specFiles) {
const relativeSpecFilePath = normalizePath(relative(projectRoot, file));
const targetName = options.ciTargetName + '--' + relativeSpecFilePath;
Expand All @@ -237,6 +232,10 @@ async function buildCypressTargets(
options: {
cwd: projectRoot,
},
metadata: {
technologies: ['cypress'],
description: `Runs Cypress Tests in ${relativeSpecFilePath} in CI`,
},
};
dependsOn.push({
target: targetName,
Expand All @@ -251,10 +250,12 @@ async function buildCypressTargets(
inputs,
outputs,
dependsOn,
metadata: {
technologies: ['cypress'],
description: 'Runs Cypress Tests in CI',
},
};
ciTargetGroup.push(options.ciTargetName);
} else {
targetGroups = null;
}
}

Expand All @@ -266,15 +267,23 @@ async function buildCypressTargets(
cache: true,
inputs: getInputs(namedInputs),
outputs: getOutputs(projectRoot, cypressConfig, 'component'),
metadata: {
technologies: ['cypress'],
description: 'Runs Cypress Component Tests',
},
};
}

targets[options.openTargetName] = {
command: `cypress open`,
options: { cwd: projectRoot },
metadata: {
technologies: ['cypress'],
description: 'Opens Cypress',
},
};

return { targets, targetGroups };
return { targets, metadata };
}

function normalizeOptions(options: CypressPluginOptions): CypressPluginOptions {
Expand Down
Loading

0 comments on commit 21f90ca

Please sign in to comment.