1
1
import context from '@aws-lambda-powertools/testing-utils/context' ;
2
- import type { APIGatewayProxyEvent , Context } from 'aws-lambda' ;
2
+ import type { Context } from 'aws-lambda' ;
3
3
import { beforeEach , describe , expect , it , vi } from 'vitest' ;
4
4
import { BaseRouter } from '../../../src/rest/BaseRouter.js' ;
5
5
import { HttpErrorCodes , HttpVerbs } from '../../../src/rest/constants.js' ;
@@ -17,72 +17,13 @@ import type {
17
17
RouteHandler ,
18
18
RouterOptions ,
19
19
} 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' ;
86
27
87
28
describe ( 'Class: BaseRouter' , ( ) => {
88
29
class TestResolver extends BaseRouter {
@@ -493,7 +434,6 @@ describe('Class: BaseRouter', () => {
493
434
// Prepare
494
435
const app = new TestResolver ( ) ;
495
436
const executionOrder : string [ ] = [ ] ;
496
- let middlewareScope : any ;
497
437
498
438
app . use ( async ( params , options , next ) => {
499
439
executionOrder . push ( 'middleware-start' ) ;
@@ -507,7 +447,6 @@ describe('Class: BaseRouter', () => {
507
447
@app . get ( '/test' )
508
448
public async getTest ( ) {
509
449
executionOrder . push ( 'handler' ) ;
510
- middlewareScope = this . scope ;
511
450
return { message : `${ this . scope } : success` } ;
512
451
}
513
452
@@ -529,7 +468,6 @@ describe('Class: BaseRouter', () => {
529
468
'handler' ,
530
469
'middleware-end' ,
531
470
] ) ;
532
- expect ( middlewareScope ) . toBe ( 'class-scope' ) ;
533
471
expect ( result ) . toEqual ( {
534
472
statusCode : 200 ,
535
473
body : JSON . stringify ( { message : 'class-scope: success' } ) ,
@@ -1032,24 +970,24 @@ describe('Class: BaseRouter', () => {
1032
970
) ;
1033
971
1034
972
class Lambda {
973
+ public scope = 'class-scope' ;
974
+
1035
975
@app . get ( '/test' , [ middleware ] )
1036
976
public async getTest ( ) {
1037
977
executionOrder . push ( 'handler' ) ;
1038
- return { result : ' decorator-with-middleware' } ;
978
+ return { result : ` ${ this . scope } : decorator-with-middleware` } ;
1039
979
}
1040
980
1041
981
public async handler ( event : unknown , context : Context ) {
1042
- return app . resolve ( event , context ) ;
982
+ return app . resolve ( event , context , { scope : this } ) ;
1043
983
}
1044
984
}
1045
985
1046
986
const lambda = new Lambda ( ) ;
987
+ const handler = lambda . handler . bind ( lambda ) ;
1047
988
1048
989
// Act
1049
- const result = await lambda . handler (
1050
- createTestEvent ( '/test' , 'GET' ) ,
1051
- context
1052
- ) ;
990
+ const result = await handler ( createTestEvent ( '/test' , 'GET' ) , context ) ;
1053
991
1054
992
// Assess
1055
993
expect ( executionOrder ) . toEqual ( [
@@ -1059,7 +997,9 @@ describe('Class: BaseRouter', () => {
1059
997
] ) ;
1060
998
expect ( result ) . toEqual ( {
1061
999
statusCode : 200 ,
1062
- body : JSON . stringify ( { result : 'decorator-with-middleware' } ) ,
1000
+ body : JSON . stringify ( {
1001
+ result : 'class-scope: decorator-with-middleware' ,
1002
+ } ) ,
1063
1003
headers : { 'Content-Type' : 'application/json' } ,
1064
1004
isBase64Encoded : false ,
1065
1005
} ) ;
0 commit comments