From bc0566f4c3d7785d05659cb6eccf03531e489995 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 17 Jan 2025 15:09:40 +0000 Subject: [PATCH] fix(testing): e2e config generators should prompt for missing serve data (#29660) ## Current Behavior When running the `configuration` generator from `@nx/playwright` and `@nx/cypress` for existing appliactions - they currently generate a config that is likely to be invalid becaue there is not enough information to correctly assume the port, target and baseUrl for the application. This data is provided by the application generators that call the `configuration` generator - but that means that users running it manually must remember to pass the information via flags. ## Expected Behavior If the serve data is missing, prompt the user to input the correct information to ensure the config generation is as accurate as possible. --- .../generators/configuration/configuration.ts | 45 +++++++++++++++++++ .../generators/configuration/configuration.ts | 39 ++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/packages/cypress/src/generators/configuration/configuration.ts b/packages/cypress/src/generators/configuration/configuration.ts index 3671a480b416a..b42c025bae0cc 100644 --- a/packages/cypress/src/generators/configuration/configuration.ts +++ b/packages/cypress/src/generators/configuration/configuration.ts @@ -39,6 +39,7 @@ import { installedCypressVersion } from '../../utils/cypress-version'; import { typesNodeVersion, viteVersion } from '../../utils/versions'; import { addBaseCypressSetup } from '../base-setup/base-setup'; import cypressInitGenerator, { addPlugin } from '../init/init'; +import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt'; export interface CypressE2EConfigSchema { project: string; @@ -194,6 +195,18 @@ async function normalizeOptions(tree: Tree, options: CypressE2EConfigSchema) { Rename or remove the existing e2e target.`); } + if ( + !options.baseUrl && + !options.devServerTarget && + !projectConfig?.targets?.serve + ) { + const { devServerTarget, baseUrl } = await promptForMissingServeData( + options.project + ); + options.devServerTarget = devServerTarget; + options.baseUrl = baseUrl; + } + if ( !options.baseUrl && !options.devServerTarget && @@ -228,6 +241,38 @@ In this case you need to provide a devServerTarget,':[: }; } +async function promptForMissingServeData(projectName: string) { + const { devServerTarget, port } = await promptWhenInteractive<{ + devServerTarget: string; + port: number; + }>( + [ + { + type: 'input', + name: 'devServerTarget', + message: + 'What is the name of the target used to serve the application locally?', + initial: `${projectName}:serve`, + }, + { + type: 'numeral', + name: 'port', + message: 'What port will the application be served on?', + initial: 3000, + }, + ], + { + devServerTarget: `${projectName}:serve`, + port: 3000, + } + ); + + return { + devServerTarget, + baseUrl: `http://localhost:${port}`, + }; +} + async function addFiles( tree: Tree, options: NormalizedSchema, diff --git a/packages/playwright/src/generators/configuration/configuration.ts b/packages/playwright/src/generators/configuration/configuration.ts index bbdfa4f0a7f6b..c8565a3912568 100644 --- a/packages/playwright/src/generators/configuration/configuration.ts +++ b/packages/playwright/src/generators/configuration/configuration.ts @@ -20,6 +20,7 @@ import { writeJson, } from '@nx/devkit'; import { resolveImportPath } from '@nx/devkit/src/generators/project-name-and-root-utils'; +import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt'; import { getRelativePathToRootTsConfig } from '@nx/js'; import { normalizeLinterOption } from '@nx/js/src/utils/generator-prompts'; import { @@ -269,6 +270,13 @@ async function normalizeOptions( const linter = await normalizeLinterOption(tree, options.linter); + if (!options.webServerCommand || !options.webServerAddress) { + const { webServerCommand, webServerAddress } = + await promptForMissingServeData(options.project); + options.webServerCommand = webServerCommand; + options.webServerAddress = webServerAddress; + } + return { ...options, addPlugin, @@ -277,6 +285,37 @@ async function normalizeOptions( }; } +async function promptForMissingServeData(projectName: string) { + const { command, port } = await promptWhenInteractive<{ + command: string; + port: number; + }>( + [ + { + type: 'input', + name: 'command', + message: 'What command should be run to serve the application locally?', + initial: `npx nx serve ${projectName}`, + }, + { + type: 'numeral', + name: 'port', + message: 'What port will the application be served on?', + initial: 3000, + }, + ], + { + command: `npx nx serve ${projectName}`, + port: 3000, + } + ); + + return { + webServerCommand: command, + webServerAddress: `http://localhost:${port}`, + }; +} + function getBrowsersInstallTask() { return () => { output.log({