Skip to content

Commit

Permalink
fix(testing): add webServerCommands/ciWebServerCommands option for cy…
Browse files Browse the repository at this point in the history
…press configuration
  • Loading branch information
FrozenPandaz committed Dec 6, 2023
1 parent 3befa3a commit 6f34803
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe('Cypress e2e configuration', () => {
await cypressE2EConfigurationGenerator(tree, {
project: 'my-app',
baseUrl: 'http://localhost:4200',
webServerCommands: {
default: 'nx run my-app:serve',
production: 'nx run my-app:serve:production',
},
ciWebServerCommand: 'nx run my-app:serve-static',
});
expect(tree.read('apps/my-app/cypress.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
Expand Down
14 changes: 10 additions & 4 deletions packages/cypress/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export interface CypressE2EConfigSchema {
port?: number | 'cypress-auto';
jsx?: boolean;
rootProject?: boolean;

webServerCommands?: Record<string, string>;
ciWebServerCommand?: string;
}

type NormalizedSchema = ReturnType<typeof normalizeOptions>;
Expand Down Expand Up @@ -166,9 +169,12 @@ async function addFiles(
const cyFile = joinPathFragments(projectConfig.root, 'cypress.config.ts');
let webServerCommands: Record<string, string>;

let ciDevServerTarget: string;
let ciWebServerCommand: string;

if (hasPlugin && options.devServerTarget) {
if (hasPlugin && options.webServerCommands && options.ciWebServerCommand) {
webServerCommands = options.webServerCommands;
ciWebServerCommand = options.ciWebServerCommand;
} else if (hasPlugin && options.devServerTarget) {
webServerCommands = {};

webServerCommands.default = 'nx run ' + options.devServerTarget;
Expand All @@ -192,7 +198,7 @@ async function addFiles(
}
// Add ci/static e2e target if serve target is found
if (devServerProjectConfig.targets?.['serve-static']) {
ciDevServerTarget = `nx run ${parsedTarget.project}:serve-static`;
ciWebServerCommand = `nx run ${parsedTarget.project}:serve-static`;
}
}
const updatedCyConfig = await addDefaultE2EConfig(
Expand All @@ -201,7 +207,7 @@ async function addFiles(
cypressDir: options.directory,
bundler: options.bundler === 'vite' ? 'vite' : undefined,
webServerCommands,
ciWebServerCommand: ciDevServerTarget,
ciWebServerCommand: ciWebServerCommand,
},
options.baseUrl
);
Expand Down
17 changes: 13 additions & 4 deletions packages/react/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export async function addE2e(
): Promise<GeneratorCallback> {
switch (options.e2eTestRunner) {
case 'cypress': {
if (
(options.bundler === 'webpack' && !hasWebpackPlugin(tree)) ||
(options.bundler === 'vite' && !hasVitePlugin(tree))
) {
const hasNxBuildPlugin =
(options.bundler === 'webpack' && hasWebpackPlugin(tree)) ||
(options.bundler === 'vite' && hasVitePlugin(tree));
if (!hasNxBuildPlugin) {
webStaticServeGenerator(tree, {
buildTarget: `${options.projectName}:build`,
targetName: 'serve-static',
Expand Down Expand Up @@ -52,6 +52,15 @@ export async function addE2e(
baseUrl: 'http://localhost:4200',
jsx: true,
rootProject: options.rootProject,
webServerCommands: hasNxBuildPlugin
? {
default: `nx run ${options.projectName}:serve`,
production: `nx run ${options.projectName}:serve:production`,
}
: undefined,
ciWebServerCommand: hasNxBuildPlugin
? `nx run ${options.projectName}:serve-static`
: undefined,
});
}
case 'playwright': {
Expand Down

0 comments on commit 6f34803

Please sign in to comment.