@@ -10,6 +10,7 @@ const {
10
10
HandlerNotFound,
11
11
ImportModuleError,
12
12
MalformedHandlerName,
13
+ UserCodeSyntaxError,
13
14
} = require ( 'lambda-runtime/Errors.js' ) ;
14
15
const UserFunction = require ( 'lambda-runtime/UserFunction.js' ) ;
15
16
@@ -250,6 +251,166 @@ describe('UserFunction.load method', () => {
250
251
251
252
response . should . be . resolvedWith ( 'moon' ) ;
252
253
} ) ;
254
+
255
+ it ( 'should successfully load a CJS handler from extensionless file (no package.json)' , async ( ) => {
256
+ const handler = await UserFunction . load (
257
+ path . join ( HANDLERS_ROOT , 'extensionless' ) ,
258
+ 'index.handler' ,
259
+ ) ;
260
+ const response = await handler ( 'test event' ) ;
261
+
262
+ response . should . equal ( 'Hello from extensionless CJS' ) ;
263
+ } ) ;
264
+
265
+ it ( 'should fail to load ESM syntax from extensionless file (no package.json)' , async ( ) => {
266
+ await UserFunction . load (
267
+ path . join ( HANDLERS_ROOT , 'extensionless' ) ,
268
+ 'esm-extensionless.handler' ,
269
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
270
+ } ) ;
271
+
272
+ it ( 'should load CJS handler from extensionless file with type:commonjs' , async ( ) => {
273
+ // package.json is ignored in the case of extensionless
274
+ const handler = await UserFunction . load (
275
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-cjs' ) ,
276
+ 'cjs.handler' ,
277
+ ) ;
278
+ const response = await handler ( 'test event' ) ;
279
+
280
+ response . should . equal ( 'Hello from extensionless CJS' ) ;
281
+ } ) ;
282
+
283
+ it ( 'should fail to load ESM handler from extensionless file with type:commonjs' , async ( ) => {
284
+ // package.json is ignored in the case of extensionless
285
+ await UserFunction . load (
286
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-cjs' ) ,
287
+ 'esm.handler' ,
288
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
289
+ } ) ;
290
+
291
+ it ( 'should load CJS handler from extensionless file with type:module' , async ( ) => {
292
+ // package.json is ignored in the case of extensionless
293
+ const handler = await UserFunction . load (
294
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-esm' ) ,
295
+ 'cjs.handler' ,
296
+ ) ;
297
+ const response = await handler ( 'test event' ) ;
298
+
299
+ response . should . equal ( 'Hello from extensionless CJS' ) ;
300
+ } ) ;
301
+
302
+ it ( 'should fail to load ESM handler from extensionless file with type:module' , async ( ) => {
303
+ // package.json is ignored in the case of extensionless
304
+ await UserFunction . load (
305
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-esm' ) ,
306
+ 'esm.handler' ,
307
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
308
+ } ) ;
309
+
310
+ it ( 'should load CJS handler from JS file with type:commonjs' , async ( ) => {
311
+ const handler = await UserFunction . load (
312
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-cjs' ) ,
313
+ 'cjsModule.handler' ,
314
+ ) ;
315
+ const response = await handler ( 'test event' ) ;
316
+
317
+ response . should . equal ( 'Hello from CJS.js' ) ;
318
+ } ) ;
319
+
320
+ it ( 'should fail to load ESM handler from JS file with type:commonjs' , async ( ) => {
321
+ await UserFunction . load (
322
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-cjs' ) ,
323
+ 'esmModule.handler' ,
324
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
325
+ } ) ;
326
+
327
+ it ( 'should load ESM handler from JS file with type:module' , async ( ) => {
328
+ const handler = await UserFunction . load (
329
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-esm' ) ,
330
+ 'esmModule.handler' ,
331
+ ) ;
332
+ const response = await handler ( 'test event' ) ;
333
+
334
+ response . should . equal ( 'Hello from ESM.js' ) ;
335
+ } ) ;
336
+
337
+ it ( 'should fail to load CJS handler from JS file with type:module' , async ( ) => {
338
+ await UserFunction . load (
339
+ path . join ( HANDLERS_ROOT , 'pkg' , 'type-esm' ) ,
340
+ 'cjsModule.handler' ,
341
+ ) . should . be . rejectedWith (
342
+ ReferenceError ,
343
+ / m o d u l e i s n o t d e f i n e d i n E S m o d u l e s c o p e / ,
344
+ ) ;
345
+ } ) ;
346
+
347
+ it ( 'should fail to load ESM handler from JS file without type context' , async ( ) => {
348
+ await UserFunction . load (
349
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
350
+ 'esmModule.handler' ,
351
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
352
+ } ) ;
353
+
354
+ it ( 'should fail to load CJS handler from MJS file without type context' , async ( ) => {
355
+ await UserFunction . load (
356
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
357
+ 'cjsInMjs.handler' ,
358
+ ) . should . be . rejectedWith (
359
+ ReferenceError ,
360
+ / m o d u l e i s n o t d e f i n e d i n E S m o d u l e s c o p e / ,
361
+ ) ;
362
+ } ) ;
363
+
364
+ it ( 'should fail to load ESM handler from CJS file without type context' , async ( ) => {
365
+ await UserFunction . load (
366
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
367
+ 'esmInCjs.handler' ,
368
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
369
+ } ) ;
370
+
371
+ it ( 'should fail to load mixed context handler from JS file without type context' , async ( ) => {
372
+ await UserFunction . load (
373
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
374
+ 'cjsAndMjs.handler' ,
375
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
376
+ } ) ;
377
+
378
+ it ( 'should successfully load ESM handler importing from CJS' , async ( ) => {
379
+ const handler = await UserFunction . load (
380
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
381
+ 'esmImportCjs.handler' ,
382
+ ) ;
383
+
384
+ const response = await handler ( ) ;
385
+ response . should . equal ( 'Hello from CJS!' ) ;
386
+ } ) ;
387
+
388
+ it ( 'should fail when CJS tries to import from ESM using static import' , async ( ) => {
389
+ await UserFunction . load (
390
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
391
+ 'cjsImportESM.handler' ,
392
+ ) . should . be . rejectedWith ( UserCodeSyntaxError ) ;
393
+ } ) ;
394
+
395
+ it ( 'should successfully load CJS handler importing from CJS' , async ( ) => {
396
+ const handler = await UserFunction . load (
397
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
398
+ 'cjsImportCjs.handler' ,
399
+ ) ;
400
+
401
+ const response = await handler ( ) ;
402
+ response . should . equal ( 'Hello from CJS!' ) ;
403
+ } ) ;
404
+
405
+ it ( 'should fail when using require in .mjs' , async ( ) => {
406
+ await UserFunction . load (
407
+ path . join ( HANDLERS_ROOT , 'pkg-less' ) ,
408
+ 'esmRequireCjs.handler' ,
409
+ ) . should . be . rejectedWith (
410
+ ReferenceError ,
411
+ / r e q u i r e i s n o t d e f i n e d i n E S m o d u l e s c o p e / ,
412
+ ) ;
413
+ } ) ;
253
414
} ) ;
254
415
255
416
describe ( 'type guards HandlerFunction' , ( ) => {
0 commit comments