fix: 重构所有定时器测试为 Jest fake timers,彻底解决 CI Run tests 卡死
CI / test (20.x) (push) Canceled after 0s
CI / test (22.x) (push) Canceled after 0s
CI / test (24.x) (push) Canceled after 0s
CI / test (18.x) (push) Canceled after 18m34s

tests/utils.test.ts: debounce/throttle/sleep 使用 useFakeTimers + advanceTimersByTime; tests/core.test.ts: 历史栈防抖合并测试用 fake timers 替代 400ms setTimeout; tests/plugins.test.ts: autoSave 三个测试用 fake timers 替代 setTimeout/done; README.md: 更新测试徽章为 353 passed; site/index.html, site/demo.html: 更新测试数为 353
This commit is contained in:
2026-07-28 21:18:26 +08:00
parent b7a3da8176
commit cf0bc45055
6 changed files with 87 additions and 72 deletions
+13 -9
View File
@@ -1053,11 +1053,16 @@ describe('MarkdownEditor - 工具栏点击', () => {
});
describe('MarkdownEditor - 历史栈合并', () => {
test('连续输入触发防抖合并', (done) => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
test('连续输入触发防抖合并', () => {
document.body.innerHTML = '';
const c = document.createElement('div');
document.body.appendChild(c);
const ed = new MarkdownEditor(c, { value: '' });
// 初始历史栈长度(构造时已 push 一次)
const initialLen = ed._history.length;
// 连续快速输入
ed.textarea.value = 'a';
ed.textarea.dispatchEvent(new Event('input', { bubbles: true }));
@@ -1065,14 +1070,13 @@ describe('MarkdownEditor - 历史栈合并', () => {
ed.textarea.dispatchEvent(new Event('input', { bubbles: true }));
ed.textarea.value = 'abc';
ed.textarea.dispatchEvent(new Event('input', { bubbles: true }));
// 等待防抖结束
setTimeout(() => {
const initialLen = ed._history.length;
// 防抖合并后历史栈不应为 3
expect(ed._history.length).toBeLessThanOrEqual(initialLen + 1);
ed.destroy();
done();
}, 500);
// 防抖期内历史栈不应增长
expect(ed._history.length).toBe(initialLen);
// 推进防抖时间(默认 400ms),触发 _pushHistory
jest.advanceTimersByTime(400);
// 防抖合并后历史栈只增加 1 条
expect(ed._history.length).toBe(initialLen + 1);
ed.destroy();
});
test('历史栈超过上限自动裁剪', () => {