src/content/docs/linter/rules/no-conditional-expect.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.4.2` - Diagnostic Category: [`lint/nursery/noConditionalExpect`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`test`](/linter/domains#test) - Sources: - Same as [`playwright/no-conditional-expect`](https://github.com/playwright-community/eslint-plugin-playwright/blob/main/docs/rules/no-conditional-expect.md) - Same as [`jest/no-conditional-expect`](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/no-conditional-expect.md) - Same as [`vitest/no-conditional-expect`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-conditional-expect.md){
"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:
test.skip() to skip the entire testexpect.soft() for optional assertionstest("conditional expect", async ({ page }) => {
if (someCondition) {
await expect(page).toHaveTitle("Title");
}
});
test("ternary expect", async ({ page }) => {
someCondition ? await expect(page).toHaveTitle("Title") : null;
});
test("catch expect", async ({ page }) => {
try {
await page.click("button");
} catch (e) {
await expect(page).toHaveTitle("Title");
}
});
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");
});