1
1
import { TSESLint } from '@typescript-eslint/experimental-utils' ;
2
+ import dedent from 'dedent' ;
2
3
import resolveFrom from 'resolve-from' ;
3
4
import rule from '../prefer-expect-assertions' ;
4
5
@@ -13,35 +14,196 @@ ruleTester.run('prefer-expect-assertions', rule, {
13
14
invalid : [
14
15
{
15
16
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
+ ] ,
17
34
} ,
18
35
{
19
36
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
+ ] ,
21
54
} ,
22
55
{
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
+ ] ,
29
89
} ,
30
90
{
31
91
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
+ ] ,
33
110
} ,
34
111
{
35
112
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
+ ] ,
37
121
} ,
38
122
{
39
123
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
+ ] ,
41
137
} ,
42
138
{
43
139
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
+ ] ,
45
207
} ,
46
208
] ,
47
209
@@ -52,9 +214,12 @@ ruleTester.run('prefer-expect-assertions', rule, {
52
214
'test("it1", function() {expect.assertions(0);})' ,
53
215
'test("it1", function() {expect.hasAssertions();})' ,
54
216
'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
+ ` ,
58
223
'test("it1")' ,
59
224
'itHappensToStartWithIt("foo", function() {})' ,
60
225
'testSomething("bar", function() {})' ,
0 commit comments