VSCode调试时如何禁用Vitest的超时限制?
Vitest调试模式下超时问题的解决方案梳理
我在编写测试时,习惯用Vitest官方VSCode扩展右键选择「Debug test」的方式调试,过程中会反复单步执行、重构检查并多次运行测试,但Vitest默认5秒的超时时间不够用。目前官方扩展没有提供针对调试场景单独设置超时或检测到VSCODE_INSPECTOR_OPTIONS环境变量时忽略超时的选项。
我梳理了几种可行方案:
方案1:在helper文件中重写test函数
通过封装Vitest的test函数,根据环境变量判断是否设置超长超时:
import { TestOptions, test as _test } from "vitest"; export function test(name: string, fn: any): void { const options: TestOptions = {}; if (process.env.VSCODE_INSPECTOR_OPTIONS) { options.timeout = 999999; } return _test(name, options, fn); }
缺点:所有测试文件都需要从helper文件导入test,而非直接从vitest导入,代码冗余且易混乱。
方案2:在describe块中配置超时
在每个测试文件的describe块中添加条件化超时配置:
import { describe, expect, it } from "vitest"; import { rebuildLeaderboardScores } from "../lib/leaderboard"; const options = { timeout: process.env.VSCODE_INSPECTOR_OPTIONS ? 999999 : 5000, }; describe("leaderboard functions test set", options, () => { it("rebuilding leaderboard attempt that doesn't exist fails", () => { expect(() => rebuildLeaderboardScores("1")).toThrowError(/leaderboard/); }); });
缺点:需要在每个测试文件重复配置,或额外维护helper文件导入配置,操作繁琐。
方案3:扩展Vitest配置文件设置全局超时
新建配置文件覆盖全局超时,再在VSCode扩展中指定该文件:
import config from "./vitest.config"; config.test!.testTimeout = 999999; export default config;
优缺点:这是目前相对最优的方案,但仍需维护额外配置文件,且只是设置超长超时而非真正禁用超时。
现有方案的共性问题
我尝试过通过CLI设置test-timeout,但在右键调试的方式下无效。上述所有方案的核心问题是:超时设置会应用于VSCode中所有测试运行场景,无法仅针对调试模式生效。
内容的提问来源于stack exchange,提问作者seaders
相关产品推荐
相关产品推荐

