Skip to content

Commit b328ded

Browse files
test(u5b): scope the injected stat to the row that installs it
vi.restoreAllMocks() in afterEach restores spies; it does not clear an implementation set on the fs/promises factory mocks, so the double installed by the leftover-stat rows stayed in place for the rest of the file. Keyed on the literal backup path that was inert, because beforeEach mkdtemps a fresh directory per test and the captured path cannot recur. Keyed on dev+ino it fires again as soon as the operating system hands a later test's unrelated file the inode an earlier row captured - measured on the hosted ubuntu leg, where six unrelated rows in this file failed and the three leftover rows passed. Five of the six carry this injection's fingerprint; the 0o600 row is skipIf(win32) and so has no local oracle. Restore the previous implementation in a finally, and name in the test what the matcher is keyed on and for how long it lives.
1 parent 5841dd2 commit b328ded

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

‎src/utils/__tests__/safeWriteJson.test.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ describe("safeWriteJson", () => {
673673
return false // a candidate that cannot be stat-ed is not the injected leftover
674674
}
675675
}
676+
// The double is installed for this test only. vi.restoreAllMocks() in afterEach does not
677+
// clear implementations set on the fs/promises factory mocks, and the operating system
678+
// recycles inodes, so a matcher left installed rejects a later test's unrelated file the
679+
// moment that file is handed the inode this row captured.
680+
const previousStat = vi.mocked(fs.stat).getMockImplementation()
676681
vi.mocked(fs.stat).mockImplementation(async (backupPath) => {
677682
if (await isInjectedInode(backupPath)) {
678683
throw Object.assign(new Error("Backup stat failed"), { code })
@@ -681,14 +686,18 @@ describe("safeWriteJson", () => {
681686
})
682687
const merge = vi.fn((existing: unknown) => existing)
683688

684-
if (code === "ENOENT") {
685-
await safeWriteJson(currentTestFilePath, {}, { merge })
686-
expect(merge).toHaveBeenCalledWith({ committed: true }, {})
687-
} else {
688-
await expect(safeWriteJson(currentTestFilePath, {}, { merge })).rejects.toMatchObject({ code })
689-
expect(merge).not.toHaveBeenCalled()
690-
expect(await readFileContent(unreadableBackup)).toEqual({ stale: true })
691-
expect(await readFileContent(readableBackup)).toEqual({ committed: true })
689+
try {
690+
if (code === "ENOENT") {
691+
await safeWriteJson(currentTestFilePath, {}, { merge })
692+
expect(merge).toHaveBeenCalledWith({ committed: true }, {})
693+
} else {
694+
await expect(safeWriteJson(currentTestFilePath, {}, { merge })).rejects.toMatchObject({ code })
695+
expect(merge).not.toHaveBeenCalled()
696+
expect(await readFileContent(unreadableBackup)).toEqual({ stale: true })
697+
expect(await readFileContent(readableBackup)).toEqual({ committed: true })
698+
}
699+
} finally {
700+
vi.mocked(fs.stat).mockImplementation(previousStat ?? actual.stat)
692701
}
693702
},
694703
)

0 commit comments

Comments
 (0)