Skip to content

Commit 4e606a5

Browse files
authored
fix(linter): improve jest/no-large-snapshots (#11291)
- re-word diagnostic messages ("Disallow large snapshots." => "Snapshot is too long.") - fix incorrect help message - related: jest-community/eslint-plugin-jest#1736 - add test to include `no_snapshot` diagnostic in test snapshot - minor other code refactorings to improve readability
1 parent 14f790f commit 4e606a5

File tree

2 files changed

+83
-17
lines changed

2 files changed

+83
-17
lines changed

crates/oxc_linter/src/rules/jest/no_large_snapshots.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ use crate::{
1616
utils::{PossibleJestNode, iter_possible_jest_call_node, parse_expect_jest_fn_call},
1717
};
1818

19-
// TODO: re-word diagnostic messages
20-
fn no_snapshot(x0: usize, span: Span) -> OxcDiagnostic {
21-
OxcDiagnostic::warn("Disallow large snapshots.")
22-
.with_help(format!("`{x0:?}`s should begin with lowercase"))
19+
fn no_snapshot(line_count: usize, span: Span) -> OxcDiagnostic {
20+
OxcDiagnostic::warn("Snapshot is too long.")
21+
.with_help(format!(
22+
"Expected to not encounter a Jest snapshot but one was found that is {line_count} lines long"
23+
))
2324
.with_label(span)
2425
}
2526

26-
fn too_long_snapshots(x0: usize, x1: usize, span: Span) -> OxcDiagnostic {
27-
OxcDiagnostic::warn("Disallow large snapshots.")
27+
fn too_long_snapshot(line_limit: usize, line_count: usize, span: Span) -> OxcDiagnostic {
28+
OxcDiagnostic::warn("Snapshot is too long.")
2829
.with_help(format!(
29-
"Expected Jest snapshot to be smaller than {x0:?} lines but was {x1:?} lines long"
30+
"Expected Jest snapshot to be smaller than {line_limit} lines but was {line_count} lines long"
3031
))
3132
.with_label(span)
3233
}
@@ -64,7 +65,6 @@ declare_oxc_lint!(
6465
///
6566
/// ### Example
6667
///
67-
///
6868
/// Examples of **incorrect** code for this rule:
6969
/// ```javascript
7070
/// exports[`a large snapshot 1`] = `
@@ -121,6 +121,7 @@ declare_oxc_lint!(
121121
/// line 51
122122
/// `;
123123
/// ```
124+
///
124125
/// Examples of **incorrect** code for this rule:
125126
/// ```js
126127
/// exports[`a more manageable and readable snapshot 1`] = `
@@ -143,13 +144,15 @@ impl Rule for NoLargeSnapshots {
143144
.and_then(|c| c.get("maxSize"))
144145
.and_then(serde_json::Value::as_number)
145146
.and_then(serde_json::Number::as_u64)
146-
.map_or(50, |v| usize::try_from(v).unwrap_or(50));
147+
.and_then(|v| usize::try_from(v).ok())
148+
.unwrap_or(50);
147149

148150
let inline_max_size = config
149151
.and_then(|c| c.get("inlineMaxSize"))
150152
.and_then(serde_json::Value::as_number)
151153
.and_then(serde_json::Number::as_u64)
152-
.map_or(max_size, |v| usize::try_from(v).unwrap_or(max_size));
154+
.and_then(|v| usize::try_from(v).ok())
155+
.unwrap_or(max_size);
153156

154157
let allowed_snapshots = config
155158
.and_then(|c| c.get("allowedSnapshots"))
@@ -236,7 +239,7 @@ impl NoLargeSnapshots {
236239
if line_count == 0 {
237240
ctx.diagnostic(no_snapshot(line_count, expr_stmt.span));
238241
} else {
239-
ctx.diagnostic(too_long_snapshots(self.max_size, line_count, expr_stmt.span));
242+
ctx.diagnostic(too_long_snapshot(self.max_size, line_count, expr_stmt.span));
240243
}
241244
}
242245
}
@@ -248,7 +251,7 @@ impl NoLargeSnapshots {
248251
if self.inline_max_size == 0 {
249252
ctx.diagnostic(no_snapshot(line_count, span));
250253
} else {
251-
ctx.diagnostic(too_long_snapshots(self.inline_max_size, line_count, span));
254+
ctx.diagnostic(too_long_snapshot(self.inline_max_size, line_count, span));
252255
}
253256
}
254257
}
@@ -341,7 +344,7 @@ fn test() {
341344
// #[cfg(not(target_os = "windows"))]
342345
// let another_snap_path = "/another-mock-component.jsx.snap";
343346

344-
let tow_match_inline_cases = generate_match_inline_snapshot(2);
347+
let two_match_inline_cases = generate_match_inline_snapshot(2);
345348
let two_throw_error_match_cases = generate_throw_error_matching_inline_snapshot(2);
346349
let twenty_match_inline_cases = generate_match_inline_snapshot(20);
347350
let sixty_match_inline_cases = generate_match_inline_snapshot(60);
@@ -365,7 +368,7 @@ fn test() {
365368
("expect(something).toBe(1)", None, None, None),
366369
("expect(something).toMatchInlineSnapshot", None, None, None),
367370
("expect(something).toMatchInlineSnapshot()", None, None, None),
368-
(tow_match_inline_cases.as_str(), None, None, None),
371+
(two_match_inline_cases.as_str(), None, None, None),
369372
(two_throw_error_match_cases.as_str(), None, None, None),
370373
(
371374
twenty_match_inline_cases.as_str(),
@@ -424,6 +427,12 @@ fn test() {
424427
None,
425428
None,
426429
),
430+
(
431+
fifty_throw_error_match_cases.as_str(),
432+
Some(serde_json::json!([{ "maxSize": 0 }])),
433+
None,
434+
None,
435+
),
427436
// '/mock-component.jsx.snap'
428437
// (fifty_two_exports_snapshot.as_str(), None, None, Some(PathBuf::from(snap_path))),
429438
// '/mock-component.jsx.snap'

crates/oxc_linter/src/snapshots/jest_no_large_snapshots.snap

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4-
eslint-plugin-jest(no-large-snapshots): Disallow large snapshots.
4+
eslint-plugin-jest(no-large-snapshots): Snapshot is too long.
55
╭─[no_large_snapshots.tsx:1:41]
66
1 │ ╭─▶ expect(something).toMatchInlineSnapshot(`
77
2 │ │ line
@@ -58,7 +58,7 @@ source: crates/oxc_linter/src/tester.rs
5858
╰────
5959
help: Expected Jest snapshot to be smaller than 50 lines but was 51 lines long
6060

61-
eslint-plugin-jest(no-large-snapshots): Disallow large snapshots.
61+
eslint-plugin-jest(no-large-snapshots): Snapshot is too long.
6262
╭─[no_large_snapshots.tsx:1:54]
6363
1 │ ╭─▶ expect(something).toThrowErrorMatchingInlineSnapshot(`
6464
2 │ │ line
@@ -115,7 +115,7 @@ source: crates/oxc_linter/src/tester.rs
115115
╰────
116116
help: Expected Jest snapshot to be smaller than 50 lines but was 51 lines long
117117

118-
eslint-plugin-jest(no-large-snapshots): Disallow large snapshots.
118+
eslint-plugin-jest(no-large-snapshots): Snapshot is too long.
119119
╭─[no_large_snapshots.tsx:1:54]
120120
1 │ ╭─▶ expect(something).toThrowErrorMatchingInlineSnapshot(`
121121
2 │ │ line
@@ -171,3 +171,60 @@ source: crates/oxc_linter/src/tester.rs
171171
52 │ ╰─▶ `);
172172
╰────
173173
help: Expected Jest snapshot to be smaller than 50 lines but was 51 lines long
174+
175+
eslint-plugin-jest(no-large-snapshots): Snapshot is too long.
176+
╭─[no_large_snapshots.tsx:1:54]
177+
1 │ ╭─▶ expect(something).toThrowErrorMatchingInlineSnapshot(`
178+
2 │ │ line
179+
3 │ │ line
180+
4 │ │ line
181+
5 │ │ line
182+
6 │ │ line
183+
7 │ │ line
184+
8 │ │ line
185+
9 │ │ line
186+
10 │ │ line
187+
11 │ │ line
188+
12 │ │ line
189+
13 │ │ line
190+
14 │ │ line
191+
15 │ │ line
192+
16 │ │ line
193+
17 │ │ line
194+
18 │ │ line
195+
19 │ │ line
196+
20 │ │ line
197+
21 │ │ line
198+
22 │ │ line
199+
23 │ │ line
200+
24 │ │ line
201+
25 │ │ line
202+
26 │ │ line
203+
27 │ │ line
204+
28 │ │ line
205+
29 │ │ line
206+
30 │ │ line
207+
31 │ │ line
208+
32 │ │ line
209+
33 │ │ line
210+
34 │ │ line
211+
35 │ │ line
212+
36 │ │ line
213+
37 │ │ line
214+
38 │ │ line
215+
39 │ │ line
216+
40 │ │ line
217+
41 │ │ line
218+
42 │ │ line
219+
43 │ │ line
220+
44 │ │ line
221+
45 │ │ line
222+
46 │ │ line
223+
47 │ │ line
224+
48 │ │ line
225+
49 │ │ line
226+
50 │ │ line
227+
51 │ │ line
228+
52 │ ╰─▶ `);
229+
╰────
230+
help: Expected to not encounter a Jest snapshot but one was found that is 51 lines long

0 commit comments

Comments
 (0)