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