CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/jest-testing

Jest testing patterns — test structure, mocking, async testing, snapshot

99

1.26x
Quality

99%

Does it follow best practices?

Impact

99%

1.26x

Average score across 6 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-5/

Write Tests for the Request Rate Limiter

Problem/Feature Description

The platform team has built a RateLimiter utility that enforces a maximum number of function calls per time window. When the limit is exceeded, calls are queued and executed in batches once the window resets. The limiter uses setTimeout internally to schedule the window reset and flush the queue.

The existing implementation has no test coverage and has caused production incidents due to subtle timing bugs. The engineering team needs a thorough test suite that verifies the limiter's behavior at boundary conditions — exactly at the limit, one over the limit, and after the window resets — without the tests actually waiting for real time to pass.

Output Specification

Write a complete Jest test file at src/utils/__tests__/rateLimiter.test.ts.

Tests should cover at minimum:

  • Calls within the limit execute immediately
  • Calls exceeding the limit are queued
  • Queued calls execute after the time window resets
  • The window counter resets correctly so new calls are allowed after reset

Input Files

src/utils/rateLimiter.ts

export class RateLimiter {
  private callCount = 0;
  private queue: Array<() => void> = [];

  constructor(
    private readonly maxCalls: number,
    private readonly windowMs: number
  ) {
    this.scheduleReset();
  }

  execute(fn: () => void): void {
    if (this.callCount < this.maxCalls) {
      this.callCount++;
      fn();
    } else {
      this.queue.push(fn);
    }
  }

  private scheduleReset(): void {
    setTimeout(() => {
      this.callCount = 0;
      const toFlush = this.queue.splice(0, this.maxCalls);
      toFlush.forEach((fn) => {
        this.callCount++;
        fn();
      });
      this.scheduleReset();
    }, this.windowMs);
  }
}

evals

tile.json