11import context from '@aws-lambda-powertools/testing-utils/context' ;
2- import type { APIGatewayProxyEvent , Context } from 'aws-lambda' ;
2+ import type { Context } from 'aws-lambda' ;
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44import { BaseRouter } from '../../../src/rest/BaseRouter.js' ;
55import { HttpErrorCodes , HttpVerbs } from '../../../src/rest/constants.js' ;
@@ -17,72 +17,13 @@ import type {
1717 RouteHandler ,
1818 RouterOptions ,
1919} from '../../../src/types/rest.js' ;
20-
21- const createTestEvent = (
22- path : string ,
23- httpMethod : string
24- ) : APIGatewayProxyEvent => ( {
25- path,
26- httpMethod,
27- headers : { } ,
28- body : null ,
29- multiValueHeaders : { } ,
30- isBase64Encoded : false ,
31- pathParameters : null ,
32- queryStringParameters : null ,
33- multiValueQueryStringParameters : null ,
34- stageVariables : null ,
35- requestContext : {
36- httpMethod,
37- path,
38- domainName : 'localhost' ,
39- } as any ,
40- resource : '' ,
41- } ) ;
42-
43- // Middleware factory functions
44- const createTrackingMiddleware = (
45- name : string ,
46- executionOrder : string [ ]
47- ) : Middleware => {
48- return async ( _params , _options , next ) => {
49- executionOrder . push ( `${ name } -start` ) ;
50- await next ( ) ;
51- executionOrder . push ( `${ name } -end` ) ;
52- } ;
53- } ;
54-
55- const createThrowingMiddleware = (
56- name : string ,
57- executionOrder : string [ ] ,
58- errorMessage : string
59- ) : Middleware => {
60- return async ( _params , _options , _next ) => {
61- executionOrder . push ( name ) ;
62- throw new Error ( errorMessage ) ;
63- } ;
64- } ;
65-
66- const createReturningMiddleware = (
67- name : string ,
68- executionOrder : string [ ] ,
69- response : any
70- ) : Middleware => {
71- return async ( _params , _options , _next ) => {
72- executionOrder . push ( name ) ;
73- return response ;
74- } ;
75- } ;
76-
77- const createNoNextMiddleware = (
78- name : string ,
79- executionOrder : string [ ]
80- ) : Middleware => {
81- return async ( _params , _options , _next ) => {
82- executionOrder . push ( name ) ;
83- // Intentionally doesn't call next()
84- } ;
85- } ;
20+ import {
21+ createNoNextMiddleware ,
22+ createReturningMiddleware ,
23+ createTestEvent ,
24+ createThrowingMiddleware ,
25+ createTrackingMiddleware ,
26+ } from './helpers.js' ;
8627
8728describe ( 'Class: BaseRouter' , ( ) => {
8829 class TestResolver extends BaseRouter {
@@ -493,7 +434,6 @@ describe('Class: BaseRouter', () => {
493434 // Prepare
494435 const app = new TestResolver ( ) ;
495436 const executionOrder : string [ ] = [ ] ;
496- let middlewareScope : any ;
497437
498438 app . use ( async ( params , options , next ) => {
499439 executionOrder . push ( 'middleware-start' ) ;
@@ -507,7 +447,6 @@ describe('Class: BaseRouter', () => {
507447 @app . get ( '/test' )
508448 public async getTest ( ) {
509449 executionOrder . push ( 'handler' ) ;
510- middlewareScope = this . scope ;
511450 return { message : `${ this . scope } : success` } ;
512451 }
513452
@@ -529,7 +468,6 @@ describe('Class: BaseRouter', () => {
529468 'handler' ,
530469 'middleware-end' ,
531470 ] ) ;
532- expect ( middlewareScope ) . toBe ( 'class-scope' ) ;
533471 expect ( result ) . toEqual ( {
534472 statusCode : 200 ,
535473 body : JSON . stringify ( { message : 'class-scope: success' } ) ,
@@ -1032,24 +970,24 @@ describe('Class: BaseRouter', () => {
1032970 ) ;
1033971
1034972 class Lambda {
973+ public scope = 'class-scope' ;
974+
1035975 @app . get ( '/test' , [ middleware ] )
1036976 public async getTest ( ) {
1037977 executionOrder . push ( 'handler' ) ;
1038- return { result : ' decorator-with-middleware' } ;
978+ return { result : ` ${ this . scope } : decorator-with-middleware` } ;
1039979 }
1040980
1041981 public async handler ( event : unknown , context : Context ) {
1042- return app . resolve ( event , context ) ;
982+ return app . resolve ( event , context , { scope : this } ) ;
1043983 }
1044984 }
1045985
1046986 const lambda = new Lambda ( ) ;
987+ const handler = lambda . handler . bind ( lambda ) ;
1047988
1048989 // Act
1049- const result = await lambda . handler (
1050- createTestEvent ( '/test' , 'GET' ) ,
1051- context
1052- ) ;
990+ const result = await handler ( createTestEvent ( '/test' , 'GET' ) , context ) ;
1053991
1054992 // Assess
1055993 expect ( executionOrder ) . toEqual ( [
@@ -1059,7 +997,9 @@ describe('Class: BaseRouter', () => {
1059997 ] ) ;
1060998 expect ( result ) . toEqual ( {
1061999 statusCode : 200 ,
1062- body : JSON . stringify ( { result : 'decorator-with-middleware' } ) ,
1000+ body : JSON . stringify ( {
1001+ result : 'class-scope: decorator-with-middleware' ,
1002+ } ) ,
10631003 headers : { 'Content-Type' : 'application/json' } ,
10641004 isBase64Encoded : false ,
10651005 } ) ;
0 commit comments