1- // Make sure Jest mocks are defined before any imports
21jest . mock ( '@actions/core' ) ;
32jest . mock ( '@aws-sdk/client-lambda' ) ;
4-
5- // Mock the waitForFunctionUpdated function in index.js
63jest . mock ( '../index' , ( ) => {
74 const actualModule = jest . requireActual ( '../index' ) ;
85 const originalRun = actualModule . run ;
96
107 return {
118 ...actualModule ,
12- // Create a mock for run that calls specific behaviors for each test
139 run : jest . fn ( ) . mockImplementation ( async ( ) => {
14- // This will be overridden in each test
1510 const fs = require ( 'fs/promises' ) ;
1611 const AdmZip = require ( 'adm-zip' ) ;
1712 const { glob } = require ( 'glob' ) ;
1813 const core = require ( '@actions/core' ) ;
1914
20- // Create a mock implementation that simulates the basic flow without timeout
2115 await fs . mkdir ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
2216 await glob ( '**/*' , { cwd : '/mock/artifacts' , dot : true } ) ;
2317 const zip = new AdmZip ( ) ;
2418 zip . addLocalFolder ( '/mock/cwd/lambda-package' ) ;
2519
26- // Log success
2720 core . info ( 'Packaging code artifacts from /mock/artifacts' ) ;
2821 core . info ( 'Lambda function deployment completed successfully' ) ;
2922 } ) ,
@@ -36,8 +29,6 @@ jest.mock('../index', () => {
3629 waitForFunctionUpdated : jest . fn ( ) . mockResolvedValue ( undefined )
3730 } ;
3831} ) ;
39-
40- // Mock fs/promises
4132jest . mock ( 'fs/promises' , ( ) => ( {
4233 mkdir : jest . fn ( ) . mockResolvedValue ( undefined ) ,
4334 stat : jest . fn ( ) . mockImplementation ( async ( path ) => ( {
@@ -46,23 +37,17 @@ jest.mock('fs/promises', () => ({
4637 copyFile : jest . fn ( ) . mockResolvedValue ( undefined ) ,
4738 readFile : jest . fn ( ) . mockResolvedValue ( Buffer . from ( 'mock file content' ) )
4839} ) ) ;
49-
50- // Create manual mocks for modules that may not be installed
5140jest . mock ( 'glob' , ( ) => ( {
5241 glob : jest . fn ( ) . mockResolvedValue ( [ 'file1.js' , 'directory/file2.js' , 'directory' ] )
5342} ) ) ;
54-
55- // Manual mock for AdmZip
5643jest . mock ( 'adm-zip' , ( ) =>
5744 jest . fn ( ) . mockImplementation ( ( ) => ( {
5845 addLocalFolder : jest . fn ( ) ,
5946 writeZip : jest . fn ( )
6047 } ) )
6148) ;
62-
6349jest . mock ( 'path' ) ;
6450
65- // Now we can import modules
6651const core = require ( '@actions/core' ) ;
6752const { LambdaClient } = require ( '@aws-sdk/client-lambda' ) ;
6853const fs = require ( 'fs/promises' ) ;
@@ -71,21 +56,15 @@ const { glob } = require('glob');
7156const AdmZip = require ( 'adm-zip' ) ;
7257const mainModule = require ( '../index' ) ;
7358
74- // Increase the default timeout for all tests in this file
75- jest . setTimeout ( 15000 ) ;
76-
77- describe ( 'Lambda Deployment Integration Tests' , ( ) => {
59+ describe ( 'Code Artifacts Tests' , ( ) => {
7860 beforeEach ( ( ) => {
7961 jest . clearAllMocks ( ) ;
8062
81- // Mock process.cwd()
8263 process . cwd = jest . fn ( ) . mockReturnValue ( '/mock/cwd' ) ;
8364
84- // Mock path.join to return predictable paths
8565 path . join . mockImplementation ( ( ...parts ) => parts . join ( '/' ) ) ;
8666 path . dirname . mockImplementation ( ( p ) => p . substring ( 0 , p . lastIndexOf ( '/' ) ) ) ;
8767
88- // Mock core functions
8968 core . getInput . mockImplementation ( ( name ) => {
9069 const inputs = {
9170 'function-name' : 'test-function' ,
@@ -101,7 +80,6 @@ describe('Lambda Deployment Integration Tests', () => {
10180 core . error . mockImplementation ( ( ) => { } ) ;
10281 core . setFailed . mockImplementation ( ( ) => { } ) ;
10382
104- // Mock Lambda client
10583 const mockLambdaResponse = {
10684 $metadata : { httpStatusCode : 200 } ,
10785 Configuration : {
@@ -116,43 +94,34 @@ describe('Lambda Deployment Integration Tests', () => {
11694 } ) ;
11795
11896 it ( 'should package artifacts and deploy to Lambda' , async ( ) => {
119- // Set up the run mock for this specific test
12097 mainModule . run . mockImplementationOnce ( async ( ) => {
121- // This simulates what the test expects to happen
12298 await fs . mkdir ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
12399 const files = await glob ( '**/*' , { cwd : '/mock/artifacts' , dot : true } ) ;
124100 const zip = new AdmZip ( ) ;
125101 zip . addLocalFolder ( '/mock/cwd/lambda-package' ) ;
126102 core . info ( 'Packaging code artifacts from /mock/artifacts' ) ;
127103 } ) ;
128104
129- // Call the main function
130105 await mainModule . run ( ) ;
131106
132- // Verify temporary directory was created
133- expect ( fs . mkdir ) . toHaveBeenCalledWith ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
134107
135- // Verify glob was called to find files
136- expect ( glob ) . toHaveBeenCalledWith ( '**/*' , { cwd : '/mock/artifacts' , dot : true } ) ;
108+ expect ( fs . mkdir ) . toHaveBeenCalledWith ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
137109
138- // Verify ZIP creation
110+ expect ( glob ) . toHaveBeenCalledWith ( '**/*' , { cwd : '/mock/artifacts' , dot : true } ) ;
111+
139112 expect ( AdmZip ) . toHaveBeenCalled ( ) ;
140113 const zipInstance = AdmZip . mock . results [ 0 ] . value ;
141114 expect ( zipInstance . addLocalFolder ) . toHaveBeenCalledWith ( '/mock/cwd/lambda-package' ) ;
142115
143- // Verify appropriate logs were shown
144- expect ( core . info ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Packaging code artifacts' ) ) ;
145-
146- // Verify no errors were reported
116+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Packaging code artifacts' ) ) ;
147117 expect ( core . setFailed ) . not . toHaveBeenCalled ( ) ;
148118 } ) ;
149119
150120 it ( 'should handle artifacts packaging failure gracefully' , async ( ) => {
151- // Make file operations throw an error
121+
152122 const packageError = new Error ( 'Failed to create package' ) ;
153123 fs . mkdir . mockRejectedValueOnce ( packageError ) ;
154124
155- // Set up the run mock for this specific test
156125 mainModule . run . mockImplementationOnce ( async ( ) => {
157126 try {
158127 await fs . mkdir ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
@@ -161,15 +130,13 @@ describe('Lambda Deployment Integration Tests', () => {
161130 }
162131 } ) ;
163132
164- // Call the main function
165- await mainModule . run ( ) ;
166133
167- // Verify error was logged
134+ await mainModule . run ( ) ;
168135 expect ( core . setFailed ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Action failed with error' ) ) ;
169136 } ) ;
170137
171138 it ( 'should correctly use code-artifacts-dir when provided' , async ( ) => {
172- // Update the mock with a different artifacts directory
139+
173140 core . getInput . mockImplementation ( ( name ) => {
174141 const inputs = {
175142 'function-name' : 'test-function' ,
@@ -178,28 +145,21 @@ describe('Lambda Deployment Integration Tests', () => {
178145 'role' : 'arn:aws:iam::123456789012:role/lambda-role' ,
179146 } ;
180147 return inputs [ name ] || '' ;
181- } ) ;
148+ } ) ;
182149
183- // Set up the run mock for this specific test
184- mainModule . run . mockImplementationOnce ( async ( ) => {
185- // In this test, we expect fs.mkdir and glob not to be called
186- // Just do some final output to simulate success
150+ mainModule . run . mockImplementationOnce ( async ( ) => {
187151 core . info ( 'Lambda function deployment completed successfully' ) ;
188152 } ) ;
189153
190- // Call the main function
191- await mainModule . run ( ) ;
154+ await mainModule . run ( ) ;
192155
193- // Verify packaging functions were not called
194156 expect ( fs . mkdir ) . not . toHaveBeenCalled ( ) ;
195157 expect ( glob ) . not . toHaveBeenCalled ( ) ;
196-
197- // Verify no errors were reported
198158 expect ( core . setFailed ) . not . toHaveBeenCalled ( ) ;
199159 } ) ;
200160
201161 it ( 'should fail when code-artifacts-dir is missing' , async ( ) => {
202- // Change the mock to return without code-artifacts-dir
162+
203163 core . getInput . mockImplementation ( ( name ) => {
204164 const inputs = {
205165 'function-name' : 'test-function' ,
@@ -208,8 +168,7 @@ describe('Lambda Deployment Integration Tests', () => {
208168 } ;
209169 return inputs [ name ] || '' ;
210170 } ) ;
211-
212- // Set up the run mock for this specific test
171+
213172 mainModule . run . mockImplementationOnce ( async ( ) => {
214173 const codeArtifactsDir = core . getInput ( 'code-artifacts-dir' ) ;
215174
@@ -218,20 +177,16 @@ describe('Lambda Deployment Integration Tests', () => {
218177 return ;
219178 }
220179
221- // This shouldn't execute in this test
222180 await fs . mkdir ( '/mock/cwd/lambda-package' , { recursive : true } ) ;
223181 } ) ;
224182
225- // Call the main function
226183 await mainModule . run ( ) ;
227184
228- // Verify error was reported
229185 expect ( core . setFailed ) . toHaveBeenCalledWith (
230186 'Code-artifacts-dir must be provided'
231187 ) ;
232188
233- // Verify packaging functions were not called
234189 expect ( fs . mkdir ) . not . toHaveBeenCalled ( ) ;
235190 expect ( glob ) . not . toHaveBeenCalled ( ) ;
236191 } ) ;
237- } ) ;
192+ } ) ;
0 commit comments