|
| 1 | +/** |
| 2 | + * Test middelware parser |
| 3 | + * |
| 4 | + * @group unit/parser |
| 5 | + */ |
| 6 | + |
| 7 | +import middy from '@middy/core'; |
| 8 | +import { Context } from 'aws-lambda'; |
| 9 | +import { parser } from '../../src/middleware/parser.js'; |
| 10 | +import { generateMock } from '@anatine/zod-mock'; |
| 11 | +import { SqsSchema } from '../../src/schemas/sqs.js'; |
| 12 | +import { z, ZodSchema } from 'zod'; |
| 13 | +import { sqsEnvelope } from '../../src/envelopes/sqs'; |
| 14 | + |
| 15 | +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>; |
| 21 | + const handler = async ( |
| 22 | + event: schema | unknown, |
| 23 | + _context: Context |
| 24 | + ): Promise<schema | unknown> => { |
| 25 | + return event; |
| 26 | + }; |
| 27 | + |
| 28 | + describe(' when envelope is provided ', () => { |
| 29 | + const middyfiedHandler = middy(handler).use( |
| 30 | + parser({ schema: schema, envelope: sqsEnvelope }) |
| 31 | + ); |
| 32 | + |
| 33 | + it('should parse request body with schema and envelope', async () => { |
| 34 | + const bodyMock = generateMock(schema); |
| 35 | + |
| 36 | + const event = generateMock(SqsSchema, { |
| 37 | + stringMap: { |
| 38 | + body: () => JSON.stringify(bodyMock), |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const result = (await middyfiedHandler(event, {} as Context)) as schema[]; |
| 43 | + result.forEach((item) => { |
| 44 | + expect(item).toEqual(bodyMock); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should throw when envelope does not match', async () => { |
| 49 | + await expect(async () => { |
| 50 | + await middyfiedHandler({ name: 'John', age: 18 }, {} as Context); |
| 51 | + }).rejects.toThrowError(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should throw when schema does not match', async () => { |
| 55 | + const event = generateMock(SqsSchema, { |
| 56 | + stringMap: { |
| 57 | + body: () => '42', |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + await expect(middyfiedHandler(event, {} as Context)).rejects.toThrow(); |
| 62 | + }); |
| 63 | + it('should throw when provided schema is invalid', async () => { |
| 64 | + const middyfiedHandler = middy(handler).use( |
| 65 | + parser({ schema: {} as ZodSchema, envelope: sqsEnvelope }) |
| 66 | + ); |
| 67 | + |
| 68 | + await expect(middyfiedHandler(42, {} as Context)).rejects.toThrowError(); |
| 69 | + }); |
| 70 | + it('should throw when envelope is correct but schema is invalid', async () => { |
| 71 | + const event = generateMock(SqsSchema, { |
| 72 | + stringMap: { |
| 73 | + body: () => JSON.stringify({ name: 'John', foo: 'bar' }), |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const middyfiedHandler = middy(handler).use( |
| 78 | + parser({ schema: {} as ZodSchema, envelope: sqsEnvelope }) |
| 79 | + ); |
| 80 | + |
| 81 | + await expect( |
| 82 | + middyfiedHandler(event, {} as Context) |
| 83 | + ).rejects.toThrowError(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe(' when envelope is not provided', () => { |
| 88 | + it('should parse the event with built-in schema', async () => { |
| 89 | + const event = generateMock(SqsSchema); |
| 90 | + |
| 91 | + const middyfiedHandler = middy(handler).use( |
| 92 | + parser({ schema: SqsSchema }) |
| 93 | + ); |
| 94 | + |
| 95 | + expect(await middyfiedHandler(event, {} as Context)).toEqual(event); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should parse custom event', async () => { |
| 99 | + const event = { name: 'John', age: 18 }; |
| 100 | + const middyfiedHandler = middy(handler).use(parser({ schema })); |
| 101 | + |
| 102 | + expect(await middyfiedHandler(event, {} as Context)).toEqual(event); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should throw when the schema does not match', async () => { |
| 106 | + const middyfiedHandler = middy(handler).use(parser({ schema })); |
| 107 | + |
| 108 | + await expect(middyfiedHandler(42, {} as Context)).rejects.toThrow(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should throw when provided schema is invalid', async () => { |
| 112 | + const middyfiedHandler = middy(handler).use( |
| 113 | + parser({ schema: {} as ZodSchema }) |
| 114 | + ); |
| 115 | + |
| 116 | + await expect( |
| 117 | + middyfiedHandler({ foo: 'bar' }, {} as Context) |
| 118 | + ).rejects.toThrowError(); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments