1
1
import assert from 'assert' ;
2
2
import { execSync } from 'child_process' ;
3
+ import getPort , { clearLockedPorts } from 'get-port' ;
3
4
import { chromium , devices } from 'playwright' ;
5
+ import waitPort from 'wait-port' ;
4
6
import 'zx/globals' ;
5
7
6
8
function skip ( ) { }
@@ -12,8 +14,8 @@ if (!fs.existsSync(tmp)) {
12
14
fs . mkdirSync ( tmp , { recursive : true } ) ;
13
15
}
14
16
// 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 ;
17
19
18
20
async function cleanup ( { process, browser } = { } ) {
19
21
try {
@@ -81,7 +83,7 @@ ReactDOM.createRoot(document.getElementById("root")).render(<App />);
81
83
await cleanup ( { process, browser } ) ;
82
84
} ) ;
83
85
84
- runTest ( 'js: anonymize default export hmr' , async ( ) => {
86
+ runTest ( 'js: anonymous default export hmr' , async ( ) => {
85
87
write (
86
88
normalizeFiles ( {
87
89
'/src/App.tsx' : `
@@ -496,7 +498,11 @@ return 'bar'+bar();
496
498
} ) ;
497
499
await delay ( DELAY_TIME ) ;
498
500
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
+ ) ;
500
506
isReload = lastResult . random !== thisResult . random ;
501
507
assert . equal ( isReload , true , 'should reload' ) ;
502
508
lastResult = thisResult ;
@@ -1432,14 +1438,15 @@ async function startMakoDevServer() {
1432
1438
'scripts' ,
1433
1439
'mako.js' ,
1434
1440
) } ${ tmp } --watch`. nothrow ( ) ;
1441
+ await waitPort ( { port : MAKO_DEV_PORT , timeout : 10000 } ) ;
1435
1442
return { process : p } ;
1436
1443
}
1437
1444
1438
1445
async function startBrowser ( ) {
1439
1446
const browser = await chromium . launch ( ) ;
1440
1447
const context = await browser . newContext ( devices [ 'iPhone 11' ] ) ;
1441
1448
const page = await context . newPage ( ) ;
1442
- await page . goto ( `http://localhost:${ port } ` ) ;
1449
+ await page . goto ( `http://localhost:${ MAKO_DEV_PORT } ` ) ;
1443
1450
return { browser, page } ;
1444
1451
}
1445
1452
@@ -1465,6 +1472,17 @@ async function killMakoDevServer() {
1465
1472
const res = await $ `ps -ax | grep mako | grep -v grep | awk '{print $1}'` ;
1466
1473
console . error ( 'stdout' , res . stdout ) ;
1467
1474
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` ) ;
1468
1486
}
1469
1487
1470
1488
function normalizeHtml ( html ) {
@@ -1511,7 +1529,7 @@ async function commonTest(
1511
1529
}
1512
1530
1513
1531
( async ( ) => {
1514
- console . log ( 'tests' , Object . keys ( tests ) . join ( ', ' ) ) ;
1532
+ console . log ( 'tests' , Object . keys ( tests ) . join ( ',\n ' ) ) ;
1515
1533
for ( const [ name , fn ] of Object . entries ( tests ) ) {
1516
1534
console . log ( `> ${ chalk . green ( name ) } ` ) ;
1517
1535
await fn ( ) ;
0 commit comments