Back to all reviewers

Prevent flaky test timing

langfuse/langfuse
Based on 4 comments
TypeScript

Replace non-deterministic timing patterns with reliable alternatives to prevent flaky tests. This includes: 1. Use element-based waits instead of fixed timeouts:

Testing TypeScript

Reviewer Prompt

Replace non-deterministic timing patterns with reliable alternatives to prevent flaky tests. This includes:

  1. Use element-based waits instead of fixed timeouts: ```typescript // Bad await page.waitForTimeout(2000);

// 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');
  1. Mock time-based functions when testing time-dependent logic: ```typescript // Bad const job = await createJob({ timestamp: new Date() });

// 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.

4
Comments Analyzed
TypeScript
Primary Language
Testing
Category

Source Discussions