Skip to content

Commit bad88a0

Browse files
committed
feat(prefer-expect-assertions): provide suggestions
1 parent 2eaed2b commit bad88a0

File tree

2 files changed

+304
-42
lines changed

2 files changed

+304
-42
lines changed

src/rules/__tests__/prefer-expect-assertions.test.ts

+180-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TSESLint } from '@typescript-eslint/experimental-utils';
2+
import dedent from 'dedent';
23
import resolveFrom from 'resolve-from';
34
import rule from '../prefer-expect-assertions';
45

@@ -13,35 +14,196 @@ ruleTester.run('prefer-expect-assertions', rule, {
1314
invalid: [
1415
{
1516
code: 'it("it1", () => {})',
16-
errors: [{ messageId: 'haveExpectAssertions' }],
17+
errors: [
18+
{
19+
messageId: 'haveExpectAssertions',
20+
column: 1,
21+
line: 1,
22+
suggestions: [
23+
{
24+
messageId: 'suggestAddingHasAssertions',
25+
output: 'it("it1", () => {expect.hasAssertions();})',
26+
},
27+
{
28+
messageId: 'suggestAddingAssertions',
29+
output: 'it("it1", () => {expect.assertions();})',
30+
},
31+
],
32+
},
33+
],
1734
},
1835
{
1936
code: 'it("it1", () => { foo()})',
20-
errors: [{ messageId: 'haveExpectAssertions' }],
37+
errors: [
38+
{
39+
messageId: 'haveExpectAssertions',
40+
column: 1,
41+
line: 1,
42+
suggestions: [
43+
{
44+
messageId: 'suggestAddingHasAssertions',
45+
output: 'it("it1", () => { expect.hasAssertions();foo()})',
46+
},
47+
{
48+
messageId: 'suggestAddingAssertions',
49+
output: 'it("it1", () => { expect.assertions();foo()})',
50+
},
51+
],
52+
},
53+
],
2154
},
2255
{
23-
code:
24-
'it("it1", function() {' +
25-
'\n\t\t\tsomeFunctionToDo();' +
26-
'\n\t\t\tsomeFunctionToDo2();\n' +
27-
'\t\t\t})',
28-
errors: [{ messageId: 'haveExpectAssertions' }],
56+
code: dedent`
57+
it("it1", function() {
58+
someFunctionToDo();
59+
someFunctionToDo2();
60+
})
61+
`,
62+
errors: [
63+
{
64+
messageId: 'haveExpectAssertions',
65+
column: 1,
66+
line: 1,
67+
suggestions: [
68+
{
69+
messageId: 'suggestAddingHasAssertions',
70+
output: dedent`
71+
it("it1", function() {
72+
expect.hasAssertions();someFunctionToDo();
73+
someFunctionToDo2();
74+
})
75+
`,
76+
},
77+
{
78+
messageId: 'suggestAddingAssertions',
79+
output: dedent`
80+
it("it1", function() {
81+
expect.assertions();someFunctionToDo();
82+
someFunctionToDo2();
83+
})
84+
`,
85+
},
86+
],
87+
},
88+
],
2989
},
3090
{
3191
code: 'it("it1", function() {var a = 2;})',
32-
errors: [{ messageId: 'haveExpectAssertions' }],
92+
errors: [
93+
{
94+
messageId: 'haveExpectAssertions',
95+
column: 1,
96+
line: 1,
97+
suggestions: [
98+
{
99+
messageId: 'suggestAddingHasAssertions',
100+
output:
101+
'it("it1", function() {expect.hasAssertions();var a = 2;})',
102+
},
103+
{
104+
messageId: 'suggestAddingAssertions',
105+
output: 'it("it1", function() {expect.assertions();var a = 2;})',
106+
},
107+
],
108+
},
109+
],
33110
},
34111
{
35112
code: 'it("it1", function() {expect.assertions();})',
36-
errors: [{ messageId: 'haveExpectAssertions' }],
113+
errors: [
114+
{
115+
messageId: 'assertionsRequiresOneArgument',
116+
column: 30,
117+
line: 1,
118+
suggestions: [],
119+
},
120+
],
37121
},
38122
{
39123
code: 'it("it1", function() {expect.assertions(1,2);})',
40-
errors: [{ messageId: 'haveExpectAssertions' }],
124+
errors: [
125+
{
126+
messageId: 'assertionsRequiresOneArgument',
127+
column: 43,
128+
line: 1,
129+
suggestions: [
130+
{
131+
messageId: 'suggestRemovingExtraArguments',
132+
output: 'it("it1", function() {expect.assertions(1);})',
133+
},
134+
],
135+
},
136+
],
41137
},
42138
{
43139
code: 'it("it1", function() {expect.assertions("1");})',
44-
errors: [{ messageId: 'haveExpectAssertions' }],
140+
errors: [
141+
{
142+
messageId: 'assertionsRequiresNumberArgument',
143+
column: 41,
144+
line: 1,
145+
suggestions: [],
146+
},
147+
],
148+
},
149+
{
150+
code: 'it("it1", function() {expect.hasAssertions("1");})',
151+
errors: [
152+
{
153+
messageId: 'hasAssertionsTakesNoArguments',
154+
column: 30,
155+
line: 1,
156+
suggestions: [
157+
{
158+
messageId: 'suggestRemovingExtraArguments',
159+
output: 'it("it1", function() {expect.hasAssertions();})',
160+
},
161+
],
162+
},
163+
],
164+
},
165+
{
166+
code: 'it("it1", function() {expect.hasAssertions("1", "2");})',
167+
errors: [
168+
{
169+
messageId: 'hasAssertionsTakesNoArguments',
170+
column: 30,
171+
line: 1,
172+
suggestions: [
173+
{
174+
messageId: 'suggestRemovingExtraArguments',
175+
output: 'it("it1", function() {expect.hasAssertions();})',
176+
},
177+
],
178+
},
179+
],
180+
},
181+
{
182+
code: dedent`
183+
it("it1", function() {
184+
expect.hasAssertions(() => {
185+
someFunctionToDo();
186+
someFunctionToDo2();
187+
});
188+
})
189+
`,
190+
errors: [
191+
{
192+
messageId: 'hasAssertionsTakesNoArguments',
193+
column: 10,
194+
line: 2,
195+
suggestions: [
196+
{
197+
messageId: 'suggestRemovingExtraArguments',
198+
output: dedent`
199+
it("it1", function() {
200+
expect.hasAssertions();
201+
})
202+
`,
203+
},
204+
],
205+
},
206+
],
45207
},
46208
],
47209

@@ -52,9 +214,12 @@ ruleTester.run('prefer-expect-assertions', rule, {
52214
'test("it1", function() {expect.assertions(0);})',
53215
'test("it1", function() {expect.hasAssertions();})',
54216
'it("it1", function() {expect.assertions(0);})',
55-
'it("it1", function() {\n\t\t\texpect.assertions(1);' +
56-
'\n\t\t\texpect(someValue).toBe(true)\n' +
57-
'\t\t\t})',
217+
`
218+
it("it1", function() {
219+
expect.assertions(1);
220+
expect(someValue).toBe(true)
221+
})
222+
`,
58223
'test("it1")',
59224
'itHappensToStartWithIt("foo", function() {})',
60225
'testSomething("bar", function() {})',

0 commit comments

Comments
 (0)