@@ -12,27 +12,11 @@ import * as github from '@actions/github'
12
12
import * as main from '../src/main'
13
13
import * as utils from '../src/utils'
14
14
15
- // Mock the GitHub Actions core library
15
+ // Mock the GitHub Actions library
16
16
const infoMock = jest . spyOn ( core , 'info' )
17
- const setFailedMock = jest
18
- . spyOn ( core , 'setFailed' )
19
- . mockImplementation ( console . debug )
20
- const inputMock = jest
21
- . spyOn ( core , 'getInput' )
22
- . mockImplementation ( ( name : string ) : string => {
23
- switch ( name ) {
24
- case 'changed_files' :
25
- return JSON . stringify ( [
26
- './packages/pkg1/file1.css' ,
27
- './packages/pkgB/file2.tsx'
28
- ] )
29
- default :
30
- return ''
31
- }
32
- } )
33
-
34
- jest . spyOn ( exec , 'exec' ) . mockImplementation ( async ( ) => 0 )
35
- const getExecOutputMock = jest . spyOn ( exec , 'getExecOutput' )
17
+ const setFailedMock = jest . spyOn ( core , 'setFailed' )
18
+ const inputMock = jest . spyOn ( core , 'getInput' )
19
+ const execSpy = jest . spyOn ( exec , 'exec' )
36
20
37
21
const originalContext = github . context
38
22
@@ -41,7 +25,18 @@ const runMock = jest.spyOn(main, 'run')
41
25
42
26
describe ( 'action' , ( ) => {
43
27
beforeEach ( ( ) => {
44
- jest . clearAllMocks ( )
28
+ inputMock . mockImplementation ( ( name : string ) : string => {
29
+ switch ( name ) {
30
+ case 'changed_files' :
31
+ return JSON . stringify ( [
32
+ './packages/pkg1/file1.css' ,
33
+ './packages/pkgB/file2.tsx'
34
+ ] )
35
+ default :
36
+ return ''
37
+ }
38
+ } )
39
+ setFailedMock . mockImplementation ( console . debug )
45
40
} )
46
41
47
42
afterEach ( ( ) => {
@@ -84,7 +79,8 @@ describe('action', () => {
84
79
}
85
80
}
86
81
} )
87
- getExecOutputMock
82
+ jest
83
+ . spyOn ( exec , 'getExecOutput' )
88
84
// workspaces info
89
85
. mockImplementationOnce ( async ( ) => ( {
90
86
stdout : JSON . stringify ( {
@@ -101,6 +97,12 @@ describe('action', () => {
101
97
stderr : '' ,
102
98
exitCode : 0
103
99
} ) )
100
+ // check changeset installed
101
+ . mockImplementationOnce ( async ( ) => ( {
102
+ stdout : '' ,
103
+ stderr : '' ,
104
+ exitCode : 0
105
+ } ) )
104
106
// changeset info
105
107
. mockImplementationOnce ( async ( ) => ( {
106
108
stdout : `🦋 info Packages to be bumped at patch:
@@ -123,6 +125,7 @@ describe('action', () => {
123
125
// Assert
124
126
expect ( runMock ) . toHaveReturned ( )
125
127
expect ( setFailedMock ) . not . toHaveBeenCalled ( )
128
+ expect ( execSpy ) . not . toHaveBeenCalled ( )
126
129
127
130
// Verify that all of the core library functions were called correctly
128
131
expect ( infoMock ) . toHaveBeenNthCalledWith (
@@ -135,6 +138,74 @@ describe('action', () => {
135
138
)
136
139
} )
137
140
141
+ it ( 'installs changeset when not installed' , async ( ) => {
142
+ // Arrange
143
+ jest . spyOn ( utils , 'getChangesets' ) . mockImplementation (
144
+ async ( ) : Promise < utils . Changesets > =>
145
+ Promise . resolve ( {
146
+ releases : [
147
+ {
148
+ name : '@owner/pkg1' ,
149
+ type : 'patch'
150
+ } ,
151
+ {
152
+ name : '@owner/pkgB' ,
153
+ type : 'patch'
154
+ }
155
+ ]
156
+ } )
157
+ )
158
+ Object . defineProperty ( github , 'context' , {
159
+ value : {
160
+ ...originalContext ,
161
+ eventName : 'pull_request' ,
162
+ payload : {
163
+ pull_request : {
164
+ head : {
165
+ sha : '1234567890'
166
+ } ,
167
+ base : {
168
+ ref : 'main'
169
+ }
170
+ }
171
+ }
172
+ }
173
+ } )
174
+ jest
175
+ . spyOn ( exec , 'getExecOutput' )
176
+ // workspaces info
177
+ . mockImplementationOnce ( async ( ) => ( {
178
+ stdout : JSON . stringify ( {
179
+ type : 'log' ,
180
+ data : JSON . stringify ( {
181
+ '@owner/pkg1' : {
182
+ location : './packages/pkg1'
183
+ } ,
184
+ '@owner/pkgB' : {
185
+ location : './packages/pkgB'
186
+ }
187
+ } )
188
+ } ) ,
189
+ stderr : '' ,
190
+ exitCode : 0
191
+ } ) )
192
+ // check changeset installed
193
+ . mockImplementationOnce ( async ( ) => ( {
194
+ stdout : '' ,
195
+ stderr : '' ,
196
+ exitCode : 1
197
+ } ) )
198
+
199
+ // Act
200
+ await main . run ( )
201
+
202
+ // Assert
203
+ expect ( runMock ) . toHaveReturned ( )
204
+
205
+ // Verify that all of the core library functions were called correctly
206
+ expect ( execSpy ) . toHaveBeenCalledWith ( 'yarn add @changesets/cli@latest -W' )
207
+ } )
208
+
138
209
it ( 'fails when there are packages that need changeset entries' , async ( ) => {
139
210
// Arrange
140
211
Object . defineProperty ( github , 'context' , {
@@ -153,7 +224,8 @@ describe('action', () => {
153
224
}
154
225
}
155
226
} )
156
- getExecOutputMock
227
+ jest
228
+ . spyOn ( exec , 'getExecOutput' )
157
229
// workspaces info
158
230
. mockImplementationOnce ( async ( ) => ( {
159
231
stdout : JSON . stringify ( {
@@ -170,6 +242,12 @@ describe('action', () => {
170
242
stderr : '' ,
171
243
exitCode : 0
172
244
} ) )
245
+ // changeset installed
246
+ . mockImplementationOnce ( async ( ) => ( {
247
+ stdout : '' ,
248
+ stderr : '' ,
249
+ exitCode : 0
250
+ } ) )
173
251
// changeset info
174
252
. mockImplementationOnce ( async ( ) => ( {
175
253
stdout : '' ,
@@ -244,7 +322,8 @@ describe('action', () => {
244
322
}
245
323
}
246
324
} )
247
- getExecOutputMock
325
+ jest
326
+ . spyOn ( exec , 'getExecOutput' )
248
327
// workspaces info
249
328
. mockImplementationOnce ( async ( ) => ( {
250
329
stdout : JSON . stringify ( {
@@ -291,7 +370,8 @@ describe('action', () => {
291
370
}
292
371
}
293
372
} )
294
- getExecOutputMock
373
+ jest
374
+ . spyOn ( exec , 'getExecOutput' )
295
375
// workspaces info
296
376
. mockImplementationOnce ( async ( ) => ( {
297
377
stdout : '' ,
@@ -326,7 +406,8 @@ describe('action', () => {
326
406
}
327
407
}
328
408
} )
329
- getExecOutputMock
409
+ jest
410
+ . spyOn ( exec , 'getExecOutput' )
330
411
// workspaces info
331
412
. mockImplementationOnce ( async ( ) => ( {
332
413
stdout : JSON . stringify ( {
@@ -343,6 +424,12 @@ describe('action', () => {
343
424
stderr : '' ,
344
425
exitCode : 0
345
426
} ) )
427
+ // changeset installed
428
+ . mockImplementationOnce ( async ( ) => ( {
429
+ stdout : '' ,
430
+ stderr : '' ,
431
+ exitCode : 0
432
+ } ) )
346
433
// changeset info
347
434
. mockImplementationOnce ( async ( ) => ( {
348
435
stdout : JSON . stringify ( `🦋 info Packages to be bumped at patch:
0 commit comments