noConditionalExpect
このコンテンツはまだ日本語訳がありません。
Summary
Section titled “Summary”- Rule available since:
v2.4.2 - Diagnostic Category:
lint/nursery/noConditionalExpect - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
playwright/no-conditional-expect - Same as
jest/no-conditional-expect - Same as
vitest/no-conditional-expect
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noConditionalExpect": "error" } } }}Description
Section titled “Description”Disallow conditional expect() calls inside tests.
Conditional expectations are problematic because they can silently pass when the condition is false, meaning assertions may never actually run. This can lead to tests that pass despite bugs in the code.
If you need conditional testing logic, consider:
- Using
test.skip()to skip the entire test - Splitting into separate tests with clear conditions
- Using
expect.soft()for optional assertions
Examples
Section titled “Examples”Invalid
Section titled “Invalid”test("conditional expect", async ({ page }) => { if (someCondition) { await expect(page).toHaveTitle("Title"); }});code-block.js:3:15 lint/nursery/noConditionalExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected conditional expect() call.
1 │ test(“conditional expect”, async ({ page }) => {
2 │ if (someCondition) {
> 3 │ await expect(page).toHaveTitle(“Title”);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │ });
ℹ This expect() is inside a if statement, which means it may not always run.
ℹ Consider using test.skip() to conditionally skip the test, or restructure to avoid conditional expectations.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
test("ternary expect", async ({ page }) => { someCondition ? await expect(page).toHaveTitle("Title") : null;});code-block.js:2:27 lint/nursery/noConditionalExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected conditional expect() call.
1 │ test(“ternary expect”, async ({ page }) => {
> 2 │ someCondition ? await expect(page).toHaveTitle(“Title”) : null;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ });
4 │
ℹ This expect() is inside a ternary expression, which means it may not always run.
ℹ Consider using test.skip() to conditionally skip the test, or restructure to avoid conditional expectations.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
test("catch expect", async ({ page }) => { try { await page.click("button"); } catch (e) { await expect(page).toHaveTitle("Title"); }});code-block.js:5:15 lint/nursery/noConditionalExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected conditional expect() call.
3 │ await page.click(“button”);
4 │ } catch (e) {
> 5 │ await expect(page).toHaveTitle(“Title”);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ }
7 │ });
ℹ This expect() is inside a catch clause, which means it may not always run.
ℹ Consider using test.skip() to conditionally skip the test, or restructure to avoid conditional expectations.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
test("unconditional expect", async ({ page }) => { await expect(page).toHaveTitle("Title");});test("skip based on condition", async ({ page }) => { test.skip(someCondition, "Reason to skip"); await expect(page).toHaveTitle("Title");});Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.