Skip to content

Commit 57c9a4a

Browse files
committed
test(@angular-devkit/build-angular): prevent flakiness in live-reload proxies dev-server test
Ignore proxy connection errors when the browser reloads or disconnects, and replace fixed setTimeout delays with waitForAppLiveReload polling helper to avoid navigation race conditions. (cherry picked from commit 4ac7ceb)
1 parent a6e30fa commit 57c9a4a

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

‎packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,33 @@ async function createProxy(target: string, secure: boolean, ws = true): Promise<
101101
},
102102
}).listen(proxyPort);
103103

104+
server.on('error', () => {
105+
// Ignore proxy connection errors that occur when the browser reloads or disconnects.
106+
});
107+
104108
return {
105109
server,
106110
url: `${secure ? 'https' : 'http'}://localhost:${proxyPort}`,
107111
};
108112
}
109113

114+
async function waitForAppLiveReload(page: Page): Promise<void> {
115+
const startTime = Date.now();
116+
while (Date.now() - startTime < 30_000) {
117+
try {
118+
const text = await page.evaluate(() => document.querySelector('p')?.innerText);
119+
if (text === 'app-live-reload') {
120+
return;
121+
}
122+
} catch {
123+
// Ignore execution context destruction errors during page navigation.
124+
}
125+
await setTimeoutPromise(100);
126+
}
127+
128+
throw new Error('Timed out waiting for page to reload with updated text.');
129+
}
130+
110131
async function goToPageAndWaitForWS(page: Page, url: string): Promise<void> {
111132
const baseUrl = url.replace(/^http/, 'ws');
112133
const socksRequest = baseUrl.at(-1) === '/' ? `${baseUrl}ng-cli-ws` : `${baseUrl}/ng-cli-ws`;
@@ -203,8 +224,7 @@ describeServeBuilder(
203224
async ({ result }) => {
204225
expect(result?.success).toBeTrue();
205226

206-
// Wait for page to reload.
207-
await setTimeoutPromise(500);
227+
await waitForAppLiveReload(page);
208228

209229
const innerText = await page.evaluate(() => document.querySelector('p').innerText);
210230
expect(innerText).toBe('app-live-reload');
@@ -238,8 +258,7 @@ describeServeBuilder(
238258
async ({ result }) => {
239259
expect(result?.success).toBeTrue();
240260

241-
// Wait for page to reload.
242-
await setTimeoutPromise(500);
261+
await waitForAppLiveReload(page);
243262

244263
const innerText = await page.evaluate(
245264
() => document.querySelector('p').innerText,
@@ -281,8 +300,7 @@ describeServeBuilder(
281300
async ({ result }) => {
282301
expect(result?.success).toBeTrue();
283302

284-
// Wait for page to reload.
285-
await setTimeoutPromise(500);
303+
await waitForAppLiveReload(page);
286304

287305
const innerText = await page.evaluate(
288306
() => document.querySelector('p').innerText,

0 commit comments

Comments
 (0)