Code Room
Vibe codingHardvc-g150
Subject Ai flaky testsLevel Mid–Senior~16 minCommon in Concurrency interviewsIndustries Software development

Question

An AI agent's async JS test for a debounced search was flaky in CI. The agent 'fixed' it by adding a sleep:

js
test('debounced search fires once', async () => {  const fn = jest.fn();  const search = debounce(fn, 300);  search('a'); search('ab'); search('abc');  await new Promise(r => setTimeout(r, 350)); // wait for debounce  expect(fn).toHaveBeenCalledTimes(1);  expect(fn).toHaveBeenCalledWith('abc');});

It now passes locally and 'usually' passes in CI. Why is this fix wrong, and how should the test be made reliable?

What a strong answer looks like

Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it — tests, edge cases, reading critically — and how you’d re-prompt or decompose to get it right.

Describe your solution

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.