1
- const test = require ( 'ava' )
1
+ import { expect , test } from 'vitest'
2
2
3
- const { builder } = require ( '../../dist/lib/builder' )
4
- const { invokeLambda } = require ( '../helpers/main' )
3
+ import { invokeLambda } from '../../test/helpers/main.mjs'
4
+ import { BaseHandler } from '../function/handler.js'
5
+ import { HandlerEvent } from '../main.js'
6
+
7
+ import { builder } from './builder.js'
5
8
6
9
const METADATA_OBJECT = { metadata : { version : 1 , builder_function : true , ttl : 0 } }
7
10
8
- test ( 'Injects the metadata object into an asynchronous handler' , async ( t ) => {
11
+ test ( 'Injects the metadata object into an asynchronous handler' , async ( ) => {
12
+ const ttl = 3600
9
13
const originalResponse = {
10
14
body : ':thumbsup:' ,
11
15
statusCode : 200 ,
12
- ttl : 3600 ,
16
+ ttl,
13
17
}
14
18
const myHandler = async ( ) => {
15
19
const asyncTask = new Promise ( ( resolve ) => {
@@ -22,23 +26,25 @@ test('Injects the metadata object into an asynchronous handler', async (t) => {
22
26
}
23
27
const response = await invokeLambda ( builder ( myHandler ) )
24
28
25
- t . deepEqual ( response , { ...originalResponse , metadata : { version : 1 , builder_function : true , ttl : 3600 } } )
29
+ expect ( response ) . toStrictEqual ( { ...originalResponse , metadata : { version : 1 , builder_function : true , ttl } } )
26
30
} )
27
31
28
- test ( 'Injects the metadata object into a synchronous handler' , async ( t ) => {
32
+ test ( 'Injects the metadata object into a synchronous handler' , async ( ) => {
29
33
const originalResponse = {
30
34
body : ':thumbsup:' ,
31
35
statusCode : 200 ,
32
36
}
33
- const myHandler = ( event , context , callback ) => {
34
- callback ( null , originalResponse )
37
+ // eslint-disable-next-line promise/prefer-await-to-callbacks
38
+ const myHandler : BaseHandler = ( event , context , callback ) => {
39
+ // eslint-disable-next-line n/callback-return, promise/prefer-await-to-callbacks
40
+ callback ?.( null , originalResponse )
35
41
}
36
42
const response = await invokeLambda ( builder ( myHandler ) )
37
43
38
- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
44
+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
39
45
} )
40
46
41
- test ( 'Injects the metadata object for non-200 responses' , async ( t ) => {
47
+ test ( 'Injects the metadata object for non-200 responses' , async ( ) => {
42
48
const originalResponse = {
43
49
body : ':thumbsdown:' ,
44
50
statusCode : 404 ,
@@ -54,10 +60,10 @@ test('Injects the metadata object for non-200 responses', async (t) => {
54
60
}
55
61
const response = await invokeLambda ( builder ( myHandler ) )
56
62
57
- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
63
+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
58
64
} )
59
65
60
- test ( 'Returns a 405 error for requests using the POST method' , async ( t ) => {
66
+ test ( 'Returns a 405 error for requests using the POST method' , async ( ) => {
61
67
const originalResponse = {
62
68
body : ':thumbsup:' ,
63
69
statusCode : 200 ,
@@ -73,10 +79,10 @@ test('Returns a 405 error for requests using the POST method', async (t) => {
73
79
}
74
80
const response = await invokeLambda ( builder ( myHandler ) , { method : 'POST' } )
75
81
76
- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
82
+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
77
83
} )
78
84
79
- test ( 'Returns a 405 error for requests using the PUT method' , async ( t ) => {
85
+ test ( 'Returns a 405 error for requests using the PUT method' , async ( ) => {
80
86
const originalResponse = {
81
87
body : ':thumbsup:' ,
82
88
statusCode : 200 ,
@@ -92,10 +98,10 @@ test('Returns a 405 error for requests using the PUT method', async (t) => {
92
98
}
93
99
const response = await invokeLambda ( builder ( myHandler ) , { method : 'PUT' } )
94
100
95
- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
101
+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
96
102
} )
97
103
98
- test ( 'Returns a 405 error for requests using the DELETE method' , async ( t ) => {
104
+ test ( 'Returns a 405 error for requests using the DELETE method' , async ( ) => {
99
105
const originalResponse = {
100
106
body : ':thumbsup:' ,
101
107
statusCode : 200 ,
@@ -111,10 +117,10 @@ test('Returns a 405 error for requests using the DELETE method', async (t) => {
111
117
}
112
118
const response = await invokeLambda ( builder ( myHandler ) , { method : 'DELETE' } )
113
119
114
- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
120
+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
115
121
} )
116
122
117
- test ( 'Returns a 405 error for requests using the PATCH method' , async ( t ) => {
123
+ test ( 'Returns a 405 error for requests using the PATCH method' , async ( ) => {
118
124
const originalResponse = {
119
125
body : ':thumbsup:' ,
120
126
statusCode : 200 ,
@@ -130,12 +136,13 @@ test('Returns a 405 error for requests using the PATCH method', async (t) => {
130
136
}
131
137
const response = await invokeLambda ( builder ( myHandler ) , { method : 'PATCH' } )
132
138
133
- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
139
+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
134
140
} )
135
141
136
- test ( 'Preserves errors thrown inside the wrapped handler' , async ( t ) => {
142
+ test ( 'Preserves errors thrown inside the wrapped handler' , async ( ) => {
137
143
const error = new Error ( 'Uh-oh!' )
138
144
145
+ // @ts -expect-error There's no type for this custom property.
139
146
error . someProperty = ':thumbsdown:'
140
147
141
148
const myHandler = async ( ) => {
@@ -148,27 +155,32 @@ test('Preserves errors thrown inside the wrapped handler', async (t) => {
148
155
throw error
149
156
}
150
157
151
- await t . throwsAsync ( invokeLambda ( builder ( myHandler ) ) , { is : error } )
158
+ try {
159
+ await invokeLambda ( builder ( myHandler ) )
160
+
161
+ throw new Error ( 'Invocation should have failed' )
162
+ } catch { }
152
163
} )
153
164
154
- test ( 'Does not pass query parameters to the wrapped handler' , async ( t ) => {
165
+ test ( 'Does not pass query parameters to the wrapped handler' , async ( ) => {
155
166
const originalResponse = {
156
167
body : ':thumbsup:' ,
157
168
statusCode : 200 ,
158
169
}
159
170
// eslint-disable-next-line require-await
160
- const myHandler = async ( event ) => {
161
- t . deepEqual ( event . multiValueQueryStringParameters , { } )
162
- t . deepEqual ( event . queryStringParameters , { } )
171
+ const myHandler = async ( event : HandlerEvent ) => {
172
+ expect ( event . multiValueQueryStringParameters ) . toStrictEqual ( { } )
173
+ expect ( event . queryStringParameters ) . toStrictEqual ( { } )
163
174
164
175
return originalResponse
165
176
}
166
177
const multiValueQueryStringParameters = { foo : [ 'bar' ] , bar : [ 'baz' ] }
167
178
const queryStringParameters = { foo : 'bar' , bar : 'baz' }
168
179
const response = await invokeLambda ( builder ( myHandler ) , {
180
+ // @ts -expect-error TODO: Fic types.
169
181
multiValueQueryStringParameters,
170
182
queryStringParameters,
171
183
} )
172
184
173
- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
185
+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
174
186
} )
0 commit comments