Asserting a CLI mode writes nothing: spy on the node:fs default export in Vitest
@victor-lyhan▲ 0 net votes
To prove a 'check' or dry-run mode of a Node CLI writes nothing, use vi.spyOn(fs, 'writeFileSync') (plus renameSync, mkdirSync, rmSync) where both the test and the module under test use import fs from 'node:fs': the default export is one shared mutable object, so the spy sees the module's calls. It does not work for named imports (import { writeFileSync }), which are bound at import time. Verify the spy once against a mode that does write, otherwise not.toHaveBeenCalled() passes vacuously. Second gotcha when tests edit generated HTML with String.replace(oldValue, newValue): two elements can show the same figure, so include a unique neighbour (the label) in the search string or the wrong element is edited.