Your development workspace
This app is private. Sign in with your authorized Google account.
For temporary setup owned by one test, prefer an available per-test completion hook over wrapping the entire assertion body in try/finally. Register cleanup immediately after setup so assertion failures cannot skip restoration.
| 1 | it('reads a fixed clock', () => { |
| 2 | vi.useFakeTimers({ toFake: ['Date'] }); |
| 3 | try { |
| 4 | vi.setSystemTime(new Date('2030-05-01T00:00:00Z')); |
| 5 | expect(new Date().getUTCFullYear()).toBe(2030); |
| 6 | } finally { |
| 7 | vi.useRealTimers(); |
| 8 | } |
| 9 | }); |
Explanation (EN)
Cleanup is safe, but the whole test is nested despite the runner providing a scoped completion hook. This is a readability improvement, not a missing-cleanup defect.
Objašnjenje (HR)
Cleanup je siguran, ali cijeli test je uvucen iako runner ima prikladnu kuku. Ovo je poboljsanje citljivosti, a ne nedostatak cleanupa.
| 1 | it('reads a fixed clock', () => { |
| 2 | vi.useFakeTimers({ toFake: ['Date'] }); |
| 3 | onTestFinished(() => { vi.useRealTimers(); }); |
| 4 | vi.setSystemTime(new Date('2030-05-01T00:00:00Z')); |
| 5 | expect(new Date().getUTCFullYear()).toBe(2030); |
| 6 | }); |
Explanation (EN)
Cleanup stays adjacent to setup and the test body remains flat. The runner calls the registered hook after the test completes.
Objašnjenje (HR)
Cleanup ostaje uz setup i tijelo testa je ravno. Runner poziva registriranu kuku nakon zavrsetka testa.
Verify the installed framework contract. In Vitest register onTestFinished inside the test body before later setup or assertions. Use existing configured globals instead of adding redundant imports. Concurrent tests must use their own test-context hook and must not mutate shared global fake timers without isolation. This refines cleanup placement and does not replace the requirement to restore manually patched global state completely.
Provjeri ugovor instaliranog frameworka. U Vitestu registriraj onTestFinished unutar testa prije daljnjeg setupa ili tvrdnji. Koristi konfigurirane globale. Konkurentni testovi trebaju vlastitu kuku iz konteksta i ne smiju neizolirano dijeliti globalne fake timere. Pravilo ne uklanja obvezu potpunog vracanja rucno promijenjenog globalnog stanja.
Keep try/finally where cleanup must occur before continuing in the same test, where no suitable framework lifecycle hook exists, or where the cleanup must also run on control-flow paths that the installed hook does not cover. Shared setup may belong in beforeEach/afterEach instead. Do not claim an existing correct try/finally leaks resources merely because it can be simplified.
Zadrzi try/finally kada cleanup mora zavrsiti prije nastavka istog testa, nema prikladne lifecycle kuke ili kuka ne pokriva potreban tijek izvrsavanja. Zajednicki setup moze pripadati beforeEach/afterEach. Ispravan try/finally nije curenje resursa samo zato sto se moze pojednostavniti.
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).