@@ -23,8 +23,10 @@ const stdout = () => {
23
23
return `\n${ result } ` ;
24
24
} ;
25
25
26
- const expectAutofixCommit = ( ) => {
27
- expect ( runESLint ) . toHaveBeenCalledTimes ( 1 ) ;
26
+ const expectAutofixCommit = (
27
+ { eslint } : { eslint : boolean } = { eslint : true } ,
28
+ ) => {
29
+ expect ( runESLint ) . toHaveBeenCalledTimes ( eslint ? 1 : 0 ) ;
28
30
expect ( runPrettier ) . toHaveBeenCalledTimes ( 1 ) ;
29
31
expect ( Git . commitAllChanges ) . toHaveBeenCalledTimes ( 1 ) ;
30
32
} ;
@@ -51,26 +53,28 @@ beforeEach(() => {
51
53
afterEach ( jest . resetAllMocks ) ;
52
54
53
55
describe ( 'autofix' , ( ) => {
56
+ const params = { debug : false , eslint : true , prettier : true } ;
57
+
54
58
it ( 'bails on a non-CI environment' , async ( ) => {
55
59
delete process . env . CI ;
56
60
57
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
61
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
58
62
59
63
expectNoAutofix ( ) ;
60
64
} ) ;
61
65
62
66
it ( 'bails on the master branch' , async ( ) => {
63
67
jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'master' ) ;
64
68
65
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
69
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
66
70
67
71
expectNoAutofix ( ) ;
68
72
} ) ;
69
73
70
74
it ( 'bails on the main branch' , async ( ) => {
71
75
jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'main' ) ;
72
76
73
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
77
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
74
78
75
79
expectNoAutofix ( ) ;
76
80
} ) ;
@@ -80,7 +84,7 @@ describe('autofix', () => {
80
84
81
85
jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'devel' ) ;
82
86
83
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
87
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
84
88
85
89
expectNoAutofix ( ) ;
86
90
} ) ;
@@ -90,7 +94,7 @@ describe('autofix', () => {
90
94
91
95
jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'beta' ) ;
92
96
93
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
97
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
94
98
95
99
expectNoAutofix ( ) ;
96
100
} ) ;
@@ -101,15 +105,23 @@ describe('autofix', () => {
101
105
. mocked ( Git . getHeadCommitMessage )
102
106
. mockResolvedValue ( 'Run `skuba format`' ) ;
103
107
104
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
108
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
109
+
110
+ expectNoAutofix ( ) ;
111
+ } ) ;
112
+
113
+ it ( 'bails on no fixable issues' , async ( ) => {
114
+ await expect (
115
+ autofix ( { ...params , eslint : false , prettier : false } ) ,
116
+ ) . resolves . toBeUndefined ( ) ;
105
117
106
118
expectNoAutofix ( ) ;
107
119
} ) ;
108
120
109
121
it ( 'skips push on empty commit' , async ( ) => {
110
122
jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( undefined ) ;
111
123
112
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
124
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
113
125
114
126
expectAutofixCommit ( ) ;
115
127
expect ( Git . push ) . not . toHaveBeenCalled ( ) ;
@@ -131,7 +143,7 @@ describe('autofix', () => {
131
143
132
144
jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
133
145
134
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
146
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
135
147
136
148
expectAutofixCommit ( ) ;
137
149
@@ -151,7 +163,7 @@ describe('autofix', () => {
151
163
jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
152
164
jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'dev' ) ;
153
165
154
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
166
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
155
167
156
168
expectAutofixCommit ( ) ;
157
169
expect ( Git . push ) . toHaveBeenNthCalledWith ( 1 , {
@@ -170,6 +182,58 @@ describe('autofix', () => {
170
182
` ) ;
171
183
} ) ;
172
184
185
+ it ( 'handles fixable issues from ESLint only' , async ( ) => {
186
+ jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
187
+ jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'dev' ) ;
188
+
189
+ await expect (
190
+ autofix ( { ...params , eslint : true , prettier : false } ) ,
191
+ ) . resolves . toBeUndefined ( ) ;
192
+
193
+ expectAutofixCommit ( ) ;
194
+ expect ( Git . push ) . toHaveBeenNthCalledWith ( 1 , {
195
+ auth : { type : 'gitHubApp' } ,
196
+ dir : expect . any ( String ) ,
197
+ ref : 'commit-sha' ,
198
+ remoteRef : 'dev' ,
199
+ } ) ;
200
+
201
+ // We should run both ESLint and Prettier
202
+ expect ( stdout ( ) ) . toMatchInlineSnapshot ( `
203
+ "
204
+
205
+ Trying to autofix with ESLint and Prettier...
206
+ Pushed fix commit commit-sha.
207
+ "
208
+ ` ) ;
209
+ } ) ;
210
+
211
+ it ( 'handles fixable issues from Prettier only' , async ( ) => {
212
+ jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
213
+ jest . mocked ( Git . currentBranch ) . mockResolvedValue ( 'dev' ) ;
214
+
215
+ await expect (
216
+ autofix ( { ...params , eslint : false , prettier : true } ) ,
217
+ ) . resolves . toBeUndefined ( ) ;
218
+
219
+ expectAutofixCommit ( { eslint : false } ) ;
220
+ expect ( Git . push ) . toHaveBeenNthCalledWith ( 1 , {
221
+ auth : { type : 'gitHubApp' } ,
222
+ dir : expect . any ( String ) ,
223
+ ref : 'commit-sha' ,
224
+ remoteRef : 'dev' ,
225
+ } ) ;
226
+
227
+ // We should only run Prettier
228
+ expect ( stdout ( ) ) . toMatchInlineSnapshot ( `
229
+ "
230
+
231
+ Trying to autofix with Prettier...
232
+ Pushed fix commit commit-sha.
233
+ "
234
+ ` ) ;
235
+ } ) ;
236
+
173
237
it ( 'tolerates guard errors' , async ( ) => {
174
238
const ERROR = new Error ( 'badness!' ) ;
175
239
@@ -178,7 +242,7 @@ describe('autofix', () => {
178
242
179
243
jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
180
244
181
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
245
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
182
246
183
247
expectAutofixCommit ( ) ;
184
248
expect ( Git . push ) . toHaveBeenNthCalledWith ( 1 , {
@@ -199,7 +263,7 @@ describe('autofix', () => {
199
263
it ( 'bails on commit error' , async ( ) => {
200
264
jest . mocked ( Git . commitAllChanges ) . mockRejectedValue ( MOCK_ERROR ) ;
201
265
202
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
266
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
203
267
204
268
expectAutofixCommit ( ) ;
205
269
expect ( Git . push ) . not . toHaveBeenCalled ( ) ;
@@ -219,7 +283,7 @@ describe('autofix', () => {
219
283
jest . mocked ( Git . commitAllChanges ) . mockResolvedValue ( 'commit-sha' ) ;
220
284
jest . mocked ( Git . push ) . mockRejectedValue ( MOCK_ERROR ) ;
221
285
222
- await expect ( autofix ( { debug : false } ) ) . resolves . toBeUndefined ( ) ;
286
+ await expect ( autofix ( params ) ) . resolves . toBeUndefined ( ) ;
223
287
224
288
expectAutofixCommit ( ) ;
225
289
expect ( Git . push ) . toHaveBeenNthCalledWith ( 1 , {
0 commit comments