From ee551146cae00a8fadb82aff43012852f3aa6a58 Mon Sep 17 00:00:00 2001 From: gdjdkid <67935269+gdjdkid@users.noreply.github.com> Date: Fri, 27 Mar 2026 12:03:21 +0800 Subject: [PATCH] fix: move File polyfill to top of fetcher.test.js and fix vitest config --- test/fetcher.test.js | 17 +++++++++-------- vitest.config.js | 5 +---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/test/fetcher.test.js b/test/fetcher.test.js index f68461d..aa1e118 100644 --- a/test/fetcher.test.js +++ b/test/fetcher.test.js @@ -1,19 +1,19 @@ +// 为 Node 18 提供 Web API polyfill import { beforeAll } from 'vitest'; -// 为 Node 18 提供缺失的 Web API(File, FormData 等) beforeAll(() => { - if (typeof File === 'undefined') { + if (typeof global.File === 'undefined') { global.File = class File { constructor(bits, name, options = {}) { this.bits = bits; this.name = name; this.type = options.type || ''; - this.size = bits.length || 0; + this.size = bits?.length || 0; } }; } - if (typeof FormData === 'undefined') { + if (typeof global.FormData === 'undefined') { global.FormData = class FormData { constructor() { this.data = new Map(); @@ -25,15 +25,17 @@ beforeAll(() => { } }); +// ===================================================== + import { describe, it, expect, vi, beforeEach } from 'vitest'; -// mock execSync,避免真实的 curl 网络请求 +// Mock child_process vi.mock('child_process', () => ({ execSync: vi.fn(), spawnSync: vi.fn(), })); -// mock cache,避免缓存干扰测试结果 +// Mock cache vi.mock('../lib/cache.js', () => ({ getCache: vi.fn().mockReturnValue(null), setCache: vi.fn(), @@ -43,7 +45,7 @@ vi.mock('../lib/cache.js', () => ({ import { execSync } from 'child_process'; import { search } from '../lib/fetcher.js'; -// 模拟 JAVBUS 详情页 HTML +// 模拟 HTML 数据 const MOCK_JAVBUS_HTML = ` SSIS-001 @@ -71,7 +73,6 @@ const MOCK_JAVBUS_HTML = ` `; -// 模拟 404 页面 const MOCK_404_HTML = ` 404 diff --git a/vitest.config.js b/vitest.config.js index da364f8..11ef20a 100644 --- a/vitest.config.js +++ b/vitest.config.js @@ -4,9 +4,6 @@ export default defineConfig({ test: { environment: 'node', include: ['test/**/*.test.js'], - pool: 'forks', // 对 Node 18 更友好 - deps: { - inline: ['axios', 'chalk', 'ora'], - }, + pool: 'forks', }, }); \ No newline at end of file