@@ -15,6 +15,8 @@ import {
1515 IdempotencyPersistenceLayerError ,
1616} from '../../src/Exceptions' ;
1717import { IdempotencyConfig } from '../../src' ;
18+ import { Context } from 'aws-lambda' ;
19+ import { helloworldContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts' ;
1820
1921const mockSaveInProgress = jest
2022 . spyOn ( BasePersistenceLayer . prototype , 'saveInProgress' )
@@ -26,6 +28,10 @@ const mockGetRecord = jest
2628 . spyOn ( BasePersistenceLayer . prototype , 'getRecord' )
2729 . mockImplementation ( ) ;
2830
31+ const dummyContext = helloworldContext ;
32+
33+ const mockConfig : IdempotencyConfig = new IdempotencyConfig ( { } ) ;
34+
2935class PersistenceLayerTestClass extends BasePersistenceLayer {
3036 protected _deleteRecord = jest . fn ( ) ;
3137 protected _getRecord = jest . fn ( ) ;
@@ -39,21 +45,25 @@ class TestinClassWithLambdaHandler {
3945 @idempotentLambdaHandler ( {
4046 persistenceStore : new PersistenceLayerTestClass ( ) ,
4147 } )
42- public testing ( record : Record < string , unknown > ) : string {
48+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
49+ public testing ( record : Record < string , unknown > , context : Context ) : string {
4350 functionalityToDecorate ( record ) ;
4451
4552 return 'Hi' ;
4653 }
4754}
4855
4956class TestingClassWithFunctionDecorator {
50- public handler ( record : Record < string , unknown > ) : string {
57+ public handler ( record : Record < string , unknown > , context : Context ) : string {
58+ mockConfig . registerLambdaContext ( context ) ;
59+
5160 return this . proccessRecord ( record ) ;
5261 }
5362
5463 @idempotentFunction ( {
5564 persistenceStore : new PersistenceLayerTestClass ( ) ,
5665 dataKeywordArgument : 'testingKey' ,
66+ config : mockConfig ,
5767 } )
5868 public proccessRecord ( record : Record < string , unknown > ) : string {
5969 functionalityToDecorate ( record ) ;
@@ -72,11 +82,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
7282
7383 describe ( 'When wrapping a function with no previous executions' , ( ) => {
7484 beforeEach ( async ( ) => {
75- await classWithFunctionDecorator . handler ( inputRecord ) ;
85+ await classWithFunctionDecorator . handler ( inputRecord , dummyContext ) ;
7686 } ) ;
7787
7888 test ( 'Then it will save the record to INPROGRESS' , ( ) => {
79- expect ( mockSaveInProgress ) . toBeCalledWith ( keyValueToBeSaved ) ;
89+ expect ( mockSaveInProgress ) . toBeCalledWith (
90+ keyValueToBeSaved ,
91+ dummyContext . getRemainingTimeInMillis ( )
92+ ) ;
8093 } ) ;
8194
8295 test ( 'Then it will call the function that was decorated' , ( ) => {
@@ -92,11 +105,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
92105 } ) ;
93106 describe ( 'When wrapping a function with no previous executions' , ( ) => {
94107 beforeEach ( async ( ) => {
95- await classWithLambdaHandler . testing ( inputRecord ) ;
108+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
96109 } ) ;
97110
98111 test ( 'Then it will save the record to INPROGRESS' , ( ) => {
99- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
112+ expect ( mockSaveInProgress ) . toBeCalledWith (
113+ inputRecord ,
114+ dummyContext . getRemainingTimeInMillis ( )
115+ ) ;
100116 } ) ;
101117
102118 test ( 'Then it will call the function that was decorated' , ( ) => {
@@ -122,14 +138,17 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
122138 new IdempotencyRecord ( idempotencyOptions )
123139 ) ;
124140 try {
125- await classWithLambdaHandler . testing ( inputRecord ) ;
141+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
126142 } catch ( e ) {
127143 resultingError = e as Error ;
128144 }
129145 } ) ;
130146
131147 test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
132- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
148+ expect ( mockSaveInProgress ) . toBeCalledWith (
149+ inputRecord ,
150+ dummyContext . getRemainingTimeInMillis ( )
151+ ) ;
133152 } ) ;
134153
135154 test ( 'Then it will get the previous execution record' , ( ) => {
@@ -159,14 +178,17 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
159178 new IdempotencyRecord ( idempotencyOptions )
160179 ) ;
161180 try {
162- await classWithLambdaHandler . testing ( inputRecord ) ;
181+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
163182 } catch ( e ) {
164183 resultingError = e as Error ;
165184 }
166185 } ) ;
167186
168187 test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
169- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
188+ expect ( mockSaveInProgress ) . toBeCalledWith (
189+ inputRecord ,
190+ dummyContext . getRemainingTimeInMillis ( )
191+ ) ;
170192 } ) ;
171193
172194 test ( 'Then it will get the previous execution record' , ( ) => {
@@ -195,11 +217,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
195217 mockGetRecord . mockResolvedValue (
196218 new IdempotencyRecord ( idempotencyOptions )
197219 ) ;
198- await classWithLambdaHandler . testing ( inputRecord ) ;
220+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
199221 } ) ;
200222
201223 test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
202- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
224+ expect ( mockSaveInProgress ) . toBeCalledWith (
225+ inputRecord ,
226+ dummyContext . getRemainingTimeInMillis ( )
227+ ) ;
203228 } ) ;
204229
205230 test ( 'Then it will get the previous execution record' , ( ) => {
@@ -215,7 +240,7 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
215240 class TestinClassWithLambdaHandlerWithConfig {
216241 @idempotentLambdaHandler ( {
217242 persistenceStore : new PersistenceLayerTestClass ( ) ,
218- config : new IdempotencyConfig ( { } ) ,
243+ config : new IdempotencyConfig ( { lambdaContext : dummyContext } ) ,
219244 } )
220245 public testing ( record : Record < string , unknown > ) : string {
221246 functionalityToDecorate ( record ) ;
@@ -237,7 +262,10 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
237262 } ) ;
238263
239264 test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
240- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
265+ expect ( mockSaveInProgress ) . toBeCalledWith (
266+ inputRecord ,
267+ dummyContext . getRemainingTimeInMillis ( )
268+ ) ;
241269 } ) ;
242270
243271 test ( 'Then an IdempotencyPersistenceLayerError is thrown' , ( ) => {
0 commit comments