chore: use portfinder to solve the problem that the port may be occupied (#53968)

This commit is contained in:
Amumu 2025-06-01 19:30:41 +08:00 committed by GitHub
parent 95609e57c6
commit bfc49e4156
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -287,6 +287,7 @@
"package-manager-detector": "^1.0.0",
"pixelmatch": "^7.1.0",
"pngjs": "^7.0.0",
"portfinder": "^1.0.37",
"prettier": "^3.4.1",
"pretty-format": "^29.7.0",
"prismjs": "^1.29.0",

View File

@ -6,6 +6,7 @@ import { globSync } from 'glob';
import { createServer } from 'http-server';
import fetch from 'isomorphic-fetch';
import uniq from 'lodash/uniq';
import portfinder from 'portfinder';
const components = uniq(
globSync('components/!(overview)/*.md', { cwd: join(process.cwd()), dot: false }).map((path) =>
@ -15,8 +16,11 @@ const components = uniq(
describe('site test', () => {
let server: http.Server | https.Server;
const port = 3000;
const portPromise = portfinder.getPortPromise({
port: 3000,
});
const render = async (path: string) => {
const port = await portPromise;
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
const html: string = await res.text();
const $ = load(html, { xml: true });
@ -46,7 +50,8 @@ describe('site test', () => {
expect(tables.length).toMatchSnapshot();
};
beforeAll(() => {
beforeAll(async () => {
const port = await portPromise;
server = createServer({ root: join(process.cwd(), '_site') });
server.listen(port);