1
0

refactor: 清理测试代码 eslint-disable 指令,消除文件级和重复局部禁用

This commit is contained in:
2026-05-21 00:35:08 +08:00
parent ccd16a583e
commit b432581444
6 changed files with 185 additions and 175 deletions

View File

@@ -104,6 +104,17 @@ describe("loadConfig", () => {
expect((error as Error).message).toContain(message);
}
async function expectConfigLoadError(configPath: string, message: string): Promise<void> {
let error: unknown;
try {
await loadConfig(configPath);
} catch (caught) {
error = caught;
}
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toContain(message);
}
test("解析最简 HTTP 配置", async () => {
const configPath = join(tempDir, "minimal-http.yaml");
await writeFile(
@@ -327,8 +338,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("name 不能为空白");
await expectConfigLoadError(configPath, "name 不能为空白");
});
test("name 仅包含空白字符抛出错误", async () => {
@@ -343,8 +353,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("name 不能为空白");
await expectConfigLoadError(configPath, "name 不能为空白");
});
test("description 显式 null 保留为 null", async () => {
@@ -449,8 +458,7 @@ targets:
maxRedirects: "\${max_redirects}"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("maxRedirects");
await expectConfigLoadError(configPath, "maxRedirects");
});
test("变量替换后通过 schema 校验", async () => {
@@ -491,8 +499,7 @@ targets:
url: "\${MISSING_BASE_URL}/health"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("未定义的变量");
await expectConfigLoadError(configPath, "未定义的变量");
});
test("绝对 dataDir 保持不变", async () => {
@@ -546,8 +553,7 @@ targets:
});
test("配置文件不存在抛出错误", async () => {
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig("/nonexistent/file.yaml")).rejects.toThrow("配置文件不存在");
await expectConfigLoadError("/nonexistent/file.yaml", "配置文件不存在");
});
test("target 缺少 id 抛出错误", async () => {
@@ -560,8 +566,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 id 字段");
await expectConfigLoadError(configPath, "缺少 id 字段");
});
test("target 缺少 type 抛出错误", async () => {
@@ -575,8 +580,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 type 字段");
await expectConfigLoadError(configPath, "缺少 type 字段");
});
test("HTTP target 缺少 url 抛出错误", async () => {
@@ -590,8 +594,7 @@ targets:
http: {}
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 http.url 字段");
await expectConfigLoadError(configPath, "缺少 http.url 字段");
});
test("HTTP target 缺少 http 分组抛出清晰错误", async () => {
@@ -604,8 +607,7 @@ targets:
type: http
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 http.url 字段");
await expectConfigLoadError(configPath, "缺少 http.url 字段");
});
test("HTTP target ignoreSSL 非布尔值抛出错误", async () => {
@@ -621,8 +623,7 @@ targets:
ignoreSSL: "true"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("http.ignoreSSL 类型不合法");
await expectConfigLoadError(configPath, "http.ignoreSSL 类型不合法");
});
test("HTTP target maxRedirects 非负整数校验", async () => {
@@ -638,8 +639,7 @@ targets:
maxRedirects: 1.5
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("http.maxRedirects 类型不合法");
await expectConfigLoadError(configPath, "http.maxRedirects 类型不合法");
});
test("HTTP target status 模式非法抛出错误", async () => {
@@ -656,8 +656,7 @@ targets:
status: ["abc"]
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("status 模式");
await expectConfigLoadError(configPath, "status 模式");
});
test("cmd target 缺少 exec 抛出错误", async () => {
@@ -671,8 +670,7 @@ targets:
cmd: {}
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 cmd.exec 字段");
await expectConfigLoadError(configPath, "缺少 cmd.exec 字段");
});
test("非法 target type 抛出错误", async () => {
@@ -685,8 +683,7 @@ targets:
type: dns
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("不支持的 type");
await expectConfigLoadError(configPath, "不支持的 type");
});
test("target id 重复抛出错误", async () => {
@@ -706,8 +703,7 @@ targets:
url: "http://b.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("target id 重复");
await expectConfigLoadError(configPath, "target id 重复");
});
test("target id 为空字符串抛出错误", async () => {
@@ -721,8 +717,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 id 字段");
await expectConfigLoadError(configPath, "缺少 id 字段");
});
test("target id 命名不合法抛出错误", async () => {
@@ -736,8 +731,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("id 不符合命名规则");
await expectConfigLoadError(configPath, "id 不符合命名规则");
});
test("target id 包含下划线和连字符通过", async () => {
@@ -758,8 +752,7 @@ targets:
test("targets 为空数组抛出错误", async () => {
const configPath = join(tempDir, "empty-targets.yaml");
await writeFile(configPath, `targets: []`);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("至少一个 target");
await expectConfigLoadError(configPath, "至少一个 target");
});
test("无效端口号抛出错误", async () => {
@@ -776,8 +769,7 @@ targets:
url: "http://a.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("server.port 数值范围不合法");
await expectConfigLoadError(configPath, "server.port 数值范围不合法");
});
test("非法 maxConcurrentChecks 抛出错误", async () => {
@@ -794,8 +786,7 @@ targets:
url: "http://a.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("runtime.maxConcurrentChecks 数值范围不合法");
await expectConfigLoadError(configPath, "runtime.maxConcurrentChecks 数值范围不合法");
});
test("非法 size 格式抛出错误", async () => {
@@ -813,8 +804,7 @@ targets:
url: "http://a.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("无效的 size 格式");
await expectConfigLoadError(configPath, "无效的 size 格式");
});
test("非法 interval 格式抛出错误", async () => {
@@ -830,8 +820,7 @@ targets:
url: "http://a.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("无效的时长格式");
await expectConfigLoadError(configPath, "无效的时长格式");
});
test("解析 expect 配置", async () => {
@@ -1011,8 +1000,7 @@ targets:
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("group 必须为字符串");
await expectConfigLoadError(configPath, "group 必须为字符串");
});
test("HTTP headers 非字符串值抛出错误", async () => {
@@ -1029,8 +1017,7 @@ targets:
X-Custom: 123
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("http.headers");
await expectConfigLoadError(configPath, "http.headers");
});
test("HTTP body 非字符串抛出错误", async () => {
@@ -1046,8 +1033,7 @@ targets:
body: 123
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("http.body 类型不合法");
await expectConfigLoadError(configPath, "http.body 类型不合法");
});
test("maxBodyBytes 负数抛出错误", async () => {
@@ -1063,8 +1049,7 @@ targets:
maxBodyBytes: -1
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("非负安全整数");
await expectConfigLoadError(configPath, "非负安全整数");
});
test("maxBodyBytes 非整数抛出错误", async () => {
@@ -1080,8 +1065,7 @@ targets:
maxBodyBytes: 1.5
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("非负安全整数");
await expectConfigLoadError(configPath, "非负安全整数");
});
test("expect.status 数字不在 100-599 范围抛出错误", async () => {
@@ -1098,8 +1082,7 @@ targets:
status: [999]
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("100-599");
await expectConfigLoadError(configPath, "100-599");
});
test("expect.status 范围 6xx 抛出错误", async () => {
@@ -1116,8 +1099,7 @@ targets:
status: ["6xx"]
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("5xx");
await expectConfigLoadError(configPath, "5xx");
});
test("expect.durationMs 对象简写抛出错误", async () => {
@@ -1135,8 +1117,7 @@ targets:
foo: "bar"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("expect.durationMs.foo 是未知 matcher");
await expectConfigLoadError(configPath, "expect.durationMs.foo 是未知 matcher");
});
test("expect.body 非数组抛出错误", async () => {
@@ -1153,8 +1134,7 @@ targets:
body: "not-array"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("expect.body 必须为数组");
await expectConfigLoadError(configPath, "expect.body 必须为数组");
});
test("body rule 缺少支持字段抛出错误", async () => {
@@ -1172,8 +1152,7 @@ targets:
- foo: "bar"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("foo 是未知字段");
await expectConfigLoadError(configPath, "foo 是未知字段");
});
test("body rule 使用 match 字段(非支持)抛出错误", async () => {
@@ -1191,8 +1170,7 @@ targets:
- match: "ok"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("match 是未知字段");
await expectConfigLoadError(configPath, "match 是未知字段");
});
test("body rule 直接 matcher 混入 extractor 抛出错误", async () => {
@@ -1212,8 +1190,7 @@ targets:
path: "$.status"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("直接 matcher 不能与 extractor 混用");
await expectConfigLoadError(configPath, "直接 matcher 不能与 extractor 混用");
});
test("body regex 非法正则抛出错误", async () => {
@@ -1231,8 +1208,7 @@ targets:
- regex: "[invalid"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("regex 正则不合法");
await expectConfigLoadError(configPath, "regex 正则不合法");
});
test("body json path 不以 $. 开头抛出错误", async () => {
@@ -1252,8 +1228,7 @@ targets:
equals: "ok"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("json.path");
await expectConfigLoadError(configPath, "json.path");
});
test("body css selector 为空抛出错误", async () => {
@@ -1272,8 +1247,7 @@ targets:
selector: ""
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("css.selector 必须为非空字符串");
await expectConfigLoadError(configPath, "css.selector 必须为非空字符串");
});
test("旧 match matcher 抛出错误", async () => {
@@ -1292,8 +1266,7 @@ targets:
match: "[invalid"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("match 是未知 matcher");
await expectConfigLoadError(configPath, "match 是未知 matcher");
});
test("operator gte 非数字抛出错误", async () => {
@@ -1313,8 +1286,7 @@ targets:
gte: "abc"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("gte 必须为有限数字");
await expectConfigLoadError(configPath, "gte 必须为有限数字");
});
test("operator exists 非布尔值抛出错误", async () => {
@@ -1334,8 +1306,7 @@ targets:
exists: "yes"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("exists 必须为布尔值");
await expectConfigLoadError(configPath, "exists 必须为布尔值");
});
test("未知字段导致启动失败", async () => {
@@ -1357,8 +1328,7 @@ targets:
note: "ignored"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("unknownHttpField 是未知字段");
await expectConfigLoadError(configPath, "unknownHttpField 是未知字段");
});
test("xpath path 非空字符串校验", async () => {
@@ -1377,8 +1347,7 @@ targets:
path: ""
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("xpath.path 必须为非空字符串");
await expectConfigLoadError(configPath, "xpath.path 必须为非空字符串");
});
test("expect headers 非对象抛出错误", async () => {
@@ -1395,8 +1364,7 @@ targets:
headers: "invalid"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("expect.headers 类型不合法");
await expectConfigLoadError(configPath, "expect.headers 类型不合法");
});
test("HTTP method 小写输入失败", async () => {
@@ -1760,8 +1728,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("description");
await expectConfigLoadError(configPath, "description");
});
test("description 超过 500 字符抛出错误", async () => {
@@ -1776,8 +1743,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("description");
await expectConfigLoadError(configPath, "description");
});
test("变量替换后 description 超长抛出错误", async () => {
@@ -1794,8 +1760,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("description");
await expectConfigLoadError(configPath, "description");
});
test("id 超过 30 字符抛出错误", async () => {
@@ -1809,8 +1774,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("id");
await expectConfigLoadError(configPath, "id");
});
test("name 超过 30 字符抛出错误", async () => {
@@ -1825,8 +1789,7 @@ targets:
url: "http://example.com"
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("name");
await expectConfigLoadError(configPath, "name");
});
test("解析最简 tcp 配置", async () => {