Skip to content

feat(jest): port no-unnecessary-assertion rule - #2334

Merged
fansenze merged 2 commits into
mainfrom
feat/jest-no-unnecessary-assertion
Sep 28, 2026
Merged

fansenze merged 2 commits into
mainfrom
feat/jest-no-unnecessary-assertion

Conversation

@elecmonkey

Copy link
Copy Markdown
Member

Motivation

TypeScript types can prove that some Jest assertions cannot produce the asserted result, so these assertions either add no coverage or point at inaccurate types.

Changes

Add the type-aware jest/no-unnecessary-assertion rule. It reports toBeNull(), toBeUndefined(), toBeDefined() and toBeNaN(), with or without .not, when the subject's type cannot be null, undefined or a number, and warns when strictNullChecks is disabled.

Move the rule shell shared with rstest/no-unnecessary-assertion (the strictNullChecks check, the checked matchers and the messages) into internal/utils/test_framework/rules/no_unnecessary_assertion, and move the Rstest rule onto it without changing its behavior. Each plugin keeps its own expect parsing and subject type analysis, because the Rstest rule deliberately skips unresolved generics, void and intersection types that eslint-plugin-jest reports.

The rule is not added to the recommended preset.

Credits

Port from eslint-plugin-jest@29.16.0's no-unnecessary-assertion.

Related Links

Related #935.

Checklist

  • Tests updated.
  • Documentation updated.

@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

🦀📦 Binary size

Commit 026ac67 merged into base 3c4a05e — feat(react): add state-in-constructor rule (#2307).

Binary Base This PR Change
rslint (linux-x64-gnu) 39.47 MiB 39.48 MiB +4.00 KiB (+0.01%)

Stripped go build -ldflags="-s -w" ./cmd/rslint, Go 1.27.0, linux/amd64 · run · 2026-09-28 09:24 UTC

@elecmonkey
elecmonkey marked this pull request as ready for review September 28, 2026 08:31
Comment thread internal/plugins/jest/rules/no_unnecessary_assertion/no_unnecessary_assertion.md Outdated
…ssary_assertion.md

Co-authored-by: senze.fan <151665030@qq.com>
@fansenze
fansenze enabled auto-merge (squash) September 28, 2026 08:58
@fansenze
fansenze merged commit 5cffdf0 into main Sep 28, 2026
15 checks passed
@fansenze
fansenze deleted the feat/jest-no-unnecessary-assertion branch September 28, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants