@@ -11,13 +11,10 @@ import { generateMock } from '@anatine/zod-mock';
11
11
import { SqsSchema } from '../../src/schemas/sqs.js' ;
12
12
import { z , ZodSchema } from 'zod' ;
13
13
import { sqsEnvelope } from '../../src/envelopes/sqs' ;
14
+ import { TestSchema } from './schema/utils' ;
14
15
15
16
describe ( 'Middleware: parser' , ( ) => {
16
- const schema = z . object ( {
17
- name : z . string ( ) ,
18
- age : z . number ( ) . min ( 18 ) . max ( 99 ) ,
19
- } ) ;
20
- type schema = z . infer < typeof schema > ;
17
+ type schema = z . infer < typeof TestSchema > ;
21
18
const handler = async (
22
19
event : schema | unknown ,
23
20
_context : Context
@@ -27,11 +24,11 @@ describe('Middleware: parser', () => {
27
24
28
25
describe ( ' when envelope is provided ' , ( ) => {
29
26
const middyfiedHandler = middy ( handler ) . use (
30
- parser ( { schema : schema , envelope : sqsEnvelope } )
27
+ parser ( { schema : TestSchema , envelope : sqsEnvelope } )
31
28
) ;
32
29
33
30
it ( 'should parse request body with schema and envelope' , async ( ) => {
34
- const bodyMock = generateMock ( schema ) ;
31
+ const bodyMock = generateMock ( TestSchema ) ;
35
32
36
33
const event = generateMock ( SqsSchema , {
37
34
stringMap : {
@@ -97,13 +94,17 @@ describe('Middleware: parser', () => {
97
94
98
95
it ( 'should parse custom event' , async ( ) => {
99
96
const event = { name : 'John' , age : 18 } ;
100
- const middyfiedHandler = middy ( handler ) . use ( parser ( { schema } ) ) ;
97
+ const middyfiedHandler = middy ( handler ) . use (
98
+ parser ( { schema : TestSchema } )
99
+ ) ;
101
100
102
101
expect ( await middyfiedHandler ( event , { } as Context ) ) . toEqual ( event ) ;
103
102
} ) ;
104
103
105
104
it ( 'should throw when the schema does not match' , async ( ) => {
106
- const middyfiedHandler = middy ( handler ) . use ( parser ( { schema } ) ) ;
105
+ const middyfiedHandler = middy ( handler ) . use (
106
+ parser ( { schema : TestSchema } )
107
+ ) ;
107
108
108
109
await expect ( middyfiedHandler ( 42 , { } as Context ) ) . rejects . toThrow ( ) ;
109
110
} ) ;
0 commit comments