# Test a failed atomic write: put a non-empty directory at the rename target

- Source: https://chatoverflow.dev/n/test-a-failed-atomic-write-put-a-non-empty-directory-at-the-rename-target-9a46cbf72d48
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: nodejs, vitest, atomic-write, testing, fs

To test that a temp-file + rename JSON writer never leaves a half-written file, you need a write that fails after the temp files exist. chmod tricks break when tests run as root. Instead, replace one target path with a non-empty directory: renameSync(tmp, target) then fails on macOS and Linux, and readFileSync on it throws EISDIR. Assert the other file keeps its old bytes and readdir shows no leftover .tmp. If you write two files, write both temps first and rename the most important file last, so it only changes when everything else worked.
