diff --git a/e2e/cases/cli/reload-config/index.test.ts b/e2e/cases/cli/reload-config/index.test.ts index be1e7e8a9c..e6353f3532 100644 --- a/e2e/cases/cli/reload-config/index.test.ts +++ b/e2e/cases/cli/reload-config/index.test.ts @@ -24,7 +24,7 @@ test('should restart dev server and reload config when config file changed', asy root: 'dist', }, }, - server: { port: ${getRandomPort()} } + server: { port: ${await getRandomPort()} } };`, ); @@ -45,7 +45,7 @@ test('should restart dev server and reload config when config file changed', asy root: 'dist-2', }, }, - server: { port: ${getRandomPort()} } + server: { port: ${await getRandomPort()} } };`, ); diff --git a/e2e/cases/cli/reload-env/index.test.ts b/e2e/cases/cli/reload-env/index.test.ts index e85976f595..b21832d3f0 100644 --- a/e2e/cases/cli/reload-env/index.test.ts +++ b/e2e/cases/cli/reload-env/index.test.ts @@ -25,7 +25,7 @@ test.skip('should restart dev server when .env file is changed', async () => { output: { disableFilenameHash: true, }, - server: { port: ${getRandomPort()} } + server: { port: ${await getRandomPort()} } };`, ); diff --git a/e2e/cases/preview/index.test.ts b/e2e/cases/preview/index.test.ts index fc23944798..bc7388266b 100644 --- a/e2e/cases/preview/index.test.ts +++ b/e2e/cases/preview/index.test.ts @@ -24,7 +24,7 @@ test('should allow plugin to modify preview server config', async ({ page, }) => { const cwd = join(__dirname, 'basic'); - const PORT = getRandomPort(); + const PORT = await getRandomPort(); const plugin: RsbuildPlugin = { name: 'test', diff --git a/e2e/package.json b/e2e/package.json index 6fcc7d2134..e6658f8a4b 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -30,10 +30,10 @@ "@rsbuild/plugin-pug": "workspace:*", "@rsbuild/plugin-react": "workspace:*", "@rsbuild/plugin-rem": "workspace:*", - "@rsbuild/plugin-stylus": "workspace:*", "@rsbuild/plugin-solid": "workspace:*", "@rsbuild/plugin-source-build": "workspace:*", "@rsbuild/plugin-styled-components": "workspace:*", + "@rsbuild/plugin-stylus": "workspace:*", "@rsbuild/plugin-svelte": "workspace:*", "@rsbuild/plugin-svgr": "workspace:*", "@rsbuild/plugin-swc": "workspace:*", diff --git a/e2e/scripts/shared.ts b/e2e/scripts/shared.ts index bf037f3b32..83e0a240ff 100644 --- a/e2e/scripts/shared.ts +++ b/e2e/scripts/shared.ts @@ -1,3 +1,4 @@ +import net from 'net'; import { URL } from 'url'; import assert from 'assert'; import { join } from 'path'; @@ -73,16 +74,34 @@ export const createRsbuild = async ( return rsbuild; }; +function isPortAvailable(port: number) { + const server = net.createServer().listen(port); + return new Promise((resolve, reject) => { + server.on('listening', () => { + server.close(); + resolve(true); + }); + + server.on('error', (err: { code: string }) => { + if (err.code === 'EADDRINUSE') { + resolve(false); + } else { + reject(err); + } + }); + }); +} + const portMap = new Map(); // Available port ranges: 1024 ~ 65535 // `10080` is not available in CI, so we start with `11000`. -export function getRandomPort( +export async function getRandomPort( defaultPort = Math.ceil(Math.random() * 50000) + 11000, ) { let port = defaultPort; while (true) { - if (!portMap.get(port)) { + if (!portMap.get(port) && (await isPortAvailable(port))) { portMap.set(port, 1); return port; } else { @@ -105,7 +124,7 @@ const updateConfigForTest = async ( // make devPort random to avoid port conflict config.server = { ...(config.server || {}), - port: getRandomPort(config.server?.port), + port: await getRandomPort(config.server?.port), }; config.dev ??= {};