Skip to content

Commit a46de03

Browse files
fix: hmr:e2e flaky in CI (#983)
* refactor: 🎨 use portWait in CI * refactor: 🎨 delay time configable by env variable * test: βœ… wait port released in teardown * refactor: 🎨 throw when port not release for time * Update scripts/test-hmr.mjs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * chore: πŸ”§ add aci.yml placeholder --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent d9a87c7 commit a46de03

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

β€Ž.aci.yml

Whitespace-only changes.

β€Žpackage.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020
"@emotion/react": "^11.11.1",
2121
"@emotion/styled": "^11.11.0",
2222
"@jest/expect": "^29.7.0",
23-
"@types/jest": "^29.5.12",
2423
"@okamjs/okam": "workspace:*",
2524
"@swc/helpers": "0.5.1",
2625
"@taplo/cli": "^0.7.0",
2726
"@types/express": "^4.17.21",
27+
"@types/jest": "^29.5.12",
2828
"@types/node": "^20.4.2",
2929
"@types/semver": "^7.5.0",
3030
"antd": "^5.12.2",
3131
"babel-plugin-import": "^1.13.8",
3232
"es-check": "^7.1.1",
3333
"esno": "^4",
3434
"express": "^4.18.2",
35+
"get-port": "^7.1.0",
3536
"husky": "^8.0.3",
3637
"less": "^4.2.0",
3738
"node-libs-browser-okam": "2.2.4",
@@ -42,6 +43,7 @@
4243
"react-error-overlay": "6.0.9",
4344
"react-refresh": "^0.14.0",
4445
"semver": "^7.5.4",
46+
"wait-port": "^1.1.0",
4547
"webpack": "^5.0.0",
4648
"zx": "^7.2.3"
4749
},

β€Žpnpm-lock.yaml

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žscripts/test-hmr.mjs

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import assert from 'assert';
22
import { execSync } from 'child_process';
3+
import getPort, { clearLockedPorts } from 'get-port';
34
import { chromium, devices } from 'playwright';
5+
import waitPort from 'wait-port';
46
import 'zx/globals';
57

68
function skip() {}
@@ -12,8 +14,8 @@ if (!fs.existsSync(tmp)) {
1214
fs.mkdirSync(tmp, { recursive: true });
1315
}
1416
// TODO: check port
15-
const port = 3000;
16-
const DELAY_TIME = 500;
17+
const MAKO_DEV_PORT = 3000;
18+
const DELAY_TIME = parseInt(process.env.DELAY_TIME) || 500;
1719

1820
async function cleanup({ process, browser } = {}) {
1921
try {
@@ -81,7 +83,7 @@ ReactDOM.createRoot(document.getElementById("root")).render(<App />);
8183
await cleanup({ process, browser });
8284
});
8385

84-
runTest('js: anonymize default export hmr', async () => {
86+
runTest('js: anonymous default export hmr', async () => {
8587
write(
8688
normalizeFiles({
8789
'/src/App.tsx': `
@@ -496,7 +498,11 @@ return 'bar'+bar();
496498
});
497499
await delay(DELAY_TIME);
498500
thisResult = normalizeHtml(await getRootHtml(page));
499-
assert.equal(thisResult.html, '<div>App barbar</div>', 'Second render');
501+
assert.equal(
502+
thisResult.html,
503+
'<div>App barbar</div>',
504+
`Second render: unexpected html ${thisResult.html}`,
505+
);
500506
isReload = lastResult.random !== thisResult.random;
501507
assert.equal(isReload, true, 'should reload');
502508
lastResult = thisResult;
@@ -1432,14 +1438,15 @@ async function startMakoDevServer() {
14321438
'scripts',
14331439
'mako.js',
14341440
)} ${tmp} --watch`.nothrow();
1441+
await waitPort({ port: MAKO_DEV_PORT, timeout: 10000 });
14351442
return { process: p };
14361443
}
14371444

14381445
async function startBrowser() {
14391446
const browser = await chromium.launch();
14401447
const context = await browser.newContext(devices['iPhone 11']);
14411448
const page = await context.newPage();
1442-
await page.goto(`http://localhost:${port}`);
1449+
await page.goto(`http://localhost:${MAKO_DEV_PORT}`);
14431450
return { browser, page };
14441451
}
14451452

@@ -1465,6 +1472,17 @@ async function killMakoDevServer() {
14651472
const res = await $`ps -ax | grep mako | grep -v grep | awk '{print $1}'`;
14661473
console.error('stdout', res.stdout);
14671474
await $`ps -ax | grep mako | grep -v grep | awk '{print $1}' | xargs kill -9`;
1475+
let waited = 0;
1476+
while (waited < 10000) {
1477+
await delay(1000);
1478+
clearLockedPorts();
1479+
let port = await getPort({ port: MAKO_DEV_PORT });
1480+
if (port == MAKO_DEV_PORT) {
1481+
return;
1482+
}
1483+
waited += 1000;
1484+
}
1485+
throw Error(`port(${MAKO_DEV_PORT}) not released for ${waited}ms`);
14681486
}
14691487

14701488
function normalizeHtml(html) {
@@ -1511,7 +1529,7 @@ async function commonTest(
15111529
}
15121530

15131531
(async () => {
1514-
console.log('tests', Object.keys(tests).join(', '));
1532+
console.log('tests', Object.keys(tests).join(',\n'));
15151533
for (const [name, fn] of Object.entries(tests)) {
15161534
console.log(`> ${chalk.green(name)}`);
15171535
await fn();

0 commit comments

Comments
Β (0)