1
1
import { captureLambdaHandler , logger } from '@aws-github-runner/aws-powertools-util' ;
2
2
import { Context , SQSEvent , SQSRecord } from 'aws-lambda' ;
3
- import { mocked } from 'jest-mock' ;
4
3
5
4
import { addMiddleware , adjustPool , scaleDownHandler , scaleUpHandler , ssmHousekeeper , jobRetryCheck } from './lambda' ;
6
5
import { adjust } from './pool/pool' ;
@@ -80,7 +79,7 @@ describe('Test scale up lambda wrapper.', () => {
80
79
} ) ;
81
80
82
81
it ( 'Scale without error should resolve.' , async ( ) => {
83
- const mock = mocked ( scaleUp ) ;
82
+ const mock = vi . fn ( scaleUp ) ;
84
83
mock . mockImplementation ( ( ) => {
85
84
return new Promise ( ( resolve ) => {
86
85
resolve ( ) ;
@@ -91,21 +90,24 @@ describe('Test scale up lambda wrapper.', () => {
91
90
92
91
it ( 'Non scale should resolve.' , async ( ) => {
93
92
const error = new Error ( 'Non scale should resolve.' ) ;
94
- const mock = mocked ( scaleUp ) ;
93
+ const mock = vi . fn ( scaleUp ) ;
95
94
mock . mockRejectedValue ( error ) ;
96
- await expect ( scaleUpHandler ( sqsEvent , context ) ) . resolves . not . toThrow ;
95
+ await expect ( scaleUpHandler ( sqsEvent , context ) ) . resolves . not . toThrow ( ) ;
97
96
} ) ;
98
97
99
98
it ( 'Scale should be rejected' , async ( ) => {
100
99
const error = new ScaleError ( 'Scale should be rejected' ) ;
101
- const mock = mocked ( scaleUp ) ;
102
- mock . mockRejectedValue ( error ) ;
100
+ const mock = vi . fn ( ) as MockedFunction < typeof scaleUp > ;
101
+ mock . mockImplementation ( ( ) => {
102
+ return Promise . reject ( error ) ;
103
+ } ) ;
104
+ vi . mocked ( scaleUp ) . mockImplementation ( mock ) ;
103
105
await expect ( scaleUpHandler ( sqsEvent , context ) ) . rejects . toThrow ( error ) ;
104
106
} ) ;
105
107
} ) ;
106
108
107
109
async function testInvalidRecords ( sqsRecords : SQSRecord [ ] ) {
108
- const mock = mocked ( scaleUp ) ;
110
+ const mock = vi . fn ( scaleUp ) ;
109
111
const logWarnSpy = vi . spyOn ( logger , 'warn' ) ;
110
112
mock . mockImplementation ( ( ) => {
111
113
return new Promise ( ( resolve ) => {
@@ -127,7 +129,7 @@ async function testInvalidRecords(sqsRecords: SQSRecord[]) {
127
129
128
130
describe ( 'Test scale down lambda wrapper.' , ( ) => {
129
131
it ( 'Scaling down no error.' , async ( ) => {
130
- const mock = mocked ( scaleDown ) ;
132
+ const mock = vi . fn ( scaleDown ) ;
131
133
mock . mockImplementation ( ( ) => {
132
134
return new Promise ( ( resolve ) => {
133
135
resolve ( ) ;
@@ -138,15 +140,15 @@ describe('Test scale down lambda wrapper.', () => {
138
140
139
141
it ( 'Scaling down with error.' , async ( ) => {
140
142
const error = new Error ( 'Scaling down with error.' ) ;
141
- const mock = mocked ( scaleDown ) ;
143
+ const mock = vi . fn ( scaleDown ) ;
142
144
mock . mockRejectedValue ( error ) ;
143
145
await expect ( scaleDownHandler ( { } , context ) ) . resolves . not . toThrow ( ) ;
144
146
} ) ;
145
147
} ) ;
146
148
147
149
describe ( 'Adjust pool.' , ( ) => {
148
150
it ( 'Receive message to adjust pool.' , async ( ) => {
149
- const mock = mocked ( adjust ) ;
151
+ const mock = vi . fn ( adjust ) ;
150
152
mock . mockImplementation ( ( ) => {
151
153
return new Promise ( ( resolve ) => {
152
154
resolve ( ) ;
@@ -156,12 +158,15 @@ describe('Adjust pool.', () => {
156
158
} ) ;
157
159
158
160
it ( 'Handle error for adjusting pool.' , async ( ) => {
159
- const mock = mocked ( adjust ) ;
160
161
const error = new Error ( 'Handle error for adjusting pool.' ) ;
161
- mock . mockRejectedValue ( error ) ;
162
+ const mock = vi . fn ( ) as MockedFunction < typeof adjust > ;
163
+ mock . mockImplementation ( ( ) => {
164
+ return Promise . reject ( error ) ;
165
+ } ) ;
166
+ vi . mocked ( adjust ) . mockImplementation ( mock ) ;
162
167
const logSpy = vi . spyOn ( logger , 'error' ) ;
163
168
await adjustPool ( { poolSize : 0 } , context ) ;
164
- expect ( logSpy ) . lastCalledWith ( expect . stringContaining ( error . message ) , expect . anything ( ) ) ;
169
+ expect ( logSpy ) . toHaveBeenCalledWith ( `Handle error for adjusting pool. ${ error . message } ` , { error } ) ;
165
170
} ) ;
166
171
} ) ;
167
172
@@ -175,7 +180,7 @@ describe('Test middleware', () => {
175
180
176
181
describe ( 'Test ssm housekeeper lambda wrapper.' , ( ) => {
177
182
it ( 'Invoke without errors.' , async ( ) => {
178
- const mock = mocked ( cleanSSMTokens ) ;
183
+ const mock = vi . fn ( cleanSSMTokens ) ;
179
184
mock . mockImplementation ( ( ) => {
180
185
return new Promise ( ( resolve ) => {
181
186
resolve ( ) ;
@@ -192,31 +197,32 @@ describe('Test ssm housekeeper lambda wrapper.', () => {
192
197
} ) ;
193
198
194
199
it ( 'Errors not throws.' , async ( ) => {
195
- const mock = mocked ( cleanSSMTokens ) ;
200
+ const mock = vi . fn ( cleanSSMTokens ) ;
196
201
mock . mockRejectedValue ( new Error ( ) ) ;
197
202
await expect ( ssmHousekeeper ( { } , context ) ) . resolves . not . toThrow ( ) ;
198
203
} ) ;
199
204
} ) ;
200
205
201
206
describe ( 'Test job retry check wrapper' , ( ) => {
202
207
it ( 'Handle without error should resolve.' , async ( ) => {
203
- const mock = mocked ( checkAndRetryJob ) ;
208
+ const mock = vi . fn ( ) as MockedFunction < typeof checkAndRetryJob > ;
204
209
mock . mockImplementation ( ( ) => {
205
- return new Promise ( ( resolve ) => {
206
- resolve ( ) ;
207
- } ) ;
210
+ return Promise . resolve ( ) ;
208
211
} ) ;
212
+ vi . mocked ( checkAndRetryJob ) . mockImplementation ( mock ) ;
209
213
await expect ( jobRetryCheck ( sqsEvent , context ) ) . resolves . not . toThrow ( ) ;
210
214
} ) ;
211
215
212
216
it ( 'Handle with error should resolve and log only a warning.' , async ( ) => {
213
- const logSpyWarn = vi . spyOn ( logger , 'warn' ) ;
214
-
215
- const mock = mocked ( checkAndRetryJob ) ;
216
217
const error = new Error ( 'Error handling retry check.' ) ;
217
- mock . mockRejectedValue ( error ) ;
218
+ const mock = vi . fn ( ) as MockedFunction < typeof checkAndRetryJob > ;
219
+ mock . mockImplementation ( ( ) => {
220
+ return Promise . reject ( error ) ;
221
+ } ) ;
222
+ vi . mocked ( checkAndRetryJob ) . mockImplementation ( mock ) ;
218
223
224
+ const logSpyWarn = vi . spyOn ( logger , 'warn' ) ;
219
225
await expect ( jobRetryCheck ( sqsEvent , context ) ) . resolves . not . toThrow ( ) ;
220
- expect ( logSpyWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( error . message ) , expect . anything ( ) ) ;
226
+ expect ( logSpyWarn ) . toHaveBeenCalledWith ( `Error processing job retry: ${ error . message } ` , { error } ) ;
221
227
} ) ;
222
228
} ) ;
0 commit comments