biome-plugin-no-if-in-tests
v1.0.0
Published
Biome plugin that forbids if statements and try-catch in test files
Maintainers
Readme
biome-plugin-no-if-in-tests
A Biome plugin that forbids if statements and try-catch blocks in test files.
Why?
Tests should be deterministic and straightforward. Conditional logic in tests often indicates:
- Tests that don't always run the same assertions
- Hidden complexity that makes tests harder to understand
- Code that should be refactored into separate test cases
Instead of try-catch, use your testing framework's built-in assertions like t.throws() (AVA) or expect(...).toThrow() (Jest).
Installation
npm install --save-dev biome-plugin-no-if-in-testsUsage
Add the plugin rules to your biome.json:
{
"$schema": "https://biomejs.dev/schemas/1.9.0/schema.json",
"plugins": [
"./node_modules/biome-plugin-no-if-in-tests/rules/no-if-in-tests.grit",
"./node_modules/biome-plugin-no-if-in-tests/rules/no-try-catch-in-tests.grit"
]
}Run Biome with experimental plugins enabled:
biome lint --use-experimental-plugins .Rules
no-if-in-tests
Forbids if and if-else statements in files matching *test.ts or *test.js.
// Bad
test("should work", (t) => {
if (condition) {
t.true(value);
}
});
// Good
test("should work when condition is true", (t) => {
t.true(value);
});
test("should work when condition is false", (t) => {
t.false(value);
});no-try-catch-in-tests
Forbids try-catch blocks in test files.
// Bad
test("should throw", (t) => {
try {
throwingFunction();
} catch (error) {
t.is(error.message, "error");
}
});
// Good
test("should throw", (t) => {
t.throws(() => throwingFunction(), { message: "error" });
});License
MIT
