Skip to content

Commit 624002a

Browse files
authored
feat(scripts-cypress): make sure webpack dev server port is unique to enable parallel execution (#35147)
1 parent 718ac46 commit 624002a

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

scripts/cypress/src/base.config.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
import * as crypto from 'crypto';
12
import * as path from 'path';
23

34
import { defineConfig } from 'cypress';
45
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
56
import type { Configuration } from 'webpack';
67

7-
/**
8-
* use this as base webpack config if you need to customize devServer webpack configuration
9-
*/
8+
const projectRoot = process.cwd();
9+
10+
// Use a high port range unlikely to collide with other services: 20000-29999
11+
const deterministicPort = 20000 + (hashToInt(projectRoot) % 10000);
12+
1013
export const baseWebpackConfig: Configuration = {
1114
resolve: {
1215
extensions: ['.js', '.ts', '.jsx', '.tsx'],
1316
},
1417
mode: 'development',
1518
devtool: 'eval',
19+
// Ensure parallel Cypress component runs don't collide on a fixed port (8080 is webpack-dev-server default).
20+
// Pick a deterministic port per project (can be overridden) since some CI setups ignore 'auto'.
21+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
22+
// @ts-ignore - devServer is provided by webpack-dev-server typings
23+
devServer: {
24+
port: process.env.WEBPACK_DEV_SERVER_PORT ? Number(process.env.WEBPACK_DEV_SERVER_PORT) : deterministicPort,
25+
host: '127.0.0.1',
26+
},
1627
output: {
1728
publicPath: '/',
1829
chunkFilename: '[name].bundle.js',
@@ -64,7 +75,6 @@ interface BaseConfig extends Cypress.ConfigOptions {
6475
};
6576
}
6677

67-
const projectRoot = process.cwd();
6878
/**
6979
* Programmatically create relative support support path, because Cypress bug
7080
* @see https://github.com/cypress-io/cypress/issues/31819
@@ -85,7 +95,6 @@ export const baseConfig = defineConfig({
8595
bundler: 'webpack',
8696
webpackConfig: cypressWebpackConfig(),
8797
},
88-
8998
supportFile: path.join(projectSupportDir, './component.js'),
9099
indexHtmlFile: path.join(projectSupportDir, './component-index.html'),
91100
},
@@ -98,3 +107,16 @@ export const baseConfig = defineConfig({
98107
// screenshotOnRunFailure: isLocalRun && argv.mode === 'run',
99108
fixturesFolder: path.join(__dirname, './fixtures'),
100109
}) as BaseConfig;
110+
111+
/**
112+
* use this as base webpack config if you need to customize devServer webpack configuration
113+
*
114+
* Generate a deterministic, project-scoped port to avoid collisions when multiple Cypress component
115+
* test servers start in parallel on the same machine/agent. Allows override via WEBPACK_DEV_SERVER_PORT.
116+
*/
117+
function hashToInt(str: string) {
118+
// Use Node.js crypto module for better hashing
119+
const hash = crypto.createHash('sha256').update(str).digest('hex');
120+
// Convert first 8 hex characters to integer
121+
return parseInt(hash.slice(0, 8), 16);
122+
}

0 commit comments

Comments
 (0)