1- import { jest , describe , it , expect , beforeEach } from '@jest/globals'
1+ import {
2+ jest ,
3+ describe ,
4+ it ,
5+ expect ,
6+ beforeEach ,
7+ afterEach ,
8+ } from '@jest/globals'
9+
10+ const mockLogger = {
11+ logoDevMode : jest . fn ( ) ,
12+ blankLine : jest . fn ( ) ,
13+ aside : jest . fn ( ) ,
14+ notice : jest . fn ( ) ,
15+ debug : jest . fn ( ) ,
16+ warning : jest . fn ( ) ,
17+ confirm : jest . fn ( ) ,
18+ success : jest . fn ( ) ,
19+ error : jest . fn ( ) ,
20+ }
221
322jest . unstable_mockModule ( '@serverless/util' , ( ) => ( {
423 log : {
5- get : jest . fn ( ( ) => ( {
6- logoDevMode : jest . fn ( ) ,
7- blankLine : jest . fn ( ) ,
8- aside : jest . fn ( ) ,
9- notice : jest . fn ( ) ,
10- debug : jest . fn ( ) ,
11- warning : jest . fn ( ) ,
12- confirm : jest . fn ( ) ,
13- success : jest . fn ( ) ,
14- error : jest . fn ( ) ,
15- } ) ) ,
24+ get : jest . fn ( ( ) => mockLogger ) ,
1625 error : jest . fn ( ) ,
1726 blankLine : jest . fn ( ) ,
1827 warning : jest . fn ( ) ,
@@ -43,6 +52,14 @@ jest.unstable_mockModule('@serverless/util', () => ({
4352const { default : AwsDev } =
4453 await import ( '../../../../../lib/plugins/aws/dev/index.js' )
4554
55+ const originalProcessVersion = process . version
56+
57+ const setProcessVersion = ( version ) => {
58+ Object . defineProperty ( process , 'version' , {
59+ configurable : true ,
60+ value : version ,
61+ } )
62+ }
4663const createServerless = ( ) => {
4764 const provider = {
4865 getStage : jest . fn ( ) ,
@@ -53,8 +70,15 @@ const createServerless = () => {
5370 return {
5471 getProvider : jest . fn ( ( ) => provider ) ,
5572 processedInput : { commands : [ ] } ,
73+ configurationInput : { } ,
5674 service : {
5775 provider : { } ,
76+ getServiceName : jest . fn ( ( ) => 'test-service' ) ,
77+ getAllFunctions : jest . fn ( ( ) => [ 'hello' ] ) ,
78+ getFunction : jest . fn ( ( ) => ( {
79+ handler : 'handler.main' ,
80+ runtime : 'nodejs20.x' ,
81+ } ) ) ,
5882 } ,
5983 }
6084}
@@ -63,9 +87,15 @@ describe('AwsDev', () => {
6387 let awsDev
6488
6589 beforeEach ( ( ) => {
90+ jest . clearAllMocks ( )
91+ setProcessVersion ( originalProcessVersion )
6692 awsDev = new AwsDev ( createServerless ( ) , { } )
6793 } )
6894
95+ afterEach ( ( ) => {
96+ setProcessVersion ( originalProcessVersion )
97+ } )
98+
6999 describe ( '#validateOnExitOption()' , ( ) => {
70100 it ( 'should not throw when --on-exit is not provided' , ( ) => {
71101 expect ( ( ) => awsDev . validateOnExitOption ( ) ) . not . toThrow ( )
@@ -91,4 +121,49 @@ describe('AwsDev', () => {
91121 }
92122 } )
93123 } )
124+
125+ describe ( '#update()' , ( ) => {
126+ it ( 'should set runtime to local node runtime when it is supported by AWS Lambda' , async ( ) => {
127+ setProcessVersion ( 'v22.1.0' )
128+ awsDev . getIotEndpoint = jest . fn ( ) . mockResolvedValue ( 'iot-endpoint' )
129+
130+ const functionConfig = {
131+ handler : 'handler.main' ,
132+ runtime : 'nodejs20.x' ,
133+ }
134+
135+ awsDev . serverless . service . getFunction = jest . fn ( ( ) => functionConfig )
136+ awsDev . serverless . service . provider . iam = { }
137+ awsDev . serverless . getProvider ( ) . getStage . mockReturnValue ( 'dev' )
138+
139+ await awsDev . update ( )
140+
141+ expect ( functionConfig . runtime ) . toBe ( 'nodejs22.x' )
142+ expect ( mockLogger . warning ) . toHaveBeenCalledWith (
143+ 'Your local machine is using Node.js v22, while at least one of your functions is not. Ensure matching runtime versions for accurate testing.' ,
144+ )
145+ } )
146+
147+ it ( 'should fall back to nodejs20.x when local node runtime is not supported by AWS Lambda' , async ( ) => {
148+ setProcessVersion ( 'v26.0.0' )
149+ awsDev . getIotEndpoint = jest . fn ( ) . mockResolvedValue ( 'iot-endpoint' )
150+
151+ const functionConfig = {
152+ handler : 'handler.main' ,
153+ runtime : 'nodejs20.x' ,
154+ }
155+
156+ awsDev . serverless . service . getFunction = jest . fn ( ( ) => functionConfig )
157+ awsDev . serverless . service . provider . iam = { }
158+ awsDev . serverless . getProvider ( ) . getStage . mockReturnValue ( 'dev' )
159+
160+ await awsDev . update ( )
161+
162+ expect ( functionConfig . runtime ) . toBe ( 'nodejs20.x' )
163+ expect ( mockLogger . warning ) . toHaveBeenCalledTimes ( 1 )
164+ expect ( mockLogger . warning ) . toHaveBeenCalledWith (
165+ 'Your local machine is using Node.js v26, which is not yet supported by AWS Lambda. Falling back to nodejs20.x for dev mode deployment.' ,
166+ )
167+ } )
168+ } )
94169} )
0 commit comments