-
Notifications
You must be signed in to change notification settings - Fork 21
feat(eslint-plugin-jest): add prefer-strict-equal rule
#623
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
fansenze
merged 6 commits into
web-infra-dev:main
from
eryue0220:feat/jest-prefer-strict-equal
Apr 16, 2026
+250
−0
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb30580
feat: add jest rule
eryue0220 8697f29
Update internal/plugins/jest/rules/prefer_strict_equal/prefer_strict_…
eryue0220 8dc53f2
fix cr
eryue0220 2ea2b26
fix conflict
eryue0220 854893b
Merge branch 'main' into feat/jest-prefer-strict-equal
eryue0220 f342032
fix cr
eryue0220 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
65 changes: 65 additions & 0 deletions
65
internal/plugins/jest/rules/prefer_strict_equal/prefer_strict_equal.go
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,65 @@ | ||
| package prefer_strict_equal | ||
|
|
||
| import ( | ||
| "github.com/microsoft/typescript-go/shim/ast" | ||
| "github.com/microsoft/typescript-go/shim/core" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/jest/utils" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| ) | ||
|
|
||
| // Message Builders | ||
|
|
||
| func buildUseToStrictEqualErrorMessage() rule.RuleMessage { | ||
| return rule.RuleMessage{ | ||
| Id: "useToStrictEqual", | ||
| Description: "Use `toStrictEqual()` instead", | ||
| } | ||
| } | ||
|
|
||
| func buildSuggestReplaceWithStrictEqualErrorMessage() rule.RuleMessage { | ||
| return rule.RuleMessage{ | ||
| Id: "suggestReplaceWithStrictEqual", | ||
| Description: "Replace with `toStrictEqual()`", | ||
| } | ||
| } | ||
|
|
||
| var PreferStrictEqualRule = rule.Rule{ | ||
| Name: "jest/prefer-strict-equal", | ||
| Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { | ||
| return rule.RuleListeners{ | ||
| ast.KindCallExpression: func(node *ast.Node) { | ||
| jestFnCall := utils.ParseJestFnCall(node, ctx) | ||
| if jestFnCall == nil || jestFnCall.Kind != utils.JestFnTypeExpect { | ||
| return | ||
| } | ||
|
|
||
| MemberEntries := jestFnCall.MemberEntries | ||
| if len(MemberEntries) == 0 { | ||
| return | ||
| } | ||
|
|
||
| for _, memberEntry := range MemberEntries { | ||
| kind := memberEntry.Node.Kind | ||
| if kind != ast.KindIdentifier && kind != ast.KindStringLiteral { | ||
| continue | ||
| } | ||
|
|
||
| if memberEntry.Name != "toEqual" { | ||
| continue | ||
| } | ||
|
|
||
| ctx.ReportNodeWithSuggestions( | ||
| memberEntry.Node, | ||
| buildUseToStrictEqualErrorMessage(), | ||
| rule.RuleSuggestion{ | ||
| Message: buildSuggestReplaceWithStrictEqualErrorMessage(), | ||
| FixesArr: []rule.RuleFix{ | ||
| {Range: core.NewTextRange(memberEntry.Node.Pos(), memberEntry.Node.End()), Text: "toStrictEqual"}, | ||
|
fansenze marked this conversation as resolved.
Outdated
|
||
| }, | ||
| }, | ||
| ) | ||
| } | ||
|
fansenze marked this conversation as resolved.
|
||
| }, | ||
| } | ||
| }, | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
internal/plugins/jest/rules/prefer_strict_equal/prefer_strict_equal.md
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,21 @@ | ||
| # jest/prefer-strict-equal | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Prefer `toStrictEqual()` over `toEqual()` on `expect()`. It is common to expect objects to not only have identical values but also to have identical keys. A stricter equality will catch cases where two objects do not have identical keys. | ||
|
eryue0220 marked this conversation as resolved.
Outdated
|
||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```javascript | ||
| expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```javascript | ||
| expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); | ||
| ``` | ||
|
|
||
| ## Original Documentation | ||
|
|
||
| - [jest/prefer-strict-equal](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/prefer-strict-equal.md) | ||
73 changes: 73 additions & 0 deletions
73
internal/plugins/jest/rules/prefer_strict_equal/prefer_strict_equal_test.go
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,73 @@ | ||
| package prefer_strict_equal_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/web-infra-dev/rslint/internal/plugins/jest/fixtures" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/jest/rules/prefer_strict_equal" | ||
| "github.com/web-infra-dev/rslint/internal/rule_tester" | ||
| ) | ||
|
|
||
| func TestPreferStrictEqualRule(t *testing.T) { | ||
| rule_tester.RunRuleTester( | ||
| fixtures.GetRootDir(), | ||
| "tsconfig.json", | ||
| t, | ||
| &prefer_strict_equal.PreferStrictEqualRule, | ||
| []rule_tester.ValidTestCase{ | ||
| {Code: `expect(something).toStrictEqual(somethingElse);`}, | ||
|
fansenze marked this conversation as resolved.
|
||
| {Code: `a().toEqual('b')`}, | ||
| {Code: `expect(a);`}, | ||
| }, | ||
| []rule_tester.InvalidTestCase{ | ||
| { | ||
| Code: `expect(something).toEqual(somethingElse);`, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| { | ||
| MessageId: "useToStrictEqual", | ||
| Line: 1, | ||
| Column: 19, | ||
| Suggestions: []rule_tester.InvalidTestCaseSuggestion{ | ||
| { | ||
| MessageId: "suggestReplaceWithStrictEqual", | ||
| Output: `expect(something).toStrictEqual(somethingElse);`, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `expect(something).toEqual(somethingElse,);`, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| { | ||
| MessageId: "useToStrictEqual", | ||
| Line: 1, | ||
| Column: 19, | ||
| Suggestions: []rule_tester.InvalidTestCaseSuggestion{ | ||
| { | ||
| MessageId: "suggestReplaceWithStrictEqual", | ||
| Output: `expect(something).toStrictEqual(somethingElse,);`, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `expect(something)["toEqual"](somethingElse);`, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| { | ||
| MessageId: "useToStrictEqual", | ||
| Line: 1, | ||
| Column: 19, | ||
| Suggestions: []rule_tester.InvalidTestCaseSuggestion{ | ||
| { | ||
| MessageId: "suggestReplaceWithStrictEqual", | ||
| Output: `expect(something)[toStrictEqual](somethingElse);`, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ) | ||
| } | ||
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
61 changes: 61 additions & 0 deletions
61
packages/rslint-test-tools/tests/eslint-plugin-jest/rules/prefer-strict-equal.test.ts
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,61 @@ | ||
| import { RuleTester } from '../rule-tester'; | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('prefer-strict-equal', {} as never, { | ||
| valid: [ | ||
| { code: 'expect(something).toStrictEqual(somethingElse);' }, | ||
|
fansenze marked this conversation as resolved.
|
||
| { code: "a().toEqual('b')" }, | ||
| { code: 'expect(a);' }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'expect(something).toEqual(somethingElse);', | ||
| errors: [ | ||
| { | ||
| messageId: 'useToStrictEqual', | ||
| column: 19, | ||
| line: 1, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'suggestReplaceWithStrictEqual', | ||
| output: 'expect(something).toStrictEqual(somethingElse);', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'expect(something).toEqual(somethingElse,);', | ||
| errors: [ | ||
| { | ||
| messageId: 'useToStrictEqual', | ||
| column: 19, | ||
| line: 1, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'suggestReplaceWithStrictEqual', | ||
| output: 'expect(something).toStrictEqual(somethingElse,);', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'expect(something)["toEqual"](somethingElse);', | ||
| errors: [ | ||
| { | ||
| messageId: 'useToStrictEqual', | ||
| column: 19, | ||
| line: 1, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'suggestReplaceWithStrictEqual', | ||
| output: "expect(something)['toStrictEqual'](somethingElse);", | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
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.