feat(jest): port no-unnecessary-assertion rule - #2334
Merged
Merged
Conversation
Contributor
elecmonkey
marked this pull request as ready for review
September 28, 2026 08:31
fansenze
reviewed
Sep 28, 2026
…ssary_assertion.md Co-authored-by: senze.fan <151665030@qq.com>
fansenze
approved these changes
Sep 28, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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-assertionrule. It reportstoBeNull(),toBeUndefined(),toBeDefined()andtoBeNaN(), with or without.not, when the subject's type cannot benull,undefinedor a number, and warns whenstrictNullChecksis disabled.Move the rule shell shared with
rstest/no-unnecessary-assertion(thestrictNullCheckscheck, the checked matchers and the messages) intointernal/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,voidand intersection types thateslint-plugin-jestreports.The rule is not added to the
recommendedpreset.Credits
Port from
eslint-plugin-jest@29.16.0'sno-unnecessary-assertion.Related Links
Related #935.
Checklist