Skip to content

Commit abc135d

Browse files
committed
test: fix port conflicts
1 parent 88e9ab0 commit abc135d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

test/basic/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
44
import { createRsbuild } from '@rsbuild/core';
55
import { pluginToml } from '../../src';
6+
import { getRandomPort } from '../helper';
67

78
const __dirname = dirname(fileURLToPath(import.meta.url));
89

@@ -12,7 +13,7 @@ test('should render page as expected', async ({ page }) => {
1213
rsbuildConfig: {
1314
plugins: [pluginToml()],
1415
server: {
15-
port: 3100,
16+
port: getRandomPort(),
1617
},
1718
},
1819
});
@@ -31,7 +32,7 @@ test('should build succeed', async ({ page }) => {
3132
rsbuildConfig: {
3233
plugins: [pluginToml()],
3334
server: {
34-
port: 3100,
35+
port: getRandomPort(),
3536
},
3637
},
3738
});

test/commonjs/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
44
import { createRsbuild } from '@rsbuild/core';
55
import { pluginToml } from '../../src';
6+
import { getRandomPort } from '../helper';
67

78
const __dirname = dirname(fileURLToPath(import.meta.url));
89

@@ -15,6 +16,9 @@ test('should render page as expected', async ({ page }) => {
1516
esModule: false,
1617
}),
1718
],
19+
server: {
20+
port: getRandomPort(),
21+
},
1822
},
1923
});
2024

@@ -36,6 +40,9 @@ test('should build succeed', async ({ page }) => {
3640
esModule: false,
3741
}),
3842
],
43+
server: {
44+
port: getRandomPort(),
45+
},
3946
},
4047
});
4148

test/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const portMap = new Map();
2+
3+
export function getRandomPort(
4+
defaultPort = Math.ceil(Math.random() * 30000) + 15000,
5+
) {
6+
let port = defaultPort;
7+
while (true) {
8+
if (!portMap.get(port)) {
9+
portMap.set(port, 1);
10+
return port;
11+
}
12+
port++;
13+
}
14+
}

0 commit comments

Comments
 (0)