Back to all reviewers

avoid timing-dependent tests

remix-run/react-router
Based on 2 comments
TypeScript

Tests should not rely on fixed delays, arbitrary timeouts, or assume deterministic ordering of asynchronous operations. Instead, use proper waiting mechanisms and sort results when order doesn't matter.

Testing TypeScript

Reviewer Prompt

Tests should not rely on fixed delays, arbitrary timeouts, or assume deterministic ordering of asynchronous operations. Instead, use proper waiting mechanisms and sort results when order doesn’t matter.

Replace fixed delays like await sleep(LOADER_LATENCY_MS) with conditional waiting:

// Bad: Fixed delay
await sleep(LOADER_LATENCY_MS);
expect(router.getBlocker("KEY", fn)).toEqual({
  state: "unblocked",
  proceed: undefined,
});

// Good: Wait for condition
await waitFor(() =>
  expect(router.getBlocker("KEY", fn)).toEqual({
    state: "unblocked", 
    proceed: undefined,
  })
);

For non-deterministic collections, sort before comparison:

// Bad: Assumes request order
expect(requests).toEqual([...]);

// Good: Sort for deterministic comparison  
expect(requests.sort()).toEqual([...]);

This prevents flaky tests that fail intermittently due to timing variations across different environments and browsers.

2
Comments Analyzed
TypeScript
Primary Language
Testing
Category

Source Discussions