-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(lint/useExpect): reject asymmetric matchers and utilities as assertions #9455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dyc3
merged 7 commits into
biomejs:main
from
omar-y-abdi:fix/use-expect-false-negatives
Mar 20, 2026
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c56c646
fix(lint/useExpect): reject asymmetric matchers and utilities as asse…
omar-y-abdi d054a48
fix: address review feedback — add changeset and replace issue ref wi…
omar-y-abdi a51461c
Update .changeset/fix-use-expect-false-negatives.md
omar-y-abdi 8bf71e2
Merge branch 'main' into fix/use-expect-false-negatives
omar-y-abdi c221d57
Merge branch 'main' into fix/use-expect-false-negatives
omar-y-abdi e0b56c3
chore: lints
dyc3 ee548ac
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed [#9174](https://github.com/biomejs/biome/issues/9174): [`useExpect`](https://biomejs.dev/linter/rules/use-expect/) now correctly rejects Vitest [asymmetric matchers](https://vitest.dev/api/expect.html#expect-stringcontaining) like `expect.stringContaining()`, `expect.objectContaining()`, and utilities like `expect.extend()` that are not valid assertions. Previously these constructs caused false negatives, allowing tests without real assertions to pass the lint rule. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
crates/biome_js_analyze/tests/specs/nursery/useExpect/invalid/asymmetric-matchers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* should generate diagnostics */ | ||
|
|
||
| test("only asymmetric matcher, not an assertion", () => { | ||
| const matcher = expect.stringContaining("foo"); | ||
| }); | ||
|
|
||
| test("expect.extend is not an assertion", () => { | ||
| expect.extend({ | ||
| toBeWithinRange(received, floor, ceiling) { | ||
| return { pass: received >= floor && received <= ceiling }; | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("negated asymmetric matcher is not an assertion", () => { | ||
| const matcher = expect.not.objectContaining({ secret: expect.any(String) }); | ||
| }); |
99 changes: 99 additions & 0 deletions
99
crates/biome_js_analyze/tests/specs/nursery/useExpect/invalid/asymmetric-matchers.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: asymmetric-matchers.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should generate diagnostics */ | ||
|
|
||
| test("only asymmetric matcher, not an assertion", () => { | ||
| const matcher = expect.stringContaining("foo"); | ||
| }); | ||
|
|
||
| test("expect.extend is not an assertion", () => { | ||
| expect.extend({ | ||
| toBeWithinRange(received, floor, ceiling) { | ||
| return { pass: received >= floor && received <= ceiling }; | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("negated asymmetric matcher is not an assertion", () => { | ||
| const matcher = expect.not.objectContaining({ secret: expect.any(String) }); | ||
| }); | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| asymmetric-matchers.js:3:1 lint/nursery/useExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Test callback is missing an expect() assertion. | ||
|
|
||
| 1 │ /* should generate diagnostics */ | ||
| 2 │ | ||
| > 3 │ test("only asymmetric matcher, not an assertion", () => { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| > 4 │ const matcher = expect.stringContaining("foo"); | ||
| > 5 │ }); | ||
| │ ^^ | ||
| 6 │ | ||
| 7 │ test("expect.extend is not an assertion", () => { | ||
|
|
||
| i Tests without assertions may pass even when the behavior is broken. | ||
|
|
||
| i Add an assertion using expect() to verify the expected behavior. | ||
|
|
||
| i 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. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| asymmetric-matchers.js:7:1 lint/nursery/useExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Test callback is missing an expect() assertion. | ||
|
|
||
| 5 │ }); | ||
| 6 │ | ||
| > 7 │ test("expect.extend is not an assertion", () => { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| > 8 │ expect.extend({ | ||
| ... | ||
| > 12 │ }); | ||
| > 13 │ }); | ||
| │ ^^ | ||
| 14 │ | ||
| 15 │ it("negated asymmetric matcher is not an assertion", () => { | ||
|
|
||
| i Tests without assertions may pass even when the behavior is broken. | ||
|
|
||
| i Add an assertion using expect() to verify the expected behavior. | ||
|
|
||
| i 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. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| asymmetric-matchers.js:15:1 lint/nursery/useExpect ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Test callback is missing an expect() assertion. | ||
|
|
||
| 13 │ }); | ||
| 14 │ | ||
| > 15 │ it("negated asymmetric matcher is not an assertion", () => { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| > 16 │ const matcher = expect.not.objectContaining({ secret: expect.any(String) }); | ||
| > 17 │ }); | ||
| │ ^^ | ||
| 18 │ | ||
|
|
||
| i Tests without assertions may pass even when the behavior is broken. | ||
|
|
||
| i Add an assertion using expect() to verify the expected behavior. | ||
|
|
||
| i 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. | ||
|
|
||
|
|
||
| ``` |
23 changes: 23 additions & 0 deletions
23
crates/biome_js_analyze/tests/specs/nursery/useExpect/valid/assertion-counts.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* should not generate diagnostics */ | ||
|
|
||
| test("expect.assertions is a valid assertion check", () => { | ||
| expect.assertions(1); | ||
| expect(true).toBe(true); | ||
| }); | ||
|
|
||
| test("expect.hasAssertions is a valid assertion check", () => { | ||
| expect.hasAssertions(); | ||
| expect(1).toBe(1); | ||
| }); | ||
|
|
||
| test("expect.assertions alone counts as assertion-aware", () => { | ||
| expect.assertions(0); | ||
| }); | ||
|
|
||
| test("expect.soft is a valid assertion", async ({ page }) => { | ||
| await expect.soft(page.locator("h1")).toBeVisible(); | ||
| }); | ||
|
|
||
| test("expect.poll is a valid assertion", async ({ page }) => { | ||
| await expect.poll(() => page.title()).toBe("Title"); | ||
| }); |
31 changes: 31 additions & 0 deletions
31
crates/biome_js_analyze/tests/specs/nursery/useExpect/valid/assertion-counts.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: assertion-counts.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
|
|
||
| test("expect.assertions is a valid assertion check", () => { | ||
| expect.assertions(1); | ||
| expect(true).toBe(true); | ||
| }); | ||
|
|
||
| test("expect.hasAssertions is a valid assertion check", () => { | ||
| expect.hasAssertions(); | ||
| expect(1).toBe(1); | ||
| }); | ||
|
|
||
| test("expect.assertions alone counts as assertion-aware", () => { | ||
| expect.assertions(0); | ||
| }); | ||
|
|
||
| test("expect.soft is a valid assertion", async ({ page }) => { | ||
| await expect.soft(page.locator("h1")).toBeVisible(); | ||
| }); | ||
|
|
||
| test("expect.poll is a valid assertion", async ({ page }) => { | ||
| await expect.poll(() => page.title()).toBe("Title"); | ||
| }); | ||
|
|
||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.