-
Notifications
You must be signed in to change notification settings - Fork 35
feat(jest,rstest): add prefer-snapshot-hint rule #2293
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6540947
feat(jest,rstest): add prefer-snapshot-hint rule
elecmonkey f3521a0
fix(jest,rstest): address prefer-snapshot-hint review findings
elecmonkey 83e4757
fix(jest,rstest): harden snapshot callback resolution
elecmonkey d6ec397
fix(jest): handle wrapped callbacks and chained snapshots
elecmonkey 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
34 changes: 34 additions & 0 deletions
34
internal/plugins/jest/rules/prefer_snapshot_hint/prefer_snapshot_hint.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,34 @@ | ||
| package prefer_snapshot_hint | ||
|
|
||
| import ( | ||
| "github.com/microsoft/TypeScript/tsc/shim/ast" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/jest/utils" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| shared "github.com/web-infra-dev/rslint/internal/utils/test_framework/rules/prefer_snapshot_hint" | ||
| ) | ||
|
|
||
| var PreferSnapshotHintRule = shared.NewRule(shared.Config{ | ||
| Name: "jest/prefer-snapshot-hint", | ||
| Prepare: func(ctx rule.RuleContext) shared.Runtime { | ||
| analysis := utils.GetJestCallAnalysis(ctx) | ||
| return shared.Runtime{ | ||
| RegistrationCallbacks: analysis.RegistrationCallbacks(), | ||
| IsRegistration: func(node *ast.Node) bool { | ||
| parsed := analysis.ParseFnCall(node) | ||
| return parsed != nil && (parsed.Kind == utils.JestFnTypeTest || parsed.Kind == utils.JestFnTypeDescribe) | ||
| }, | ||
| Snapshots: func(node *ast.Node) []shared.Snapshot { | ||
| parsed := analysis.ParseExpectCall(node) | ||
| if parsed == nil || parsed.MatcherEntry == nil || | ||
| (parsed.Matcher != "toMatchSnapshot" && parsed.Matcher != "toThrowErrorMatchingSnapshot") { | ||
| return nil | ||
| } | ||
| matcherCall := parsed.MatcherEntry.Call | ||
| if matcherCall == nil || matcherCall != node { | ||
| return nil | ||
| } | ||
| return []shared.Snapshot{{Matcher: parsed.MatcherEntry.Node, Args: matcherCall.Arguments(), Properties: parsed.Matcher == "toMatchSnapshot"}} | ||
| }, | ||
| } | ||
| }, | ||
| }) |
40 changes: 40 additions & 0 deletions
40
internal/plugins/jest/rules/prefer_snapshot_hint/prefer_snapshot_hint.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,40 @@ | ||
| # prefer-snapshot-hint | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Prefer including a descriptive hint with external snapshots. Hints make snapshots easier to identify during review and updates. | ||
|
|
||
| The rule checks `toMatchSnapshot` and `toThrowErrorMatchingSnapshot`. Inline snapshots are ignored. The default `"multi"` mode requires hints in groups containing multiple external snapshot assertions; `"always"` requires a hint on every external snapshot assertion. Function expressions, arrow functions, methods, and accessors establish groups that include nested helper expressions, while test and suite registrations reset the grouping boundary. Function declarations do not establish separate groups. | ||
|
|
||
| For `toMatchSnapshot`, a single string literal or template without interpolation counts as a hint, and two arguments count as properties plus a hint. A single variable or interpolated template is reported, regardless of its type. `toThrowErrorMatchingSnapshot` accepts a single hint argument. This rule does not require type information or provide fixes. | ||
|
|
||
| ## Incorrect | ||
|
|
||
| ```js | ||
| test('cart totals', () => { | ||
| expect(cart.subtotal).toMatchSnapshot(); | ||
| expect(cart.total).toMatchSnapshot(); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Correct | ||
|
|
||
| ```js | ||
| test('cart totals', () => { | ||
| expect(cart.subtotal).toMatchSnapshot('before tax'); | ||
| expect(cart.total).toMatchSnapshot('after tax'); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| ```json | ||
| { | ||
| "jest/prefer-snapshot-hint": ["error", "always"] | ||
| } | ||
| ``` | ||
|
|
||
| ## Original Documentation | ||
|
|
||
| - [eslint-plugin-jest: prefer-snapshot-hint](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.6/docs/rules/prefer-snapshot-hint.md) | ||
| - [Source code](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.6/src/rules/prefer-snapshot-hint.ts) |
281 changes: 281 additions & 0 deletions
281
internal/plugins/jest/rules/prefer_snapshot_hint/prefer_snapshot_hint_extras_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
167 changes: 167 additions & 0 deletions
167
internal/plugins/jest/rules/prefer_snapshot_hint/prefer_snapshot_hint_upstream_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
43 changes: 43 additions & 0 deletions
43
internal/plugins/rstest/rules/prefer_snapshot_hint/prefer_snapshot_hint.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,43 @@ | ||
| package prefer_snapshot_hint | ||
|
|
||
| import ( | ||
| "github.com/microsoft/TypeScript/tsc/shim/ast" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/rstest/utils" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| shared "github.com/web-infra-dev/rslint/internal/utils/test_framework/rules/prefer_snapshot_hint" | ||
| ) | ||
|
|
||
| var PreferSnapshotHintRule = shared.NewRule(shared.Config{ | ||
| Name: "rstest/prefer-snapshot-hint", | ||
| AllowInterpolatedHints: true, | ||
| Prepare: func(ctx rule.RuleContext) shared.Runtime { | ||
| analysis := utils.GetRstestCallAnalysis(ctx) | ||
| return shared.Runtime{ | ||
| RegistrationCallbacks: analysis.RegistrationCallbacks(), | ||
| IsRegistration: func(node *ast.Node) bool { | ||
| parsed := analysis.ParseFnCall(node) | ||
| return parsed != nil && (parsed.Kind == utils.RstestFnTypeTest || parsed.Kind == utils.RstestFnTypeDescribe) | ||
| }, | ||
| Snapshots: func(node *ast.Node) []shared.Snapshot { | ||
| parsed := analysis.ParseExpectCall(node) | ||
| if parsed == nil || parsed.Head == nil || | ||
| parsed.Entry == utils.RstestExpectEntryPoll || parsed.Entry == utils.RstestExpectEntryElement { | ||
| return nil | ||
| } | ||
| var snapshots []shared.Snapshot | ||
| for _, matcher := range parsed.Matchers { | ||
| if matcher.Negated || matcher.Entry.Call == nil || | ||
| (matcher.Name != "toMatchSnapshot" && matcher.Name != "matchSnapshot" && matcher.Name != "toThrowErrorMatchingSnapshot") { | ||
| continue | ||
| } | ||
| snapshots = append(snapshots, shared.Snapshot{ | ||
| Matcher: matcher.Entry.Node, | ||
| Args: matcher.Entry.Call.Arguments(), | ||
| Properties: matcher.Name != "toThrowErrorMatchingSnapshot", | ||
| }) | ||
| } | ||
| return snapshots | ||
| }, | ||
| } | ||
| }, | ||
| }) |
45 changes: 45 additions & 0 deletions
45
internal/plugins/rstest/rules/prefer_snapshot_hint/prefer_snapshot_hint.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,45 @@ | ||
| # prefer-snapshot-hint | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Prefer descriptive hints for external snapshots so their names explain what each snapshot captures. By default, hints are required when a group contains more than one external snapshot assertion. See [Rstest snapshot naming](https://rstest.rs/guide/basic/snapshot#use-descriptive-snapshot-names). | ||
|
|
||
| The rule checks `toMatchSnapshot`, its `matchSnapshot` alias, and `toThrowErrorMatchingSnapshot`, including Chai assertion chains, `expect.soft`, and `resolves`/`rejects`. It recognizes globals, named and renamed imports, namespace imports, CommonJS imports, `rstack/test`, `import.meta.rstest`, and test-context `expect`, including parameterized and extended tests. The Rstest assertions exposed by `@rstest/playwright` are also checked. Locally shadowed or reassigned assertion bindings are ignored. Static bracket access and optional calls are recognized. Inline snapshots, file snapshots, static `expect` methods, negated snapshots, and `expect.poll`/`expect.element` chains are excluded. An assertion wrapped with `as`, `satisfies`, or a non-null assertion before the matcher access is not inspected. | ||
|
|
||
| Groups follow function expressions, arrow functions, methods, and accessors, including nested helper expressions. Each test or suite registration starts a new grouping boundary. Function declarations do not create a separate group, and helper calls are not followed to count runtime assertions. A hinted snapshot still contributes to its group's count. | ||
|
|
||
| For `toMatchSnapshot` and `matchSnapshot`, a single string literal or template literal counts as a hint; two arguments count as properties plus a hint. A single variable or other expression is treated as properties, even if its type is `string`. `toThrowErrorMatchingSnapshot` accepts one hint argument. The rule does not validate hint contents or argument types and does not require type information. | ||
|
|
||
| ## Incorrect | ||
|
|
||
| ```ts | ||
| import { expect, test } from '@rstest/core'; | ||
|
|
||
| test('cart totals', () => { | ||
| expect(cart.subtotal).toMatchSnapshot(); | ||
| expect(cart.total).toMatchSnapshot(); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Correct | ||
|
|
||
| ```ts | ||
| import { expect, test } from '@rstest/core'; | ||
|
|
||
| test('cart totals', () => { | ||
| expect(cart.subtotal).toMatchSnapshot('before tax'); | ||
| expect(cart.total).toMatchSnapshot('after tax'); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| ```json | ||
| { | ||
| "rstest/prefer-snapshot-hint": ["error", "always"] | ||
| } | ||
| ``` | ||
|
|
||
| | Option | Type | Default | Description | | ||
| | ------ | ---- | ------- | ----------- | | ||
| | mode | string | `"multi"` | Use `"always"` to require hints for every external snapshot, or `"multi"` for groups with multiple external snapshots. | |
Oops, something went wrong.
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.