From e0644250813d42e8f812e7ebd4505a63e9392edb Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 12 Dec 2021 09:19:39 +0800 Subject: [PATCH] fix(watcher): avoid racing --- src/node/watcher.ts | 19 +++++++++++++------ test/core/src/timeout.ts | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/node/watcher.ts b/src/node/watcher.ts index 1f1032440..ad138b4ad 100644 --- a/src/node/watcher.ts +++ b/src/node/watcher.ts @@ -13,6 +13,7 @@ export async function startWatcher(ctx: VitestContext, pool: WorkerPool) { const changedTests = new Set() const seen = new Set() + let promise: Promise | undefined server.watcher.on('change', (id) => { id = slash(id) @@ -38,7 +39,8 @@ export async function startWatcher(ctx: VitestContext, pool: WorkerPool) { } }) - function rerunFile(id: string) { + async function rerunFile(id: string) { + await promise clearTimeout(timer) timer = setTimeout(async() => { if (changedTests.size === 0) { @@ -57,13 +59,18 @@ export async function startWatcher(ctx: VitestContext, pool: WorkerPool) { changedTests.clear() seen.clear() - await reporter.onWatcherRerun?.(tests, id) + promise = start(tests, id, invalidates) + await promise + }, WATCHER_DEBOUNCE) + } - await pool.runTestFiles(tests, invalidates) + async function start(tests: string[], id: string, invalidates: string[]) { + await reporter.onWatcherRerun?.(tests, id) - await reporter.onFinished?.(ctx.state.getFiles(tests)) - await reporter.onWatcherStart?.() - }, WATCHER_DEBOUNCE) + await pool.runTestFiles(tests, invalidates) + + await reporter.onFinished?.(ctx.state.getFiles(tests)) + await reporter.onWatcherStart?.() } // add an empty promise so it never resolves diff --git a/test/core/src/timeout.ts b/test/core/src/timeout.ts index dad2509db..0b9884c06 100644 --- a/test/core/src/timeout.ts +++ b/test/core/src/timeout.ts @@ -1 +1 @@ -export const timeout = 100 +export const timeout = 5000 -- 2.51.2