1- const test = require ( 'ava' )
1+ import { expect , test } from 'vitest'
22
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'
58
69const METADATA_OBJECT = { metadata : { version : 1 , builder_function : true , ttl : 0 } }
710
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
913 const originalResponse = {
1014 body : ':thumbsup:' ,
1115 statusCode : 200 ,
12- ttl : 3600 ,
16+ ttl,
1317 }
1418 const myHandler = async ( ) => {
1519 const asyncTask = new Promise ( ( resolve ) => {
@@ -22,23 +26,25 @@ test('Injects the metadata object into an asynchronous handler', async (t) => {
2226 }
2327 const response = await invokeLambda ( builder ( myHandler ) )
2428
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 } } )
2630} )
2731
28- test ( 'Injects the metadata object into a synchronous handler' , async ( t ) => {
32+ test ( 'Injects the metadata object into a synchronous handler' , async ( ) => {
2933 const originalResponse = {
3034 body : ':thumbsup:' ,
3135 statusCode : 200 ,
3236 }
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 )
3541 }
3642 const response = await invokeLambda ( builder ( myHandler ) )
3743
38- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
44+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
3945} )
4046
41- test ( 'Injects the metadata object for non-200 responses' , async ( t ) => {
47+ test ( 'Injects the metadata object for non-200 responses' , async ( ) => {
4248 const originalResponse = {
4349 body : ':thumbsdown:' ,
4450 statusCode : 404 ,
@@ -54,10 +60,10 @@ test('Injects the metadata object for non-200 responses', async (t) => {
5460 }
5561 const response = await invokeLambda ( builder ( myHandler ) )
5662
57- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
63+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
5864} )
5965
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 ( ) => {
6167 const originalResponse = {
6268 body : ':thumbsup:' ,
6369 statusCode : 200 ,
@@ -73,10 +79,10 @@ test('Returns a 405 error for requests using the POST method', async (t) => {
7379 }
7480 const response = await invokeLambda ( builder ( myHandler ) , { method : 'POST' } )
7581
76- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
82+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
7783} )
7884
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 ( ) => {
8086 const originalResponse = {
8187 body : ':thumbsup:' ,
8288 statusCode : 200 ,
@@ -92,10 +98,10 @@ test('Returns a 405 error for requests using the PUT method', async (t) => {
9298 }
9399 const response = await invokeLambda ( builder ( myHandler ) , { method : 'PUT' } )
94100
95- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
101+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
96102} )
97103
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 ( ) => {
99105 const originalResponse = {
100106 body : ':thumbsup:' ,
101107 statusCode : 200 ,
@@ -111,10 +117,10 @@ test('Returns a 405 error for requests using the DELETE method', async (t) => {
111117 }
112118 const response = await invokeLambda ( builder ( myHandler ) , { method : 'DELETE' } )
113119
114- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
120+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
115121} )
116122
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 ( ) => {
118124 const originalResponse = {
119125 body : ':thumbsup:' ,
120126 statusCode : 200 ,
@@ -130,12 +136,13 @@ test('Returns a 405 error for requests using the PATCH method', async (t) => {
130136 }
131137 const response = await invokeLambda ( builder ( myHandler ) , { method : 'PATCH' } )
132138
133- t . deepEqual ( response , { body : 'Method Not Allowed' , statusCode : 405 } )
139+ expect ( response ) . toStrictEqual ( { body : 'Method Not Allowed' , statusCode : 405 } )
134140} )
135141
136- test ( 'Preserves errors thrown inside the wrapped handler' , async ( t ) => {
142+ test ( 'Preserves errors thrown inside the wrapped handler' , async ( ) => {
137143 const error = new Error ( 'Uh-oh!' )
138144
145+ // @ts -expect-error There's no type for this custom property.
139146 error . someProperty = ':thumbsdown:'
140147
141148 const myHandler = async ( ) => {
@@ -148,27 +155,32 @@ test('Preserves errors thrown inside the wrapped handler', async (t) => {
148155 throw error
149156 }
150157
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 { }
152163} )
153164
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 ( ) => {
155166 const originalResponse = {
156167 body : ':thumbsup:' ,
157168 statusCode : 200 ,
158169 }
159170 // 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 ( { } )
163174
164175 return originalResponse
165176 }
166177 const multiValueQueryStringParameters = { foo : [ 'bar' ] , bar : [ 'baz' ] }
167178 const queryStringParameters = { foo : 'bar' , bar : 'baz' }
168179 const response = await invokeLambda ( builder ( myHandler ) , {
180+ // @ts -expect-error TODO: Fic types.
169181 multiValueQueryStringParameters,
170182 queryStringParameters,
171183 } )
172184
173- t . deepEqual ( response , { ...originalResponse , ...METADATA_OBJECT } )
185+ expect ( response ) . toStrictEqual ( { ...originalResponse , ...METADATA_OBJECT } )
174186} )
0 commit comments