src/content/docs/linter/rules/use-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/useExpect`](/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/expect-expect`](https://github.com/playwright-community/eslint-plugin-playwright/blob/main/docs/rules/expect-expect.md) - Same as [`jest/expect-expect`](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/expect-expect.md) - Same as [`vitest/expect-expect`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/expect-expect.md){
"linter": {
"rules": {
"nursery": {
"useExpect": "error"
}
}
}
}
Ensure that test functions contain at least one expect() or similar assertion.
Tests without assertions may pass even when behavior is broken, leading to
false confidence in the test suite. This rule ensures that every test
validates some expected behavior using expect() or an allowed variant thereof.
expect variantstest("no assertion", async ({ page }) => {
await page.goto("/");
await page.click("button");
});
test("has assertion", async ({ page }) => {
await page.goto("/");
await expect(page).toHaveTitle("Title");
});
it("soft assertion", async ({ page }) => {
await page.goto("/");
await expect.soft(page.locator("h1")).toBeVisible();
});
Variant assertions are allowed:
it("returns bar when passed foo", () => {
assert(myFunc("foo") === "bar", "didn't return bar");
});
it("should allow passing 'foo' as an argument", () => {
expectTypeOf(myFunc).toBeCallableWith("foo");
});
it("should have proper type", () => {
assertType<(n: string) => string>(myFunc);
});
(This replicates the rule's behavior in eslint-plugin-vitest with typecheck set to true.)