Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(test): invalidate one-time timers after teardown

MagicLizi820b416ebf4f
diff --git a/scripts/__tests__/check-test-hygiene.test.mjs b/scripts/__tests__/check-test-hygiene.test.mjs
index 7a1954379a..b7848798bf 100644
--- a/scripts/__tests__/check-test-hygiene.test.mjs
+++ b/scripts/__tests__/check-test-hygiene.test.mjs
@@ -45,6 +45,16 @@ test("scanTestSource counts only unjustified hits for each rule", () => {
"afterEach(() => vi.useRealTimers());\nit('fake', () => { vi.useFakeTimers(); setTimeout(done, 100); });\nit('real', () => { setTimeout(done, 100); });",
{ realWaits: 1 },
],
+ [
+ "afterEach invalidates file registration-time fake timers for later tests",
+ "vi.useFakeTimers(); afterEach(() => vi.useRealTimers()); it('first', () => {}); it('later', () => setTimeout(done, 100));",
+ { realWaits: 1 },
+ ],
+ [
+ "afterEach invalidates describe registration-time fake timers for later tests",
+ "describe('timers', () => { vi.useFakeTimers(); afterEach(() => vi.useRealTimers()); it('first', () => {}); it('later', () => setTimeout(done, 100)); });",
+ { realWaits: 1 },
+ ],
[
"beforeEach fake timers apply to their own describe, not a sibling suite",
"describe('fake', () => { beforeEach(() => vi.useFakeTimers()); afterEach(() => vi.useRealTimers()); it('one', () => setTimeout(done, 100)); });\ndescribe('real', () => { it('two', () => setTimeout(done, 100)); });",
diff --git a/scripts/check-test-hygiene.mjs b/scripts/check-test-hygiene.mjs
index 67b94cf3af..40c383fcde 100644
--- a/scripts/check-test-hygiene.mjs
+++ b/scripts/check-test-hygiene.mjs
@@ -204,6 +204,10 @@ function timerStateAt(index, timerEvents, scopes) {
const inherited = events.filter(({ event, scope }) => event.index < index && belongsToWait(scope));
for (const { event } of inherited) fake = event[1] === 'useFakeTimers';
if (waitScope && /^(?:it|test)$/.test(waitScope.kind)) {
+ // Registration-time setup, like beforeAll, runs only once. An afterEach
+ // restoring real timers can invalidate it before a later test runs.
+ const restoresRealTimers = events.some(entry => entry.scope?.kind === 'afterEach' && entry.event[1] === 'useRealTimers' && belongsToWait(describeAt(entry.scope)));
+ if (restoresRealTimers) fake = false;
const hooks = events.filter(({ scope }) => scope && /^(?:beforeAll|beforeEach)$/.test(scope.kind) && belongsToWait(describeAt(scope)))
.sort((a, b) => {
const depthA = scopes.filter(s => s.kind === 'describe' && s.start < a.scope.start && a.scope.end < s.end).length;
@@ -214,7 +218,7 @@ function timerStateAt(index, timerEvents, scopes) {
for (const { event, scope } of hooks) {
// An afterEach restoring real timers invalidates beforeAll-only setup
// for subsequent tests. Be conservative rather than exempt a real wait.
- if (scope.kind === 'beforeAll' && events.some(entry => entry.scope?.kind === 'afterEach' && entry.event[1] === 'useRealTimers' && belongsToWait(describeAt(entry.scope)))) continue;
+ if (scope.kind === 'beforeAll' && restoresRealTimers) continue;
fake = event[1] === 'useFakeTimers';
}
}