From 13ac3bb1acccb7df82d582b68f55139956f3657d Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 2 May 2024 19:23:06 +0100 Subject: [PATCH] fix(testing): convert-to-inferred generator should handle legacy cypress executor --- .../convert-to-inferred.spec.ts | 42 ++++++++++- .../convert-to-inferred.ts | 75 +++++++++++++++---- .../executor-to-plugin-migrator.ts | 6 +- 3 files changed, 108 insertions(+), 15 deletions(-) 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 78dfad54e21c66..60e9dbb98a1a3e 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`, @@ -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 c7948f65a7e7e8..433f8daecb2fef 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,19 +21,68 @@ interface Schema { export async function convertToInferred(tree: Tree, options: Schema) { const projectGraph = await createProjectGraphAsync(); - await migrateExecutorToPlugin( - tree, - projectGraph, - '@nx/cypress:cypress', - '@nx/cypress/plugin', - (targetName) => ({ - targetName, - ciTargetName: 'e2e-ci', - }), - postTargetTransformer, - createNodes, - options.project - ); + let projectIsUsingLegacy = + options.project && + Object.values(projectGraph.nodes[options.project].data.targets).some( + (target) => target.executor === '@nrwl/cypress:cypress' + ); + if (options.project) { + if (projectIsUsingLegacy) { + await migrateExecutorToPlugin( + tree, + projectGraph, + '@nrwl/cypress:cypress', + '@nx/cypress/plugin', + (targetName) => ({ + targetName, + ciTargetName: 'e2e-ci', + }), + postTargetTransformer, + createNodes, + options.project + ); + } else { + await migrateExecutorToPlugin( + tree, + projectGraph, + '@nx/cypress:cypress', + '@nx/cypress/plugin', + (targetName) => ({ + targetName, + ciTargetName: 'e2e-ci', + }), + postTargetTransformer, + createNodes, + options.project + ); + } + } else { + await migrateExecutorToPlugin( + tree, + projectGraph, + '@nx/cypress:cypress', + '@nx/cypress/plugin', + (targetName) => ({ + targetName, + ciTargetName: 'e2e-ci', + }), + postTargetTransformer, + createNodes + ); + + await migrateExecutorToPlugin( + tree, + projectGraph, + '@nrwl/cypress:cypress', + '@nx/cypress/plugin', + (targetName) => ({ + targetName, + ciTargetName: 'e2e-ci', + }), + postTargetTransformer, + createNodes + ); + } 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 94128583562fb7..a4414dbd8e639d 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 @@ -248,7 +248,11 @@ class ExecutorToPluginMigrator { this.#executor }" executor. Please select a project that does.` : `Could not find any targets using the "${this.#executor}" executor.`; - throw new Error(errorMsg); + if (this.#specificProjectToMigrate) { + throw new Error(errorMsg); + } else if (this.#executor.startsWith('@nx')) { + console.warn(errorMsg); + } } }