diff --git a/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.spec.ts b/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.spec.ts index 78dfad54e21c6..86cba0f3da021 100644 --- a/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.spec.ts +++ b/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.spec.ts @@ -91,6 +91,7 @@ interface CreateCypressTestProjectOptions { appName: string; appRoot: string; e2eTargetName: string; + legacyExecutor?: boolean; } const defaultCreateCypressTestProjectOptions: CreateCypressTestProjectOptions = @@ -98,6 +99,7 @@ const defaultCreateCypressTestProjectOptions: CreateCypressTestProjectOptions = appName: 'myapp-e2e', appRoot: 'myapp-e2e', e2eTargetName: 'e2e', + legacyExecutor: false, }; function createTestProject( @@ -111,7 +113,9 @@ function createTestProject( projectType: 'application', targets: { [projectOpts.e2eTargetName]: { - executor: '@nx/cypress:cypress', + executor: projectOpts.legacyExecutor + ? '@nrwl/cypress:cypress' + : '@nx/cypress:cypress', options: { cypressConfig: `${projectOpts.appRoot}/cypress.config.ts`, testingType: `e2e`, @@ -217,7 +221,7 @@ describe('Cypress - Convert Executors To Plugin', () => { // project.json modifications const updatedProject = readProjectConfiguration(tree, project.name); const targetKeys = Object.keys(updatedProject.targets); - ['test'].forEach((key) => expect(targetKeys).not.toContain(key)); + expect(targetKeys).not.toContain('test'); // nx.json modifications const nxJsonPlugins = readNxJson(tree).plugins; @@ -305,6 +309,42 @@ describe('Cypress - Convert Executors To Plugin', () => { } }); + it('should setup handle legacy executor', async () => { + // ARRANGE + const project = createTestProject(tree, { + e2eTargetName: 'test', + legacyExecutor: true, + }); + + // ACT + await convertToInferred(tree, { skipFormat: true }); + + // ASSERT + // project.json modifications + const updatedProject = readProjectConfiguration(tree, project.name); + const targetKeys = Object.keys(updatedProject.targets); + ['test'].forEach((key) => expect(targetKeys).not.toContain(key)); + + // nx.json modifications + const nxJsonPlugins = readNxJson(tree).plugins; + const hasCypressPlugin = nxJsonPlugins.find((plugin) => + typeof plugin === 'string' + ? plugin === '@nx/cypress/plugin' + : plugin.plugin === '@nx/cypress/plugin' + ); + expect(hasCypressPlugin).toBeTruthy(); + if (typeof hasCypressPlugin !== 'string') { + [ + ['targetName', 'test'], + ['ciTargetName', 'e2e-ci'], + ].forEach(([targetOptionName, targetName]) => { + expect(hasCypressPlugin.options[targetOptionName]).toEqual( + targetName + ); + }); + } + }); + it('should setup a new Cypress plugin to match only projects migrated', async () => { // ARRANGE const existingProject = createTestProject(tree, { diff --git a/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.ts b/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.ts index c7948f65a7e7e..31d52629b3c65 100644 --- a/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.ts +++ b/packages/cypress/src/generators/convert-to-inferred/convert-to-inferred.ts @@ -21,7 +21,7 @@ interface Schema { export async function convertToInferred(tree: Tree, options: Schema) { const projectGraph = await createProjectGraphAsync(); - await migrateExecutorToPlugin( + let migratedProjects = await migrateExecutorToPlugin( tree, projectGraph, '@nx/cypress:cypress', @@ -35,6 +35,24 @@ export async function convertToInferred(tree: Tree, options: Schema) { options.project ); + migratedProjects += await migrateExecutorToPlugin( + tree, + projectGraph, + '@nrwl/cypress:cypress', + '@nx/cypress/plugin', + (targetName) => ({ + targetName, + ciTargetName: 'e2e-ci', + }), + postTargetTransformer, + createNodes, + options.project + ); + + if (migratedProjects === 0) { + throw new Error('Could not find any targets to migrate.'); + } + if (!options.skipFormat) { await formatFiles(tree); } diff --git a/packages/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator.ts b/packages/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator.ts index 94128583562fb..b09c89001a946 100644 --- a/packages/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator.ts +++ b/packages/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator.ts @@ -74,12 +74,15 @@ class ExecutorToPluginMigrator { this.#skipTargetFilter = skipTargetFilter ?? ((...args) => [false, '']); } - async run(): Promise { + async run(): Promise>> { await this.#init(); - for (const targetName of this.#targetAndProjectsToMigrate.keys()) { - this.#migrateTarget(targetName); + if (this.#targetAndProjectsToMigrate.size > 0) { + for (const targetName of this.#targetAndProjectsToMigrate.keys()) { + this.#migrateTarget(targetName); + } + this.#addPlugins(); } - this.#addPlugins(); + return this.#targetAndProjectsToMigrate; } async #init() { @@ -239,17 +242,6 @@ class ExecutorToPluginMigrator { } } ); - - if (this.#targetAndProjectsToMigrate.size === 0) { - const errorMsg = this.#specificProjectToMigrate - ? `Project "${ - this.#specificProjectToMigrate - }" does not contain any targets using the "${ - this.#executor - }" executor. Please select a project that does.` - : `Could not find any targets using the "${this.#executor}" executor.`; - throw new Error(errorMsg); - } } #getTargetDefaultsForExecutor() { @@ -270,6 +262,10 @@ class ExecutorToPluginMigrator { } async #getCreateNodesResults() { + if (this.#targetAndProjectsToMigrate.size === 0) { + return; + } + for (const targetName of this.#targetAndProjectsToMigrate.keys()) { const loadedPlugin = new LoadedNxPlugin( { @@ -312,7 +308,7 @@ export async function migrateExecutorToPlugin( createNodes: CreateNodes, specificProjectToMigrate?: string, skipTargetFilter?: SkipTargetFilter -): Promise { +): Promise { const migrator = new ExecutorToPluginMigrator( tree, projectGraph, @@ -324,5 +320,6 @@ export async function migrateExecutorToPlugin( specificProjectToMigrate, skipTargetFilter ); - await migrator.run(); + const migratedProjectsAndTargets = await migrator.run(); + return migratedProjectsAndTargets.size; } diff --git a/packages/playwright/src/generators/convert-to-inferred/convert-to-inferred.ts b/packages/playwright/src/generators/convert-to-inferred/convert-to-inferred.ts index 4a833fb6013fc..8e2c0533b74a9 100644 --- a/packages/playwright/src/generators/convert-to-inferred/convert-to-inferred.ts +++ b/packages/playwright/src/generators/convert-to-inferred/convert-to-inferred.ts @@ -16,16 +16,21 @@ interface Schema { export async function convertToInferred(tree: Tree, options: Schema) { const projectGraph = await createProjectGraphAsync(); - await migrateExecutorToPlugin( - tree, - projectGraph, - '@nx/playwright:playwright', - '@nx/playwright/plugin', - (targetName) => ({ targetName, ciTargetName: 'e2e-ci' }), - postTargetTransformer, - createNodes, - options.project - ); + const migratedProjects = + await migrateExecutorToPlugin( + tree, + projectGraph, + '@nx/playwright:playwright', + '@nx/playwright/plugin', + (targetName) => ({ targetName, ciTargetName: 'e2e-ci' }), + postTargetTransformer, + createNodes, + options.project + ); + + if (migratedProjects === 0) { + throw new Error('Could not find any targets to migrate.'); + } if (!options.skipFormat) { await formatFiles(tree);