|
| 1 | +import { describe } from 'node:test'; |
| 2 | +import { expect, expectTypeOf, it } from 'vitest'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { EventBridgeEnvelope, SqsEnvelope } from '../../src/envelopes/index.js'; |
| 5 | +import { parse } from '../../src/parser.js'; |
| 6 | +import type { EventBridgeEvent, SqsEvent } from '../../src/types/schema.js'; |
| 7 | +import { getTestEvent } from '../unit/helpers/utils.js'; |
| 8 | + |
| 9 | +describe('Parser types', () => { |
| 10 | + const userSchema = z.object({ |
| 11 | + name: z.string(), |
| 12 | + age: z.number(), |
| 13 | + }); |
| 14 | + type User = z.infer<typeof userSchema>; |
| 15 | + const input = { name: 'John', age: 30 }; |
| 16 | + |
| 17 | + const eventBridgeBaseEvent = getTestEvent<EventBridgeEvent>({ |
| 18 | + eventsPath: 'eventbridge', |
| 19 | + filename: 'base', |
| 20 | + }); |
| 21 | + |
| 22 | + const sqsBaseEvent = getTestEvent<SqsEvent>({ |
| 23 | + eventsPath: 'sqs', |
| 24 | + filename: 'base', |
| 25 | + }); |
| 26 | + it('infers return type for schema and safeParse', () => { |
| 27 | + // Act |
| 28 | + const result = parse(input, undefined, userSchema, true); |
| 29 | + |
| 30 | + // Assert |
| 31 | + if (result.success) { |
| 32 | + expectTypeOf(result.data).toEqualTypeOf<User>(); |
| 33 | + } else { |
| 34 | + expectTypeOf(result.originalEvent).toEqualTypeOf<User | undefined>(); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('infers return type for schema', () => { |
| 39 | + // Act |
| 40 | + const result = parse(input, undefined, userSchema); |
| 41 | + |
| 42 | + // Assert |
| 43 | + expectTypeOf(result).toEqualTypeOf<User>(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('infers return type for schema and envelope', () => { |
| 47 | + // Prepare |
| 48 | + const event = structuredClone(eventBridgeBaseEvent); |
| 49 | + event.detail = input; |
| 50 | + |
| 51 | + // Act |
| 52 | + const result = parse(event, EventBridgeEnvelope, userSchema); |
| 53 | + |
| 54 | + // Assert |
| 55 | + expectTypeOf(result).toEqualTypeOf<User>(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('infert return type for schema, object envelope and safeParse', () => { |
| 59 | + // Prepare |
| 60 | + const event = structuredClone(eventBridgeBaseEvent); |
| 61 | + event.detail = input; |
| 62 | + |
| 63 | + // Act |
| 64 | + const result = parse(event, EventBridgeEnvelope, userSchema, true); |
| 65 | + |
| 66 | + // Assert |
| 67 | + if (result.success) { |
| 68 | + expectTypeOf(result.data).toEqualTypeOf<User>(); |
| 69 | + expect(result.data).toEqual(input); |
| 70 | + } else { |
| 71 | + throw new Error('Parsing failed'); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it('infers return type for schema, array envelope and safeParse', () => { |
| 76 | + // Prepare |
| 77 | + const event = structuredClone(sqsBaseEvent); |
| 78 | + event.Records[0].body = JSON.stringify(input); |
| 79 | + |
| 80 | + // Act |
| 81 | + const result = parse(input, SqsEnvelope, userSchema, true); |
| 82 | + |
| 83 | + // Assert |
| 84 | + if (result.success) { |
| 85 | + expectTypeOf(result.data).toEqualTypeOf<User[]>(); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
0 commit comments