Skip to content

Commit 31026e1

Browse files
Use snapshots in rule tests (#709)
* Use snapshots in rule tests * Relative file name * Removed unused rule tester file * Update tools and docs
1 parent aacead4 commit 31026e1

File tree

167 files changed

+18368
-12238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+18368
-12238
lines changed

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ The following steps will walk you through the process of creating a new rule.
5656

5757
1. Test your rule:
5858

59-
Add test for every feature and option of your rule. (We use [ESLint's `RuleTester`](https://eslint.org/docs/developer-guide/nodejs-api#ruletester) for testing rules.)
59+
Add test for every feature and option of your rule. We use a snapshot-based rule tester for testing rules.
6060

61-
Use `npm test` to run all tests.
61+
Use `npm test` to run all tests. Use `npm run test:update` to update snapshots.
6262

6363
1. Document your rule:
6464

package-lock.json

+62-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"test": "npm run test:nyc",
2323
"test:nyc": "nyc --reporter=lcov npm run test:base",
2424
"test:watch": "npm run test:base -- --watch",
25+
"test:update": "npm run test:base -- --update",
2526
"update": "ts-node --transpile-only ./tools/update.ts && npm run eslint-fix && npm run update:eslint-docs",
2627
"update:eslint-docs": "npm run build && eslint-doc-generator",
2728
"new": "ts-node ./tools/new-rule.ts",
@@ -86,6 +87,7 @@
8687
"eslint-plugin-regexp": "~2.3.0",
8788
"eslint-plugin-vue": "^9.0.0",
8889
"eslint-plugin-yml": "^1.0.0",
90+
"eslint-snapshot-rule-tester": "^0.1.0",
8991
"intl-segmenter-polyfill": "^0.4.4",
9092
"markdownlint-cli": "^0.39.0",
9193
"mocha": "^10.0.0",

tests/lib/rule-tester.ts

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# eslint-snapshot-rule-tester format: v1
2+
3+
4+
Test: confusing-quantifier >> invalid
5+
Code:
6+
1 | /(a?){5}/
7+
| ^~~ [1]
8+
9+
[1] This quantifier is confusing because its minimum is 5 but it can match the empty string. Maybe replace it with `{0,5}` to reflect that it can match the empty string?
10+
---
11+
12+
13+
Test: confusing-quantifier >> invalid
14+
Code:
15+
1 | /(?:a?b*|c+){4}/
16+
| ^~~ [1]
17+
18+
[1] This quantifier is confusing because its minimum is 4 but it can match the empty string. Maybe replace it with `{0,4}` to reflect that it can match the empty string?
19+
---
20+
21+
22+
Test: confusing-quantifier >> invalid
23+
Code:
24+
1 | /[\q{a|}]+/v
25+
| ^ [1]
26+
27+
[1] This quantifier is confusing because its minimum is 1 but it can match the empty string. Maybe replace it with `*` to reflect that it can match the empty string?
28+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# eslint-snapshot-rule-tester format: v1
2+
3+
4+
Test: control-character-escape >> invalid
5+
Code:
6+
1 | /\x00/
7+
| ^~~~ [1]
8+
9+
Output:
10+
1 | /\0/
11+
12+
[1] Unexpected control character escape '\x00' (U+0000). Use '\0' instead.
13+
---
14+
15+
16+
Test: control-character-escape >> invalid
17+
Code:
18+
1 | /\x0a/
19+
| ^~~~ [1]
20+
21+
Output:
22+
1 | /\n/
23+
24+
[1] Unexpected control character escape '\x0a' (U+000a). Use '\n' instead.
25+
---
26+
27+
28+
Test: control-character-escape >> invalid
29+
Code:
30+
1 | /\cJ/
31+
| ^~~ [1]
32+
33+
Output:
34+
1 | /\n/
35+
36+
[1] Unexpected control character escape '\cJ' (U+000a). Use '\n' instead.
37+
---
38+
39+
40+
Test: control-character-escape >> invalid
41+
Code:
42+
1 | /\u{a}/u
43+
| ^~~~~ [1]
44+
45+
Output:
46+
1 | /\n/u
47+
48+
[1] Unexpected control character escape '\u{a}' (U+000a). Use '\n' instead.
49+
---
50+
51+
52+
Test: control-character-escape >> invalid
53+
Code:
54+
1 | RegExp("\\cJ")
55+
| ^~~~ [1]
56+
57+
Output:
58+
1 | RegExp("\\n")
59+
60+
[1] Unexpected control character escape '\cJ' (U+000a). Use '\n' instead.
61+
---
62+
63+
64+
Test: control-character-escape >> invalid
65+
Code:
66+
1 | RegExp("\\u{a}", "u")
67+
| ^~~~~~ [1]
68+
69+
Output:
70+
1 | RegExp("\\n", "u")
71+
72+
[1] Unexpected control character escape '\u{a}' (U+000a). Use '\n' instead.
73+
---
74+
75+
76+
Test: control-character-escape >> invalid
77+
Code:
78+
1 | /\u0009/
79+
| ^~~~~~ [1]
80+
81+
Output:
82+
1 | /\t/
83+
84+
[1] Unexpected control character escape '\u0009' (U+0009). Use '\t' instead.
85+
---
86+
87+
88+
Test: control-character-escape >> invalid >>> /\u0009/ 2
89+
Code:
90+
1 | / /
91+
| ^ [1]
92+
93+
Output:
94+
1 | /\t/
95+
96+
[1] Unexpected control character escape ' ' (U+0009). Use '\t' instead.
97+
---
98+
99+
100+
Test: control-character-escape >> invalid
101+
Code:
102+
1 |
103+
2 | const s = "\\u0009"
104+
| ^~~~~~~ [1]
105+
3 | new RegExp(s)
106+
4 |
107+
108+
Output:
109+
1 |
110+
2 | const s = "\\t"
111+
3 | new RegExp(s)
112+
4 |
113+
114+
[1] Unexpected control character escape '\u0009' (U+0009). Use '\t' instead.
115+
---
116+
117+
118+
Test: control-character-escape >> invalid
119+
Code:
120+
1 |
121+
2 | const s = "\\u"+"0009"
122+
| ^~~~~~~~~~~~ [1]
123+
3 | new RegExp(s)
124+
4 |
125+
126+
Output: unchanged
127+
128+
[1] Unexpected control character escape '\u0009' (U+0009). Use '\t' instead.
129+
---
130+
131+
132+
Test: control-character-escape >> invalid >>> RegExp("\t\r\n\0" + /\u0009/.source)
133+
Code:
134+
1 | RegExp("\t\r\n\0" + / /.source)
135+
| ^ [1]
136+
137+
Output:
138+
1 | RegExp("\t\r\n\0" + /\t/.source)
139+
140+
[1] Unexpected control character escape ' ' (U+0009). Use '\t' instead.
141+
---
142+
143+
144+
Test: control-character-escape >> invalid
145+
Code:
146+
1 | /[\q{\x00}]/v
147+
| ^~~~ [1]
148+
149+
Output:
150+
1 | /[\q{\0}]/v
151+
152+
[1] Unexpected control character escape '\x00' (U+0000). Use '\0' instead.
153+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# eslint-snapshot-rule-tester format: v1
2+
3+
4+
Test: grapheme-string-literal >> invalid
5+
Code:
6+
1 | /[\q{abc}]/v
7+
| ^~~ [1]
8+
9+
[1] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:abc|[...])`) for strings instead.
10+
---
11+
12+
13+
Test: grapheme-string-literal >> invalid
14+
Code:
15+
1 | /[\q{a|bc|}]/v
16+
| ^~ [1]
17+
18+
[1] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:bc|[...])`) for strings instead.
19+
---
20+
21+
22+
Test: grapheme-string-literal >> invalid
23+
Code:
24+
1 | /[\q{🇦🇨🇦🇩}]/v
25+
| ^~~~~~~~ [1]
26+
27+
[1] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:🇦🇨🇦🇩|[...])`) for strings instead.
28+
---
29+
30+
31+
Test: grapheme-string-literal >> invalid
32+
Code:
33+
1 | /[\q{abc|def|ghi|j|k|lm|n}]/v
34+
| ^~~ ^~~ ^~~ ^~
35+
| [1] [2] [3] [4]
36+
37+
[1] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:abc|def|ghi|lm|[...])`) for strings instead.
38+
[2] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:abc|def|ghi|lm|[...])`) for strings instead.
39+
[3] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:abc|def|ghi|lm|[...])`) for strings instead.
40+
[4] Only single characters and graphemes are allowed inside character classes. Use regular alternatives (e.g. `(?:abc|def|ghi|lm|[...])`) for strings instead.
41+
---

0 commit comments

Comments
 (0)