From e2932476a76bd4f13dd977073b00aa1cc7246f00 Mon Sep 17 00:00:00 2001 From: gdjdkid <67935269+gdjdkid@users.noreply.github.com> Date: Sun, 22 Mar 2026 12:12:00 +0800 Subject: [PATCH] Fixes: Improved unit tests for fetcher and display - display.test.js: Added vitest import to afterEach - fetcher.test.js: Changed from mock axios to mock child_process.execSync - fetcher.test.js: Added mock cache.js to avoid caching interference with tests - fetcher.test.js: Changed network failure test to expect null return --- package.json | 2 +- test/display.test.js | 5 +-- test/fetcher.test.js | 105 ++++++++++++++++++++++--------------------- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index b487000..9293d44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "javdict", - "version": "1.2.5", + "version": "1.2.6", "description": "AV番号命令行查询工具", "main": "./index.js", "type": "module", diff --git a/test/display.test.js b/test/display.test.js index f83c0ab..f83dae4 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -1,7 +1,6 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { display } from '../lib/display.js'; -// 完整的 mock 数据 const MOCK_INFO = { id: 'SSIS-001', title: '测试标题', @@ -20,7 +19,6 @@ const MOCK_INFO = { describe('display.js', () => { beforeEach(() => { - // 拦截 console.log,不真正打印,但可以断言调用内容 vi.spyOn(console, 'log').mockImplementation(() => {}); }); @@ -47,7 +45,6 @@ describe('display.js', () => { it('display:raw 模式输出 JSON 字符串', () => { display(MOCK_INFO, true); const allOutput = console.log.mock.calls.flat().join(' '); - // raw 模式应该输出可解析的 JSON expect(() => JSON.parse(allOutput)).not.toThrow(); }); diff --git a/test/fetcher.test.js b/test/fetcher.test.js index 23501fe..cbffc8d 100644 --- a/test/fetcher.test.js +++ b/test/fetcher.test.js @@ -1,49 +1,55 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; -// mock axios,避免真实网络请求 -vi.mock('axios', () => ({ - default: { - get: vi.fn(), - }, +// mock execSync,避免真实的 curl 网络请求 +vi.mock('child_process', () => ({ + execSync: vi.fn(), + spawnSync: vi.fn(), })); -import axios from 'axios'; +// mock cache,避免缓存干扰测试结果 +vi.mock('../lib/cache.js', () => ({ + getCache: vi.fn().mockReturnValue(null), + setCache: vi.fn(), + getConfig: vi.fn().mockReturnValue({ session: 'mock_session' }), +})); + +import { execSync } from 'child_process'; import { search } from '../lib/fetcher.js'; -// 模拟搜索结果页的 HTML -const MOCK_SEARCH_HTML = ` -
