Skip to content

Commit 5b7ee33

Browse files
committed
test(markdown): avoid partial writes in watcher recovery
1 parent 6349594 commit 5b7ee33

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎packages/histoire/src/node/__tests__/markdown/watcher.spec.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { writeFileSync } from 'node:fs'
12
import { unlink } from 'node:fs/promises'
23
import { join } from 'node:path'
34
import { describe, expect, it, vi } from 'vitest'
@@ -20,15 +21,17 @@ describe('markdown watcher ownership', () => {
2021
expect(file.html).toContain(file.content)
2122
completed.push(file.content!)
2223
})
23-
await project.write('[Missing](./Missing.story.md)', 'Guide.story.md')
24+
// Keep each edit complete before watcher callbacks can run. Async writes
25+
// yield after truncation, allowing a valid empty document to be published.
26+
writeFileSync(file.absolutePath, '[Missing](./Missing.story.md)')
2427
await vi.waitFor(() => expect(errors).toHaveBeenCalled())
2528
expect(String(errors.mock.calls[0][0])).toContain('[md] Cannot find story file')
2629
expect(file.content).toBe('Original')
2730
expect(completed).toEqual([])
2831
// Native change events within Chokidar's throttle window can coalesce.
2932
// Retry the real edit until admitted; a poisoned queue still cannot pass.
30-
await vi.waitFor(async () => {
31-
if (!completed.length) await project.write('Corrected', 'Guide.story.md')
33+
await vi.waitFor(() => {
34+
if (!completed.length) writeFileSync(file.absolutePath, 'Corrected')
3235
expect(completed).toEqual(['Corrected'])
3336
}, { interval: 100 })
3437
expect(file.html).toContain('Corrected')

0 commit comments

Comments
 (0)