@@ -7,14 +7,6 @@ const Serverless = require('../../test/serverless');
7
7
8
8
jest . spyOn ( console , 'log' ) ;
9
9
describe ( 'invokeLocalNodeJs' , ( ) => {
10
- const eventName = 'eventName' ;
11
- const contextName = 'contextName' ;
12
- const event = {
13
- name : eventName ,
14
- } ;
15
- const context = {
16
- name : contextName ,
17
- } ;
18
10
const myVarValue = 'MY_VAR_VALUE' ;
19
11
let serverless ;
20
12
let googleInvokeLocal ;
@@ -29,57 +21,147 @@ describe('invokeLocalNodeJs', () => {
29
21
serverless . cli . consoleLog = jest . fn ( ) ;
30
22
googleInvokeLocal = new GoogleInvokeLocal ( serverless , { } ) ;
31
23
} ) ;
32
-
33
- it ( 'should invoke a sync handler' , async ( ) => {
34
- const functionConfig = {
35
- handler : 'syncHandler' ,
24
+ describe ( 'event' , ( ) => {
25
+ const eventName = 'eventName' ;
26
+ const contextName = 'contextName' ;
27
+ const event = {
28
+ name : eventName ,
36
29
} ;
37
- await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
38
- // eslint-disable-next-line no-console
39
- expect ( console . log ) . toHaveBeenCalledWith ( 'SYNC_HANDLER' ) ;
40
- expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith ( `{\n "result": "${ eventName } "\n}` ) ;
41
- } ) ;
42
-
43
- it ( 'should handle errors in a sync handler' , async ( ) => {
44
- const functionConfig = {
45
- handler : 'syncHandlerWithError' ,
30
+ const context = {
31
+ name : contextName ,
46
32
} ;
47
- await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
48
- // eslint-disable-next-line no-console
49
- expect ( console . log ) . toHaveBeenCalledWith ( 'SYNC_HANDLER' ) ;
50
- expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
51
- expect . stringContaining ( '"errorMessage": "SYNC_ERROR"' )
52
- ) ;
53
- } ) ;
54
-
55
- it ( 'should invoke an async handler' , async ( ) => {
56
- const functionConfig = {
57
- handler : 'asyncHandler' ,
33
+ const baseConfig = {
34
+ events : [ { event : { } } ] ,
58
35
} ;
59
- await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
60
- // eslint-disable-next-line no-console
61
- expect ( console . log ) . toHaveBeenCalledWith ( 'ASYNC_HANDLER' ) ;
62
- expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith ( `{\n "result": "${ contextName } "\n}` ) ;
63
- } ) ;
36
+ it ( 'should invoke a sync handler' , async ( ) => {
37
+ const functionConfig = {
38
+ ...baseConfig ,
39
+ handler : 'eventSyncHandler' ,
40
+ } ;
41
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
42
+ // eslint-disable-next-line no-console
43
+ expect ( console . log ) . toHaveBeenCalledWith ( 'EVENT_SYNC_HANDLER' ) ;
44
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith ( `{\n "result": "${ eventName } "\n}` ) ;
45
+ } ) ;
64
46
65
- it ( 'should handle errors in an async handler' , async ( ) => {
66
- const functionConfig = {
67
- handler : 'asyncHandlerWithError' ,
68
- } ;
69
- await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
70
- // eslint-disable-next-line no-console
71
- expect ( console . log ) . toHaveBeenCalledWith ( 'ASYNC_HANDLER' ) ;
72
- expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
73
- expect . stringContaining ( '"errorMessage": "ASYNC_ERROR"' )
74
- ) ;
75
- } ) ;
47
+ it ( 'should handle errors in a sync handler' , async ( ) => {
48
+ const functionConfig = {
49
+ ...baseConfig ,
50
+ handler : 'eventSyncHandlerWithError' ,
51
+ } ;
52
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
53
+ // eslint-disable-next-line no-console
54
+ expect ( console . log ) . toHaveBeenCalledWith ( 'EVENT_SYNC_HANDLER' ) ;
55
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
56
+ expect . stringContaining ( '"errorMessage": "SYNC_ERROR"' )
57
+ ) ;
58
+ } ) ;
76
59
77
- it ( 'should give the environment variables to the handler' , async ( ) => {
78
- const functionConfig = {
79
- handler : 'envHandler' ,
60
+ it ( 'should invoke an async handler' , async ( ) => {
61
+ const functionConfig = {
62
+ ...baseConfig ,
63
+ handler : 'eventAsyncHandler' ,
64
+ } ;
65
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
66
+ // eslint-disable-next-line no-console
67
+ expect ( console . log ) . toHaveBeenCalledWith ( 'EVENT_ASYNC_HANDLER' ) ;
68
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
69
+ `{\n "result": "${ contextName } "\n}`
70
+ ) ;
71
+ } ) ;
72
+
73
+ it ( 'should handle errors in an async handler' , async ( ) => {
74
+ const functionConfig = {
75
+ ...baseConfig ,
76
+ handler : 'eventAsyncHandlerWithError' ,
77
+ } ;
78
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
79
+ // eslint-disable-next-line no-console
80
+ expect ( console . log ) . toHaveBeenCalledWith ( 'EVENT_ASYNC_HANDLER' ) ;
81
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
82
+ expect . stringContaining ( '"errorMessage": "ASYNC_ERROR"' )
83
+ ) ;
84
+ } ) ;
85
+
86
+ it ( 'should give the environment variables to the handler' , async ( ) => {
87
+ const functionConfig = {
88
+ ...baseConfig ,
89
+ handler : 'eventEnvHandler' ,
90
+ } ;
91
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
92
+ // eslint-disable-next-line no-console
93
+ expect ( console . log ) . toHaveBeenCalledWith ( myVarValue ) ;
94
+ } ) ;
95
+ } ) ;
96
+ describe ( 'http' , ( ) => {
97
+ const message = 'httpBodyMessage' ;
98
+ const req = {
99
+ body : { message } ,
100
+ } ;
101
+ const context = { } ;
102
+ const baseConfig = {
103
+ events : [ { http : '' } ] ,
80
104
} ;
81
- await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , event , context ) ;
82
- // eslint-disable-next-line no-console
83
- expect ( console . log ) . toHaveBeenCalledWith ( myVarValue ) ;
105
+ it ( 'should invoke a sync handler' , async ( ) => {
106
+ const functionConfig = {
107
+ ...baseConfig ,
108
+ handler : 'httpSyncHandler' ,
109
+ } ;
110
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , req , context ) ;
111
+ // eslint-disable-next-line no-console
112
+ expect ( console . log ) . toHaveBeenCalledWith ( 'HTTP_SYNC_HANDLER' ) ;
113
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
114
+ JSON . stringify ( { status : 200 , body : JSON . stringify ( { responseMessage : message } ) } , null , 4 )
115
+ ) ;
116
+ } ) ;
117
+
118
+ it ( 'should handle errors in a sync handler' , async ( ) => {
119
+ const functionConfig = {
120
+ ...baseConfig ,
121
+ handler : 'httpSyncHandlerWithError' ,
122
+ } ;
123
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , req , context ) ;
124
+ // eslint-disable-next-line no-console
125
+ expect ( console . log ) . toHaveBeenCalledWith ( 'HTTP_SYNC_HANDLER' ) ;
126
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
127
+ expect . stringContaining ( '"errorMessage": "SYNC_ERROR"' )
128
+ ) ;
129
+ } ) ;
130
+
131
+ it ( 'should invoke an async handler' , async ( ) => {
132
+ const functionConfig = {
133
+ ...baseConfig ,
134
+ handler : 'httpAsyncHandler' ,
135
+ } ;
136
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , req , context ) ;
137
+ // eslint-disable-next-line no-console
138
+ expect ( console . log ) . toHaveBeenCalledWith ( 'HTTP_ASYNC_HANDLER' ) ;
139
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
140
+ JSON . stringify ( { status : 200 , body : JSON . stringify ( { responseMessage : message } ) } , null , 4 )
141
+ ) ;
142
+ } ) ;
143
+
144
+ it ( 'should handle errors in an async handler' , async ( ) => {
145
+ const functionConfig = {
146
+ ...baseConfig ,
147
+ handler : 'httpAsyncHandlerWithError' ,
148
+ } ;
149
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , req , context ) ;
150
+ // eslint-disable-next-line no-console
151
+ expect ( console . log ) . toHaveBeenCalledWith ( 'HTTP_ASYNC_HANDLER' ) ;
152
+ expect ( serverless . cli . consoleLog ) . toHaveBeenCalledWith (
153
+ expect . stringContaining ( '"errorMessage": "ASYNC_ERROR"' )
154
+ ) ;
155
+ } ) ;
156
+
157
+ it ( 'should give the environment variables to the handler' , async ( ) => {
158
+ const functionConfig = {
159
+ ...baseConfig ,
160
+ handler : 'httpEnvHandler' ,
161
+ } ;
162
+ await googleInvokeLocal . invokeLocalNodeJs ( functionConfig , req , context ) ;
163
+ // eslint-disable-next-line no-console
164
+ expect ( console . log ) . toHaveBeenCalledWith ( myVarValue ) ;
165
+ } ) ;
84
166
} ) ;
85
167
} ) ;
0 commit comments