1+ import { formatLogForConsole , formatLogForError , getSysLogLines } from './logs' ;
2+
13import dockerLambda from 'docker-lambda' ;
24import dotenv from 'dotenv' ;
35import path from 'path' ;
@@ -6,35 +8,51 @@ import path from 'path';
68dotenv . config ( ) ;
79
810function run ( event ) {
9- // Run the Lambda function.
10- let result ;
11-
12- try {
13- // Run the Lambda in Docker.
14- result = dockerLambda ( {
15- // Use the Node.js 6.10.0 image.
16- dockerImage : 'lambci/lambda:nodejs6.10' ,
17- // Bind the build directory as a volume to /var/task.
18- taskDir : path . join ( __dirname , '../dist' ) ,
19- // Pass an event to the Lambda function.
20- event,
21- // Pass AWS credentials from environment.
22- // NOTE: Jest needs the environment variable values explicitly,
23- // see https://github.com/facebook/jest/issues/5362.
24- dockerArgs : [
25- '-e' ,
26- `AWS_ACCESS_KEY_ID=${ process . env . AWS_ACCESS_KEY_ID } ` ,
27- '-e' ,
28- `AWS_SECRET_ACCESS_KEY=${ process . env . AWS_SECRET_ACCESS_KEY } ` ,
29- ] ,
30- } ) ;
31- } catch ( err ) {
11+ // Run the Lambda in Docker.
12+ const result = dockerLambda ( {
13+ // Use the Node.js 6.10.0 image.
14+ dockerImage : 'lambci/lambda:nodejs6.10' ,
15+
16+ // Bind the build directory as a volume to /var/task.
17+ taskDir : path . join ( __dirname , '../dist' ) ,
18+
19+ // Capture both stderr and stdout, instead of catching Errors.
20+ returnSpawnResult : true ,
21+
22+ // Pass an event to the Lambda function.
23+ event,
24+
25+ // Pass AWS credentials from environment.
26+ // NOTE: Jest needs the environment variable values explicitly,
27+ // see https://github.com/facebook/jest/issues/5362.
28+ dockerArgs : [
29+ '-e' ,
30+ `AWS_ACCESS_KEY_ID=${ process . env . AWS_ACCESS_KEY_ID } ` ,
31+ '-e' ,
32+ `AWS_SECRET_ACCESS_KEY=${ process . env . AWS_SECRET_ACCESS_KEY } ` ,
33+ ] ,
34+ } ) ;
35+
36+ // Catch Lambda errors, based on spawn status codes/errors.
37+ if ( result . error || result . status !== 0 ) {
3238 // Throw errors back to test runner.
33- console . error ( 'Error while executing Lambda function: ' , err . message ) ;
34- throw err ;
39+ const { errorMessage } = JSON . parse ( result . stdout ) ;
40+
41+ // Format error message to be displayed in test runner.
42+ const combinedErrorMsg = formatLogForError ( errorMessage , result . stderr ) ;
43+
44+ throw new Error ( combinedErrorMsg ) ;
45+ }
46+
47+ // Capture any console.log occurrences by inspecting the syslog.
48+ const logs = getSysLogLines ( result . stderr ) ;
49+
50+ // Display all messages in a single block in the test runner.
51+ if ( logs . length ) {
52+ console . log ( formatLogForConsole ( logs ) ) ;
3553 }
3654
37- return result ;
55+ return JSON . parse ( result . stdout ) ;
3856}
3957
4058module . exports = run ;
0 commit comments