1
1
import app from './app' ;
2
2
import lambda from '../src/lambda' ;
3
3
4
- let __test = 0 ;
5
- const handler = lambda . deferred ( ( ) => new Promise ( resolve => {
6
- __test = __test + 1 ;
7
- setTimeout ( ( ) => {
8
- resolve ( app ) ;
9
- } , 10 ) ;
10
- } ) ) ;
4
+
5
+ const testEnvironment = ( ) => {
6
+ let __test = 0 ;
7
+ const _handler = lambda . deferred ( ( ) => new Promise ( resolve => {
8
+ __test = __test + 1 ;
9
+ setTimeout ( ( ) => {
10
+ resolve ( app ) ;
11
+ } , 10 ) ;
12
+ } ) ) ;
13
+
14
+ return {
15
+ getValue : ( ) => __test ,
16
+ handler : _handler ,
17
+ }
18
+ }
11
19
12
20
describe ( 'integration for deferred app' , ( ) => {
13
- it ( 'returns static file' , ( ) => {
21
+ it ( 'returns static file' , async ( ) => {
14
22
const myEvent = {
15
23
path : "/static/file.png" ,
16
24
httpMethod : "GET" ,
@@ -19,15 +27,15 @@ describe('integration for deferred app', () => {
19
27
isBase64Encoded : false ,
20
28
body : null
21
29
}
22
- return handler ( myEvent ) . then ( response => {
23
- expect ( response . statusCode ) . toEqual ( 200 ) ;
24
- expect ( response . isBase64Encoded ) . toEqual ( true ) ;
25
- expect ( response . multiValueHeaders ! [ "content-type" ] [ 0 ] ) . toEqual ( 'image/png' ) ;
26
- expect ( response . multiValueHeaders ! [ "content-length " ] [ 0 ] ) . toEqual ( '178 ' ) ;
27
- } ) ;
30
+ const t = testEnvironment ( )
31
+ const response = await t . handler ( myEvent )
32
+ expect ( response . statusCode ) . toEqual ( 200 ) ;
33
+ expect ( response . isBase64Encoded ) . toEqual ( true ) ;
34
+ expect ( response . multiValueHeaders ! [ "content-type " ] [ 0 ] ) . toEqual ( 'image/png ' ) ;
35
+ expect ( response . multiValueHeaders ! [ "content-length" ] [ 0 ] ) . toEqual ( '178' ) ;
28
36
} ) ;
29
37
30
- it ( 'resolves the app promise only once' , ( ) => {
38
+ it ( 'resolves the app promise only once' , async ( ) => {
31
39
const myEvent = {
32
40
path : "/static/file.png" ,
33
41
httpMethod : "GET" ,
@@ -36,16 +44,14 @@ describe('integration for deferred app', () => {
36
44
isBase64Encoded : false ,
37
45
body : null
38
46
}
39
- return handler ( myEvent )
40
- . then ( ( ) => {
41
- expect ( __test ) . toEqual ( 1 ) ;
42
- return handler ( myEvent ) ;
43
- } ) . then ( ( ) => {
44
- expect ( __test ) . toEqual ( 1 ) ;
45
- } ) ;
47
+ const t = testEnvironment ( )
48
+ await t . handler ( myEvent )
49
+ expect ( t . getValue ( ) ) . toEqual ( 1 ) ;
50
+ await t . handler ( myEvent ) ;
51
+ expect ( t . getValue ( ) ) . toEqual ( 1 ) ;
46
52
} ) ;
47
53
48
- it ( 'handler returns rejected promise if app cannot be initialized' , ( ) => {
54
+ it ( 'handler returns rejected promise if app cannot be initialized' , async ( ) => {
49
55
const failingHandler = lambda . deferred ( ( ) => Promise . reject ( new Error ( 'failed to initialize app' ) ) ) ;
50
56
const myEvent = {
51
57
path : "/static/file.png" ,
@@ -55,15 +61,15 @@ describe('integration for deferred app', () => {
55
61
isBase64Encoded : false ,
56
62
body : null
57
63
}
58
- return failingHandler ( myEvent )
59
- . then (
60
- ( ) => Promise . reject ( new Error ( 'should have failed' ) ) ,
61
- e => {
62
- expect ( e . message ) . toEqual ( 'failed to initialize app' )
63
- } )
64
+ try {
65
+ await failingHandler ( myEvent )
66
+ fail ( new Error ( 'should have failed' ) )
67
+ } catch ( e ) {
68
+ expect ( e . message ) . toEqual ( 'failed to initialize app' )
69
+ }
64
70
} )
65
71
66
- it ( 'returns 500 if there is a problem with the request' , ( ) => {
72
+ it ( 'returns 500 if there is a problem with the request' , async ( ) => {
67
73
const failingApp = lambda . deferred ( ( ) => Promise . resolve ( ( ) => { throw new Error ( 'failed' ) } ) ) ;
68
74
const myEvent = {
69
75
path : "/static/file.png" ,
@@ -73,15 +79,13 @@ describe('integration for deferred app', () => {
73
79
isBase64Encoded : false ,
74
80
body : null
75
81
}
76
- return failingApp ( myEvent )
77
- . then ( ( res ) => {
78
- expect ( res ) . toEqual ( {
79
- body : "" ,
80
- isBase64Encoded : false ,
81
- multiValueHeaders : { } ,
82
- statusCode : 500 ,
83
- } )
84
- } ) ;
82
+ const res = await failingApp ( myEvent )
83
+ expect ( res ) . toEqual ( {
84
+ body : "" ,
85
+ isBase64Encoded : false ,
86
+ multiValueHeaders : { } ,
87
+ statusCode : 500 ,
88
+ } )
85
89
} )
86
90
87
91
} )
0 commit comments