Skip to content

Commit 3e5e7dd

Browse files
committed
test: fix flaky tests
those were failing almost always locally on faster machines like mb m4 pro
1 parent d2a1eb7 commit 3e5e7dd

File tree

6 files changed

+115
-100
lines changed

6 files changed

+115
-100
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"@types/semver": "^7.3.12",
8383
"@types/stream-json": "^1.7.2",
8484
"@types/yargs": "^17.0.26",
85-
"@vitest/coverage-v8": "^4.0.1",
85+
"@vitest/coverage-v8": "^4.0.16",
8686
"apify": "*",
8787
"apify-node-curl-impersonate": "^1.0.15",
8888
"basic-auth-parser": "^0.0.2",
@@ -113,7 +113,7 @@
113113
"turbo": "^2.1.0",
114114
"typescript": "^5.7.3",
115115
"typescript-eslint": "^8.28.0",
116-
"vitest": "^4.0.1"
116+
"vitest": "^4.0.16"
117117
},
118118
"packageManager": "yarn@4.10.3",
119119
"volta": {

test/core/crawlers/basic_crawler.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,11 @@ describe('BasicCrawler', () => {
965965
requestQueue.fetchNextRequest = async () => queue.pop()!;
966966
requestQueue.isEmpty = async () => Promise.resolve(!queue.length);
967967

968-
setTimeout(() => queue.push(request0), 10);
969-
setTimeout(() => queue.push(request1), 100);
968+
setTimeout(() => queue.push(request0), 100);
969+
setTimeout(() => queue.push(request1), 250);
970970
setTimeout(() => {
971971
isFinished = true;
972-
}, 150);
972+
}, 600);
973973

974974
await basicCrawler.run();
975975

@@ -1016,11 +1016,11 @@ describe('BasicCrawler', () => {
10161016
requestQueue.fetchNextRequest = async () => Promise.resolve(queue.pop()!);
10171017
requestQueue.isEmpty = async () => Promise.resolve(!queue.length);
10181018

1019-
setTimeout(() => queue.push(request0), 10);
1020-
setTimeout(() => queue.push(request1), 100);
1019+
setTimeout(() => queue.push(request0), 100);
1020+
setTimeout(() => queue.push(request1), 250);
10211021
setTimeout(() => {
10221022
void basicCrawler.teardown();
1023-
}, 300);
1023+
}, 650);
10241024

10251025
await basicCrawler.run();
10261026

test/core/puppeteer_utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ describe('puppeteerUtils', () => {
242242
urlPatterns: ['.css'],
243243
});
244244
page.on('response', (response) => loadedUrls.push(response.url()));
245-
await page.goto(`${serverAddress}/special/resources`, { waitUntil: 'load' });
245+
await page.goto(`${serverAddress}/special/resources`, { waitUntil: 'networkidle0' });
246246

247247
expect(loadedUrls).toEqual(
248248
expect.arrayContaining([

test/utils/fixtures/child.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// dummy script for testing ps-tree.ts
22
setTimeout(() => {
33
/* Does nothing, but prevents exit */
4-
}, 1000);
4+
}, 5000);

test/utils/psTree.test.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,34 @@ const scripts = {
88
child: path.join(__dirname, 'fixtures', 'child.js'),
99
};
1010

11+
// Helper to poll for a condition on process tree
12+
async function waitForCondition(
13+
pid: number,
14+
condition: (children: any[]) => boolean,
15+
maxWaitMs = 3000,
16+
): Promise<any[]> {
17+
const startTime = Date.now();
18+
while (Date.now() - startTime < maxWaitMs) {
19+
const children = await psTree(pid);
20+
if (condition(children)) {
21+
return children;
22+
}
23+
await new Promise((resolve) => setTimeout(resolve, 100));
24+
}
25+
return psTree(pid); // Final attempt
26+
}
27+
1128
describe('psTree()', () => {
1229
test('Spawn a Parent process which has Ten Child Processes', async () => {
1330
const parent = exec(`node ${scripts.parent}`);
1431

15-
// Wait for the child process(es) to spawn.
16-
await new Promise((resolve) => setTimeout(resolve, 500));
17-
18-
const children = await psTree(parent.pid!);
32+
// Poll until child processes are detected
33+
const children = await waitForCondition(parent.pid!, (c) => c.length > 0);
1934

2035
expect(children.length).toBeGreaterThan(0);
2136

22-
// Allow time for the processes to be terminated.
23-
await new Promise((resolve) => setTimeout(resolve, 2000));
37+
// Poll until processes terminate
38+
await waitForCondition(parent.pid!, (c) => c.length === 0, 10000);
2439

2540
const postKillChildren = await psTree(parent.pid!);
2641

@@ -30,17 +45,17 @@ describe('psTree()', () => {
3045
test('Includes itself if includeRoot is true', async () => {
3146
const parent = exec(`node ${scripts.parent}`);
3247

33-
// Wait for the child process(es) to spawn.
34-
await new Promise((resolve) => setTimeout(resolve, 500));
35-
36-
const processes = await psTree(parent.pid!, true);
48+
// Poll until processes are detected (parent + at least one child)
49+
await waitForCondition(parent.pid!, (c) => c.length > 1, 3000);
3750

38-
const parentProcess = processes.find((process) => Number.parseInt(process.PID, 10) === parent.pid!);
51+
// Get processes with includeRoot after waiting
52+
const processesWithRoot = await psTree(parent.pid!, true);
53+
const parentProcess = processesWithRoot.find((process) => Number.parseInt(process.PID, 10) === parent.pid!);
3954

4055
expect(parentProcess).toBeDefined();
4156

42-
// Allow time for the processes to be terminated.
43-
await new Promise((resolve) => setTimeout(resolve, 2000));
57+
// Poll until processes terminate
58+
await waitForCondition(parent.pid!, (c) => c.length === 0, 10000);
4459

4560
const postKillChildren = await psTree(parent.pid!);
4661

0 commit comments

Comments
 (0)