Skip to content

Commit

Permalink
fix(testing): e2e config generators should prompt for missing serve d…
Browse files Browse the repository at this point in the history
…ata (#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.

(cherry picked from commit bc0566f)
  • Loading branch information
Coly010 authored and FrozenPandaz committed Jan 17, 2025
1 parent 749e7fc commit 9696210
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/cypress/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -228,6 +241,38 @@ In this case you need to provide a devServerTarget,'<projectName>:<targetName>[:
};
}

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,
Expand Down
39 changes: 39 additions & 0 deletions packages/playwright/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand Down

0 comments on commit 9696210

Please sign in to comment.