Skip to content

Commit

Permalink
feat(misc): remove usages of @nx/cypress:cypress-project internally
Browse files Browse the repository at this point in the history
Replace with call to `@nx/cypress:configuration`. The deprecated
generator will be removed in Nx 18.
  • Loading branch information
jaysoo committed Oct 24, 2023
1 parent 1f903ed commit 2603b93
Show file tree
Hide file tree
Showing 29 changed files with 238 additions and 141 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/cypress/generators/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
"enum": ["vite", "webpack", "none"],
"x-prompt": "Which Cypress bundler do you want to use?",
"default": "webpack"
},
"jsx": {
"description": "Whether or not this project uses JSX.",
"type": "boolean",
"default": true
}
},
"required": ["project"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside workspace.json.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"skipPackageJson": {
"type": "boolean",
"default": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
"enum": ["eslint", "none"],
"default": "eslint"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"ciTargetName": {
"type": "string",
"description": "The name of the devServerTarget to use for the Cypress CI configuration. Used to control if using <storybook-project>:static-storybook:ci or <storybook-project>:storybook:ci",
Expand All @@ -49,6 +43,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
"compilerOptions": {
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand All @@ -383,9 +384,12 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
},
"extends": "../../../tsconfig.base.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
"**/*.ts",
"**/*.js",
"cypress.config.ts",
"**/*.cy.ts",
"**/*.cy.js",
"**/*.d.ts",
],
}
`;
Expand Down Expand Up @@ -609,6 +613,7 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
"compilerOptions": {
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand All @@ -623,9 +628,12 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
},
"extends": "../../tsconfig.base.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
"**/*.ts",
"**/*.js",
"cypress.config.ts",
"**/*.cy.ts",
"**/*.cy.js",
"**/*.d.ts",
],
}
`;
Expand Down Expand Up @@ -874,6 +882,7 @@ exports[`app --strict should enable strict type checking: e2e tsconfig.json 1`]
"compilerOptions": {
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand All @@ -888,9 +897,12 @@ exports[`app --strict should enable strict type checking: e2e tsconfig.json 1`]
},
"extends": "../tsconfig.base.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
"**/*.ts",
"**/*.js",
"cypress.config.ts",
"**/*.cy.ts",
"**/*.cy.js",
"**/*.d.ts",
],
}
`;
Expand Down Expand Up @@ -1218,6 +1230,7 @@ exports[`app not nested should generate files: e2e tsconfig.json 1`] = `
"compilerOptions": {
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand All @@ -1232,9 +1245,12 @@ exports[`app not nested should generate files: e2e tsconfig.json 1`] = `
},
"extends": "../tsconfig.base.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
"**/*.ts",
"**/*.js",
"cypress.config.ts",
"**/*.cy.ts",
"**/*.cy.js",
"**/*.d.ts",
],
}
`;
Expand Down
23 changes: 14 additions & 9 deletions packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import {
} from '@nx/devkit';
import { nxVersion } from '../../../utils/versions';
import type { NormalizedSchema } from './normalized-schema';
import { cypressProjectGenerator } from '@nx/cypress';
import { configurationGenerator } from '@nx/cypress';

export async function addE2e(tree: Tree, options: NormalizedSchema) {
if (options.e2eTestRunner === 'cypress') {
// TODO: This can call `@nx/web:static-config` generator when ready
addFileServerTarget(tree, options, 'serve-static');

await cypressProjectGenerator(tree, {
name: options.e2eProjectName,
directory: options.e2eProjectRoot,
// the name and root are already normalized, instruct the generator to use them as is
projectNameAndRootFormat: 'as-provided',
project: options.name,
addProjectConfiguration(tree, options.e2eProjectName, {
projectType: 'application',
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
tags: [],
implicitDependencies: [options.name],
});
await configurationGenerator(tree, {
project: options.e2eProjectName,
directory: 'src',
linter: options.linter,
standaloneConfig: options.standaloneConfig,
skipPackageJson: options.skipPackageJson,
skipFormat: true,
devServerTarget: `${options.name}:serve:development`,
});
} else if (options.e2eTestRunner === 'playwright') {
const { configurationGenerator: playwrightConfigurationGenerator } =
Expand All @@ -35,6 +39,7 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
nxVersion
);
addProjectConfiguration(tree, options.e2eProjectName, {
projectType: 'application',
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { librarySecondaryEntryPointGenerator } from '../library-secondary-entry-
import { generateTestApplication, generateTestLibrary } from '../utils/testing';
import { cypressComponentConfiguration } from './cypress-component-configuration';

let projectGraph: ProjectGraph;
let projectGraph: ProjectGraph = { nodes: {}, dependencies: {} };
jest.mock('@nx/cypress/src/utils/cypress-version');
jest.mock('@nx/devkit', () => ({
...jest.requireActual<any>('@nx/devkit'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ export class E2eMigrator extends ProjectMigrator<SupportedTargets> {
private async migrateCypressE2eProject(): Promise<void> {
const oldCypressConfigFilePath = this.getOldCypressConfigFilePath();

// TODO(v18): This needs to be removed before v18 since the generator is going away.
await cypressProjectGenerator(this.tree, {
name: this.project.name,
project: this.appName,
linter: this.isProjectUsingEsLint ? Linter.EsLint : Linter.None,
standaloneConfig: true,
skipFormat: true,
});

Expand Down
9 changes: 7 additions & 2 deletions packages/cypress/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { configurationGenerator } from './src/generators/configuration/configuration';
import { componentConfigurationGenerator } from './src/generators/component-configuration/component-configuration';
import { cypressProjectGenerator as _cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';

export { configurationGenerator, componentConfigurationGenerator };

// Maintain backwards compatibility with the old names in case community plugins used them.
/** @deprecated Use `configurationGenerator` instead. */
// TODO(v18): Remove old name
/** @deprecated Use `configurationGenerator` instead. It will be removed in Nx 18. */
export const cypressComponentConfiguration = componentConfigurationGenerator;

export { configurationGenerator as cypressE2EConfigurationGenerator };
export { cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';
// TODO(v18): Remove project generator
/** @deprecated Add a new project and call `configurationGenerator` instead. It will be removed in Nx 18. */
export const cypressProjectGenerator = _cypressProjectGenerator;
export { cypressInitGenerator } from './src/generators/init/init';
export { migrateCypressProject } from './src/generators/migrate-to-cypress-11/migrate-to-cypress-11';
2 changes: 2 additions & 0 deletions packages/cypress/src/generators/base-setup/base-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CypressBaseSetupSchema {
* default is `cypress`
* */
directory?: string;
jsx?: boolean;
}

export function addBaseCypressSetup(
Expand All @@ -33,6 +34,7 @@ export function addBaseCypressSetup(

generateFiles(tree, join(__dirname, 'files'), projectConfig.root, {
...opts,
jsx: !!opts.jsx,
offsetFromRoot: offsetFromRoot(projectConfig.root),
offsetFromProjectRoot: opts.hasTsConfig ? opts.offsetFromProjectRoot : '',
tsConfigPath: opts.hasTsConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"**/*.js",
"<%= offsetFromProjectRoot %>cypress.config.ts",
"<%= offsetFromProjectRoot %>**/*.cy.ts",
"<%= offsetFromProjectRoot %>**/*.cy.tsx",
<% if (jsx) { %> "<%= offsetFromProjectRoot %>**/*.cy.tsx",<% } %>
"<%= offsetFromProjectRoot %>**/*.cy.js",
"<%= offsetFromProjectRoot %>**/*.cy.jsx",
<% if (jsx) { %>"<%= offsetFromProjectRoot %>**/*.cy.jsx",<% } %>
"<%= offsetFromProjectRoot %>**/*.d.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function addProjectFiles(
addBaseCypressSetup(tree, {
project: opts.project,
directory: opts.directory,
jsx: opts.jsx,
});

generateFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface CypressComponentConfigurationSchema {
skipFormat: boolean;
directory?: string;
bundler?: 'webpack' | 'vite';
jsx?: boolean;
}
27 changes: 19 additions & 8 deletions packages/cypress/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CypressE2EConfigSchema {
devServerTarget?: string;
linter?: Linter;
port?: number | 'cypress-auto';
jsx?: boolean;
}

type NormalizedSchema = ReturnType<typeof normalizeOptions>;
Expand All @@ -55,10 +56,12 @@ export async function configurationGenerator(
}
await addFiles(tree, opts);
addTarget(tree, opts);
addLinterToCyProject(tree, {

const linterTask = await addLinterToCyProject(tree, {
...opts,
cypressDir: opts.directory,
});
tasks.push(linterTask);

if (!opts.skipFormat) {
await formatFiles(tree);
Expand Down Expand Up @@ -132,6 +135,7 @@ async function addFiles(tree: Tree, options: NormalizedSchema) {
addBaseCypressSetup(tree, {
project: options.project,
directory: options.directory,
jsx: options.jsx,
});

const cyFile = joinPathFragments(projectConfig.root, 'cypress.config.ts');
Expand Down Expand Up @@ -197,18 +201,25 @@ function addTarget(tree: Tree, opts: NormalizedSchema) {
port: opts.port,
};

projectConfig.targets.e2e.configurations = {
[parsedTarget.configuration || 'production']: {
devServerTarget: `${opts.devServerTarget}${
parsedTarget.configuration ? '' : ':production'
}`,
},
};
const devServerProjectConfig = readProjectConfiguration(
tree,
parsedTarget.project
);
// Add production e2e target if serve target is found
if (
parsedTarget.configuration !== 'production' &&
devServerProjectConfig.targets?.[parsedTarget.target]?.configurations?.[
'production'
]
) {
projectConfig.targets.e2e.configurations ??= {};
projectConfig.targets.e2e.configurations['production'] = {
devServerTarget: `${parsedTarget.project}:${parsedTarget.target}:production`,
};
}
// Add ci/static e2e target if serve target is found
if (devServerProjectConfig.targets?.['serve-static']) {
projectConfig.targets.e2e.configurations ??= {};
projectConfig.targets.e2e.configurations.ci = {
devServerTarget: `${parsedTarget.project}:serve-static`,
};
Expand Down
5 changes: 5 additions & 0 deletions packages/cypress/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"enum": ["vite", "webpack", "none"],
"x-prompt": "Which Cypress bundler do you want to use?",
"default": "webpack"
},
"jsx": {
"description": "Whether or not this project uses JSX.",
"type": "boolean",
"default": true
}
},
"required": ["project"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export interface Schema {
js?: boolean;
skipFormat?: boolean;
setParserOptionsProject?: boolean;
standaloneConfig?: boolean;
skipPackageJson?: boolean;
bundler?: 'webpack' | 'vite' | 'none';
}
6 changes: 0 additions & 6 deletions packages/cypress/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside workspace.json.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"skipPackageJson": {
"type": "boolean",
"default": false,
Expand Down
20 changes: 13 additions & 7 deletions packages/next/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import { NormalizedSchema } from './normalize-options';

export async function addE2e(host: Tree, options: NormalizedSchema) {
if (options.e2eTestRunner === 'cypress') {
const { cypressProjectGenerator } = ensurePackage<
const { configurationGenerator } = ensurePackage<
typeof import('@nx/cypress')
>('@nx/cypress', nxVersion);
return cypressProjectGenerator(host, {
addProjectConfiguration(host, options.e2eProjectName, {
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
tags: [],
implicitDependencies: [options.projectName],
});
return configurationGenerator(host, {
...options,
linter: Linter.EsLint,
name: options.e2eProjectName,
directory: options.e2eProjectRoot,
// the name and root are already normalized, instruct the generator to use them as is
projectNameAndRootFormat: 'as-provided',
project: options.projectName,
project: options.e2eProjectName,
directory: 'src',
skipFormat: true,
devServerTarget: `${options.projectName}:serve`,
jsx: true,
});
} else if (options.e2eTestRunner === 'playwright') {
const { configurationGenerator } = ensurePackage<
Expand Down
Loading

0 comments on commit 2603b93

Please sign in to comment.