Back to Biomejs

useExpect

src/content/docs/linter/rules/use-expect.mdx

latest7.3 KB
Original Source

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)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"useExpect": "error"
			}
		}
	}
}

Description

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.

Allowed expect variants

Examples

Invalid

js
test("no assertion", async ({ page }) => {
    await page.goto("/");
    await page.click("button");
});
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-expect">lint/nursery/useExpect</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Test callback is missing an </span><span style="color: lightgreen;"><strong>expect()</strong></span><span style="color: lightgreen;"> assertion.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>test(&quot;no assertion&quot;, async (&#123; page &#125;) =&gt; &#123; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> await page.goto(&quot;/&quot;); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> await page.click(&quot;button&quot;); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong>&#125;); <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Tests without assertions may pass even when the behavior is broken.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Add an assertion using </span><span style="color: lightgreen;"><strong>expect()</strong></span><span style="color: lightgreen;"> to verify the expected behavior.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

js
test("has assertion", async ({ page }) => {
    await page.goto("/");
    await expect(page).toHaveTitle("Title");
});
js
it("soft assertion", async ({ page }) => {
    await page.goto("/");
    await expect.soft(page.locator("h1")).toBeVisible();
});

Variant assertions are allowed:

js
it("returns bar when passed foo", () => {
  assert(myFunc("foo") === "bar", "didn't return bar");
});
ts
it("should allow passing 'foo' as an argument", () => {
  expectTypeOf(myFunc).toBeCallableWith("foo");
});
ts
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.)

</TabItem> </Tabs>