fix(playwright): fix flaky tests (#8319)

This commit is contained in:
Bijin A B
2026-06-20 19:15:32 +05:30
committed by GitHub
parent a93e1dc8bf
commit 34a61ebe28
7 changed files with 182 additions and 141 deletions

25
tests/utils/helpers.ts Normal file
View File

@@ -0,0 +1,25 @@
import fs from 'fs';
/**
* Read and JSON-parse a file, retrying until it has parseable content.
*
* Reading an exported file immediately after the app writes it can observe a
* created-but-not-yet-flushed (empty) file — most often on Windows — which makes
* JSON.parse throw "Unexpected end of JSON input". Poll until the file has content
* that parses as JSON before returning it.
*/
export async function readExportedJson(filePath: string, { tries = 50, interval = 100 } = {}) {
let lastError: unknown;
for (let attempt = 0; attempt < tries; attempt++) {
try {
const raw = fs.readFileSync(filePath, 'utf8');
if (raw.trim().length > 0) {
return JSON.parse(raw);
}
} catch (err) {
lastError = err;
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw lastError ?? new Error(`Timed out waiting for valid JSON at ${filePath}`);
}