Skip to content

noConditionalExpect (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"suspicious": {
"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/suspicious/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.

test("ternary expect", async ({ page }) => {
someCondition ? await expect(page).toHaveTitle("Title") : null;
});
code-block.js:2:27 lint/suspicious/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.

test("catch expect", async ({ page }) => {
try {
await page.click("button");
} catch (e) {
await expect(page).toHaveTitle("Title");
}
});
code-block.js:5:15 lint/suspicious/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.

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