Skip to content

Commit e365629

Browse files
committed
test(json): add Windows file readability delay after save operations
Add waitForFile() helper that adds a 50ms delay on Windows after save() operations to ensure the file is readable before attempting to read it. This addresses a test-level race condition where tests were failing intermittently because they tried to read files immediately after writing, before Windows had made them accessible. Updated tests: - should preserve line endings - should support sort option - should use default 2-space indent for new files This complements the production code fix in retryWrite() by also ensuring tests don't race against filesystem operations.
1 parent e3ada12 commit e365629

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

test/unit/json.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,13 @@ describe('json', () => {
761761
describe('EditableJson', () => {
762762
let testDir: string
763763

764+
// Helper to wait for file to be readable on Windows after write
765+
const waitForFile = async () => {
766+
if (process.platform === 'win32') {
767+
await sleep(50)
768+
}
769+
}
770+
764771
beforeEach(async () => {
765772
testDir = await mkdtemp(join(tmpdir(), 'editable-json-test-'))
766773
})
@@ -1070,6 +1077,7 @@ describe('json', () => {
10701077
const instance = await EditableJson.load(filepath)
10711078
instance.update({ newKey: 'newValue' })
10721079
await instance.save()
1080+
await waitForFile()
10731081

10741082
const content = await readFile(filepath, 'utf8')
10751083
expect(content).toContain('\r\n')
@@ -1091,6 +1099,7 @@ describe('json', () => {
10911099
})
10921100

10931101
await instance.save({ sort: true })
1102+
await waitForFile()
10941103

10951104
const content = await readFile(filepath, 'utf8')
10961105
const keys = Object.keys(JSON.parse(content))
@@ -1116,6 +1125,7 @@ describe('json', () => {
11161125
})
11171126

11181127
await instance.save()
1128+
await waitForFile()
11191129

11201130
const content = await readFile(filepath, 'utf8')
11211131
expect(content).toBe('{\n "key": "value"\n}\n')

0 commit comments

Comments
 (0)