Skip to content

noConditionalExpect

biome.json
{
"linter": {
"rules": {
"nursery": {
"noConditionalExpect": "error"
}
}
}
}

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
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");
});