Fix a flaky test
Keep the test runner in watch mode next to the code. Every save reruns the test; the failure and the log that explains it stay visible.
The board
nvim The test and the code under test.
Processes vitest in watch mode, kept running. Every save reruns it, and the row counts the lines that report errors.
Step by step
- 01 Run the test in watch mode In Processes, Add process: nameprocesses
test, commandpnpm vitest --watch refund. It runs the tests, then keeps watching. - 02 Read the failure Expected 1 refund, got 2, at refund.spec.ts:58. Restart the row and the same test can pass: it's flaky.processes
- 03 Find the race Above the failure, the code's own log: two refund.created lines for the one order, milliseconds apart.processes
- 04 Fix it and watch it pass In refund.ts, hold the key's lock from the lookup to the insert, and save. vitest reruns beside it and goes green.nvim