Are you an LLM? You can read better optimized documentation at /docs/guide/usage/linter/rules/vitest/require-to-throw-message.md for this page in Markdown format
vitest/require-to-throw-message 正确性
作用
如果在使用 toThrow() 或 toThrowError() 时没有提供错误消息,此规则会触发警告。
为什么这很糟糕?
在没有指定预期错误消息的情况下使用 toThrow() 或 toThrowError(), 会使测试不够具体,也更难调试。当测试只检查是否抛出了 错误,但不检查是哪种错误时,即使抛出了错误类型不对的错误,它也可能通过,从而潜在地隐藏缺陷。提供预期的错误消息 或错误类型可以使测试更精确,并有助于更有效地发现回归问题。
示例
以下是此规则的错误代码示例:
javascript
test("all the things", async () => {
expect(() => a()).toThrow();
expect(() => a()).toThrowError();
await expect(a()).rejects.toThrow();
await expect(a()).rejects.toThrowError();
});以下是此规则的正确代码示例:
javascript
test("all the things", async () => {
expect(() => a()).toThrow("a");
expect(() => a()).toThrowError("a");
await expect(a()).rejects.toThrow("a");
await expect(a()).rejects.toThrowError("a");
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/require-to-throw-message": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/require-to-throw-message": "error",
},
});bash
oxlint --deny vitest/require-to-throw-message --vitest-plugin版本
此规则在 v0.2.9 中添加。
