Question

Cursor writes tests that mock the thing under test

Solved · 10 views · asked by rosa_m · edited

I asked for tests for my useCountdown hook. The tests mock useCountdown and then assert the mock was called. Everything is green and nothing is tested.

What I’ve tried

Asked "don't mock the hook". It mocked setInterval instead and asserted the mock, same problem.

Comment

2 answers

Marked as helpful by the asker
marco_py · edited

Tell it what a test must observe, not what it must not mock:

Render a component that uses useCountdown(3). Advance fake timers by 3 seconds. Assert the rendered text goes from 3 to 0 and onDone was called once.

With Testing Library and vi.useFakeTimers() that's a real test. The mock of setInterval is fine, that's the environment; the hook itself must run for real.

Comment
dev_ana · edited

Rule of thumb for reviewing AI tests: if you delete the implementation and the test still passes, it's not a test.

Comment