Replace non-deterministic timing patterns with reliable alternatives to prevent flaky tests. This includes: 1. Use element-based waits instead of fixed timeouts:
Replace non-deterministic timing patterns with reliable alternatives to prevent flaky tests. This includes:
// Good await page.waitForSelector(‘[data-testid=”element”]’, { state: ‘visible’, timeout: 5000 });
2. Use fixed timestamps for time-dependent tests:
```typescript
// Bad
const cutoffDate = new Date(Date.now() + 24*60*60*1000);
// Good
const cutoffDate = new Date('2024-01-01T00:00:00Z');
// Good const fixedDate = new Date(‘2024-01-01T00:00:00Z’); jest.useFakeTimers().setSystemTime(fixedDate); const job = await createJob({ timestamp: new Date() }); ```
This approach reduces test flakiness, improves CI reliability, and makes tests more maintainable by removing timing-dependent assumptions.
Enter the URL of a public GitHub repository