From ef231d0ec43c0c098177c7a86ab23b6e1abb8c6f Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 24 Jan 2025 00:33:45 +0100 Subject: [PATCH 1/3] fix(parser): min array length on Records --- packages/parser/src/schemas/ses.ts | 2 +- .../custom-resource-create.json} | 0 .../custom-resource-delete.json} | 0 .../custom-resource-update.json} | 0 .../events/{sesEvent.json => ses/base.json} | 0 .../parser/tests/unit/envelopes/sqs.test.ts | 3 +- .../tests/unit/parser.decorator.test.ts | 2 +- .../parser/tests/unit/schema/appsync.test.ts | 99 ++++----------- .../cloudformation-custom-resource.test.ts | 117 +++++++++++++----- .../parser/tests/unit/schema/dynamodb.test.ts | 2 +- .../tests/unit/schema/eventbridge.test.ts | 2 +- .../parser/tests/unit/schema/lambda.test.ts | 24 ++-- packages/parser/tests/unit/schema/ses.test.ts | 37 ++++-- packages/parser/tests/unit/schema/utils.ts | 7 -- 14 files changed, 159 insertions(+), 136 deletions(-) rename packages/parser/tests/events/{cloudFormationCustomResourceCreateEvent.json => cloudformation/custom-resource-create.json} (100%) rename packages/parser/tests/events/{cloudFormationCustomResourceDeleteEvent.json => cloudformation/custom-resource-delete.json} (100%) rename packages/parser/tests/events/{cloudFormationCustomResourceUpdateEvent.json => cloudformation/custom-resource-update.json} (100%) rename packages/parser/tests/events/{sesEvent.json => ses/base.json} (100%) diff --git a/packages/parser/src/schemas/ses.ts b/packages/parser/src/schemas/ses.ts index 0ed9866cc9..8121d70c70 100644 --- a/packages/parser/src/schemas/ses.ts +++ b/packages/parser/src/schemas/ses.ts @@ -173,7 +173,7 @@ const SesRecordSchema = z.object({ * @see {@link https://docs.aws.amazon.com/ses/latest/dg/receiving-email-notifications-examples.html} */ const SesSchema = z.object({ - Records: z.array(SesRecordSchema), + Records: z.array(SesRecordSchema).min(1), }); export { SesSchema, SesRecordSchema }; diff --git a/packages/parser/tests/events/cloudFormationCustomResourceCreateEvent.json b/packages/parser/tests/events/cloudformation/custom-resource-create.json similarity index 100% rename from packages/parser/tests/events/cloudFormationCustomResourceCreateEvent.json rename to packages/parser/tests/events/cloudformation/custom-resource-create.json diff --git a/packages/parser/tests/events/cloudFormationCustomResourceDeleteEvent.json b/packages/parser/tests/events/cloudformation/custom-resource-delete.json similarity index 100% rename from packages/parser/tests/events/cloudFormationCustomResourceDeleteEvent.json rename to packages/parser/tests/events/cloudformation/custom-resource-delete.json diff --git a/packages/parser/tests/events/cloudFormationCustomResourceUpdateEvent.json b/packages/parser/tests/events/cloudformation/custom-resource-update.json similarity index 100% rename from packages/parser/tests/events/cloudFormationCustomResourceUpdateEvent.json rename to packages/parser/tests/events/cloudformation/custom-resource-update.json diff --git a/packages/parser/tests/events/sesEvent.json b/packages/parser/tests/events/ses/base.json similarity index 100% rename from packages/parser/tests/events/sesEvent.json rename to packages/parser/tests/events/ses/base.json diff --git a/packages/parser/tests/unit/envelopes/sqs.test.ts b/packages/parser/tests/unit/envelopes/sqs.test.ts index d21273d47b..ea6135df98 100644 --- a/packages/parser/tests/unit/envelopes/sqs.test.ts +++ b/packages/parser/tests/unit/envelopes/sqs.test.ts @@ -51,7 +51,8 @@ describe('Envelope: SqsEnvelope', () => { expect(result).toStrictEqual([{ message: 'hello' }, { message: 'foo1' }]); }); }); - describe('safeParse', () => { + + describe('Method: safeParse', () => { it('parses an SQS event', () => { // Prepare const event = structuredClone(baseEvent); diff --git a/packages/parser/tests/unit/parser.decorator.test.ts b/packages/parser/tests/unit/parser.decorator.test.ts index 5ce14f7d04..f25dd22eb2 100644 --- a/packages/parser/tests/unit/parser.decorator.test.ts +++ b/packages/parser/tests/unit/parser.decorator.test.ts @@ -8,7 +8,7 @@ import { ParseError } from '../../src/errors.js'; import { parser } from '../../src/index.js'; import { EventBridgeSchema } from '../../src/schemas/index.js'; import type { EventBridgeEvent, ParsedResult } from '../../src/types'; -import { TestSchema, getTestEvent } from './schema/utils'; +import { TestSchema, getTestEvent } from './schema/utils.js'; describe('Parser Decorator', () => { const customEventBridgeSchema = EventBridgeSchema.extend({ diff --git a/packages/parser/tests/unit/schema/appsync.test.ts b/packages/parser/tests/unit/schema/appsync.test.ts index fe6fffd7f1..50c09dcc31 100644 --- a/packages/parser/tests/unit/schema/appsync.test.ts +++ b/packages/parser/tests/unit/schema/appsync.test.ts @@ -1,24 +1,19 @@ -/** - * Test built-in AppSync resolver schemas - */ - import { describe, expect, it } from 'vitest'; import { AppSyncBatchResolverSchema, AppSyncResolverSchema, -} from '../../../src/schemas/appsync'; -import type { AppSyncResolverEvent } from '../../../src/types'; -import { getTestEvent, omit } from './utils'; +} from '../../../src/schemas/appsync.js'; +import type { AppSyncResolverEvent } from '../../../src/types/schema.js'; +import { getTestEvent, omit } from './utils.js'; -describe('AppSync Resolver Schemas', () => { +describe('Schema: AppSync Resolver', () => { const eventsPath = 'appsync'; - - const appSyncResolverEvent: AppSyncResolverEvent = getTestEvent({ + const appSyncResolverEvent = getTestEvent({ eventsPath, filename: 'resolver', }); - const table = [ + const events = [ { name: 'null source', event: { @@ -119,73 +114,33 @@ describe('AppSync Resolver Schemas', () => { }, ]; - describe('AppSync Resolver Schema', () => { - it('should return validation error when the event is invalid', () => { - const { error } = AppSyncResolverSchema.safeParse( - omit(['request', 'info'], appSyncResolverEvent) - ); + it.each(events)('parses an AppSyn resolver event with $name', ({ event }) => { + // Assess + const result = AppSyncResolverSchema.parse(event); - expect(error?.issues).toEqual([ - { - code: 'invalid_type', - expected: 'object', - received: 'undefined', - path: ['request'], - message: 'Required', - }, - { - code: 'invalid_type', - expected: 'object', - received: 'undefined', - path: ['info'], - message: 'Required', - }, - ]); - }); + // Assess + expect(result).toEqual(event); + }); - it('should parse resolver event without identity field', () => { - const event: Omit = omit( - ['identity'], - appSyncResolverEvent - ); - const parsedEvent = AppSyncResolverSchema.parse(event); - expect(parsedEvent).toEqual(event); - }); + it('throws when the event is not an AppSync resolver event', () => { + // Prepare + const event = omit( + ['request', 'info'], + structuredClone(appSyncResolverEvent) + ); - it.each(table)('should parse resolver event with $name', ({ event }) => { - const parsedEvent = AppSyncResolverSchema.parse(event); - expect(parsedEvent).toEqual(event); - }); + // Act & Assess + expect(() => AppSyncResolverSchema.parse(event)).toThrow(); }); - describe('Batch AppSync Resolver Schema', () => { - it('should return validation error when the event is invalid', () => { - const event = omit(['request', 'info'], appSyncResolverEvent); - - const { error } = AppSyncBatchResolverSchema.safeParse([event]); + it('parses batches of AppSync resolver events', () => { + // Prepare + const event = events.map((event) => structuredClone(event.event)); - expect(error?.issues).toEqual([ - { - code: 'invalid_type', - expected: 'object', - received: 'undefined', - path: [0, 'request'], - message: 'Required', - }, - { - code: 'invalid_type', - expected: 'object', - received: 'undefined', - path: [0, 'info'], - message: 'Required', - }, - ]); - }); + // Act + const result = AppSyncBatchResolverSchema.parse(event); - it('should parse batches of appsync resolver events', () => { - const events = table.map((table) => table.event); - const parsedEvent = AppSyncBatchResolverSchema.parse(events); - expect(parsedEvent).toEqual(events); - }); + // Assess + expect(result).toEqual(event); }); }); diff --git a/packages/parser/tests/unit/schema/cloudformation-custom-resource.test.ts b/packages/parser/tests/unit/schema/cloudformation-custom-resource.test.ts index 445aac7366..594069b926 100644 --- a/packages/parser/tests/unit/schema/cloudformation-custom-resource.test.ts +++ b/packages/parser/tests/unit/schema/cloudformation-custom-resource.test.ts @@ -3,38 +3,89 @@ import { CloudFormationCustomResourceCreateSchema, CloudFormationCustomResourceDeleteSchema, CloudFormationCustomResourceUpdateSchema, -} from '../../../src/schemas/'; -import { TestEvents } from './utils.js'; - -describe('CloudFormationCustomResource ', () => { - it('should parse create event', () => { - const cloudFormationCustomResourceCreateEvent = - TestEvents.cloudFormationCustomResourceCreateEvent; - - expect( - CloudFormationCustomResourceCreateSchema.parse( - cloudFormationCustomResourceCreateEvent - ) - ).toEqual(cloudFormationCustomResourceCreateEvent); - }); - it('should parse update event', () => { - const cloudFormationCustomResourceUpdateEvent = - TestEvents.cloudFormationCustomResourceUpdateEvent; - - expect( - CloudFormationCustomResourceUpdateSchema.parse( - cloudFormationCustomResourceUpdateEvent - ) - ).toEqual(cloudFormationCustomResourceUpdateEvent); - }); - it('should parse delete event', () => { - const cloudFormationCustomResourceDeleteEvent = - TestEvents.cloudFormationCustomResourceDeleteEvent; - - expect( - CloudFormationCustomResourceDeleteSchema.parse( - cloudFormationCustomResourceDeleteEvent - ) - ).toEqual(cloudFormationCustomResourceDeleteEvent); +} from '../../../src/schemas/cloudformation-custom-resource.js'; +import type { + CloudFormationCustomResourceCreateEvent, + CloudFormationCustomResourceDeleteEvent, + CloudFormationCustomResourceUpdateEvent, +} from '../../../src/types/schema.js'; +import { getTestEvent, omit } from './utils.js'; + +describe('Schema: CloudFormationCustomResource ', () => { + const eventsPath = 'cloudformation'; + const baseCreate = getTestEvent({ + eventsPath, + filename: 'custom-resource-create', + }); + const baseDelete = getTestEvent({ + eventsPath, + filename: 'custom-resource-delete', + }); + const baseUpdate = getTestEvent({ + eventsPath, + filename: 'custom-resource-update', + }); + + it('parses a CloudFormation Custom Resource Create event', () => { + // Prepare + const event = structuredClone(baseCreate); + + // Act + const result = CloudFormationCustomResourceCreateSchema.parse(event); + + // Assess + expect(result).toStrictEqual(event); + }); + + it('throws if the event is not a CloudFormation Custom Resource Create event', () => { + // Prepare + const event = omit(['RequestType'], structuredClone(baseCreate)); + + // Act & Assess + expect(() => + CloudFormationCustomResourceCreateSchema.parse(event) + ).toThrow(); + }); + + it('parses a CloudFormation Custom Resource Delete event', () => { + // Prepare + const event = structuredClone(baseDelete); + + // Act + const result = CloudFormationCustomResourceDeleteSchema.parse(event); + + // Assess + expect(result).toStrictEqual(event); + }); + + it('throws if the event is not a CloudFormation Custom Resource Delete event', () => { + // Prepare + const event = omit(['LogicalResourceId'], structuredClone(baseDelete)); + + // Act & Assess + expect(() => + CloudFormationCustomResourceDeleteSchema.parse(event) + ).toThrow(); + }); + + it('parses a CloudFormation Custom Resource Update event', () => { + // Prepare + const event = structuredClone(baseUpdate); + + // Act + const result = CloudFormationCustomResourceUpdateSchema.parse(event); + + // Assess + expect(result).toStrictEqual(event); + }); + + it('throws if the event is not a CloudFormation Custom Resource Update event', () => { + // Prepare + const event = omit(['OldResourceProperties'], structuredClone(baseUpdate)); + + // Act & Assess + expect(() => + CloudFormationCustomResourceUpdateSchema.parse(event) + ).toThrow(); }); }); diff --git a/packages/parser/tests/unit/schema/dynamodb.test.ts b/packages/parser/tests/unit/schema/dynamodb.test.ts index e3522edb85..db684da308 100644 --- a/packages/parser/tests/unit/schema/dynamodb.test.ts +++ b/packages/parser/tests/unit/schema/dynamodb.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest'; import { DynamoDBStreamSchema } from '../../../src/schemas/dynamodb.js'; import type { DynamoDBStreamEvent } from '../../../src/types/schema.js'; -import { getTestEvent } from '../schema/utils.js'; +import { getTestEvent } from './utils.js'; describe('Schema: DynamoDB', () => { const baseEvent = getTestEvent({ diff --git a/packages/parser/tests/unit/schema/eventbridge.test.ts b/packages/parser/tests/unit/schema/eventbridge.test.ts index 49297b3a6c..8e612ea425 100644 --- a/packages/parser/tests/unit/schema/eventbridge.test.ts +++ b/packages/parser/tests/unit/schema/eventbridge.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest'; import { EventBridgeSchema } from '../../../src/schemas/eventbridge.js'; import type { EventBridgeEvent } from '../../../src/types/schema.js'; -import { getTestEvent, omit } from '../schema/utils.js'; +import { getTestEvent, omit } from './utils.js'; describe('Schema: EventBridge', () => { const baseEvent = getTestEvent({ diff --git a/packages/parser/tests/unit/schema/lambda.test.ts b/packages/parser/tests/unit/schema/lambda.test.ts index 42adf84ff8..e57e0e4d23 100644 --- a/packages/parser/tests/unit/schema/lambda.test.ts +++ b/packages/parser/tests/unit/schema/lambda.test.ts @@ -1,21 +1,28 @@ import { describe, expect, it } from 'vitest'; -import { LambdaFunctionUrlSchema } from '../../../src/schemas/'; +import { LambdaFunctionUrlSchema } from '../../../src/schemas/lambda.js'; +import type { LambdaFunctionUrlEvent } from '../../../src/types/schema.js'; import { getTestEvent } from './utils.js'; describe('Schema: LambdaFunctionUrl', () => { const eventsPath = 'lambda'; - it('throw when the event is invalid', () => { + it('throws when the event is invalid', () => { // Prepare - const event = getTestEvent({ eventsPath, filename: 'invalid' }); + const event = getTestEvent({ + eventsPath, + filename: 'invalid', + }); // Act & Assess expect(() => LambdaFunctionUrlSchema.parse(event)).toThrow(); }); - it('parses a valid event', () => { + it('parses a valid Lambda Function URL event', () => { // Prepare - const event = getTestEvent({ eventsPath, filename: 'get-request' }); + const event = getTestEvent({ + eventsPath, + filename: 'get-request', + }); // Act const parsedEvent = LambdaFunctionUrlSchema.parse(event); @@ -24,9 +31,12 @@ describe('Schema: LambdaFunctionUrl', () => { expect(parsedEvent).toEqual(event); }); - it('parses iam event', () => { + it('parses a Lambda Function URL event with iam', () => { // Prepare - const event = getTestEvent({ eventsPath, filename: 'iam-auth' }); + const event = getTestEvent({ + eventsPath, + filename: 'iam-auth', + }); // Act const parsedEvent = LambdaFunctionUrlSchema.parse(event); diff --git a/packages/parser/tests/unit/schema/ses.test.ts b/packages/parser/tests/unit/schema/ses.test.ts index 67ee78ac95..979e4091f6 100644 --- a/packages/parser/tests/unit/schema/ses.test.ts +++ b/packages/parser/tests/unit/schema/ses.test.ts @@ -1,19 +1,32 @@ import { describe, expect, it } from 'vitest'; -import { SesRecordSchema, SesSchema } from '../../../src/schemas/'; -import type { SesEvent } from '../../../src/types'; -import type { SesRecord } from '../../../src/types/schema'; -import { TestEvents } from './utils.js'; +import { SesSchema } from '../../../src/schemas/ses.js'; +import type { SesEvent } from '../../../src/types/index.js'; +import { getTestEvent, omit } from './utils.js'; -describe('SES', () => { - it('should parse ses event', () => { - const sesEvent = TestEvents.sesEvent; - expect(SesSchema.parse(sesEvent)).toEqual(sesEvent); +describe('Schema: SES', () => { + const baseEvent = getTestEvent({ + eventsPath: 'ses', + filename: 'base', }); - it('should parse record from ses event', () => { - const sesEvent: SesEvent = TestEvents.sesEvent as SesEvent; - const parsed: SesRecord = SesRecordSchema.parse(sesEvent.Records[0]); + it('parses a SES event', () => { + // Prepare + const event = structuredClone(baseEvent); - expect(parsed.ses.mail.source).toEqual('janedoe@example.com'); + // Act + const result = SesSchema.parse(event); + + // Assess + expect(result).toStrictEqual(event); + }); + + it('throws if the event is not a SES event', () => { + // Prepare + const event = { + Records: [], + }; + + // Act & Assess + expect(() => SesSchema.parse(event)).toThrow(); }); }); diff --git a/packages/parser/tests/unit/schema/utils.ts b/packages/parser/tests/unit/schema/utils.ts index de56e12a60..251910bec5 100644 --- a/packages/parser/tests/unit/schema/utils.ts +++ b/packages/parser/tests/unit/schema/utils.ts @@ -11,18 +11,12 @@ const filenames = [ 'albEvent', 'albEventPathTrailingSlash', 'albMultiValueHeadersEvent', - 'cloudFormationCustomResourceCreateEvent', - 'cloudFormationCustomResourceDeleteEvent', - 'cloudFormationCustomResourceUpdateEvent', 'kinesisFirehoseKinesisEvent', 'kinesisFirehosePutEvent', 'kinesisFirehoseSQSEvent', 'kinesisStreamCloudWatchLogsEvent', 'kinesisStreamEvent', 'kinesisStreamEventOneRecord', - 'lambdaFunctionUrlEvent', - 'lambdaFunctionUrlEventPathTrailingSlash', - 'lambdaFunctionUrlIAMEvent', 's3Event', 's3EventBridgeNotificationObjectCreatedEvent', 's3EventBridgeNotificationObjectDeletedEvent', @@ -35,7 +29,6 @@ const filenames = [ 's3ObjectEventIAMUser', 's3ObjectEventTempCredentials', 's3SqsEvent', - 'sesEvent', 'vpcLatticeEvent', 'vpcLatticeEventPathTrailingSlash', 'vpcLatticeEventV2PathTrailingSlash', From 744645c4200c7f903340c6dc946bf873ca4bda17 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 24 Jan 2025 00:52:23 +0100 Subject: [PATCH 2/3] tests: standardize decorator tests --- .../tests/unit/parser.decorator.test.ts | 180 +++++++----------- 1 file changed, 65 insertions(+), 115 deletions(-) diff --git a/packages/parser/tests/unit/parser.decorator.test.ts b/packages/parser/tests/unit/parser.decorator.test.ts index f25dd22eb2..9224ae181f 100644 --- a/packages/parser/tests/unit/parser.decorator.test.ts +++ b/packages/parser/tests/unit/parser.decorator.test.ts @@ -1,159 +1,109 @@ -import { generateMock } from '@anatine/zod-mock'; -import type { LambdaInterface } from '@aws-lambda-powertools/commons/lib/esm/types'; +import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; import type { Context } from 'aws-lambda'; import { describe, expect, it } from 'vitest'; -import type { z } from 'zod'; +import { type ZodSchema, z } from 'zod'; import { EventBridgeEnvelope } from '../../src/envelopes/index.js'; import { ParseError } from '../../src/errors.js'; import { parser } from '../../src/index.js'; import { EventBridgeSchema } from '../../src/schemas/index.js'; -import type { EventBridgeEvent, ParsedResult } from '../../src/types'; -import { TestSchema, getTestEvent } from './schema/utils.js'; +import type { EventBridgeEvent, ParsedResult } from '../../src/types/index.js'; +import { getTestEvent } from './schema/utils.js'; -describe('Parser Decorator', () => { - const customEventBridgeSchema = EventBridgeSchema.extend({ - detail: TestSchema, +describe('Decorator: parser', () => { + const schema = z.object({ + name: z.string(), + age: z.number(), + }); + const payload = { + name: 'John Doe', + age: 30, + }; + const extendedSchema = EventBridgeSchema.extend({ + detail: schema, + }); + type event = z.infer; + const baseEvent = getTestEvent({ + eventsPath: 'eventbridge', + filename: 'base', }); - - type TestEvent = z.infer; class TestClass implements LambdaInterface { - @parser({ schema: TestSchema }) - public async handler( - event: TestEvent, - _context: Context - ): Promise { - return event; - } - - @parser({ schema: customEventBridgeSchema }) - public async handlerWithCustomSchema( - event: unknown, - _context: Context - ): Promise { + @parser({ schema: extendedSchema }) + public async handler(event: event, _context: Context): Promise { return event; } - @parser({ schema: TestSchema, envelope: EventBridgeEnvelope }) + @parser({ schema, envelope: EventBridgeEnvelope }) public async handlerWithParserCallsAnotherMethod( - event: TestEvent, + event: z.infer, _context: Context ): Promise { return this.anotherMethod(event); } - @parser({ schema: TestSchema, envelope: EventBridgeEnvelope }) - public async handlerWithSchemaAndEnvelope( - event: TestEvent, - _context: Context - ): Promise { - return event; - } - @parser({ - schema: TestSchema, + schema, safeParse: true, }) public async handlerWithSchemaAndSafeParse( - event: ParsedResult, + event: ParsedResult, _context: Context - ): Promise { + ): Promise> { return event; } @parser({ - schema: TestSchema, + schema, envelope: EventBridgeEnvelope, safeParse: true, }) public async harndlerWithEnvelopeAndSafeParse( - event: ParsedResult, + event: ParsedResult, _context: Context ): Promise { return event; } - private async anotherMethod(event: TestEvent): Promise { + private async anotherMethod( + event: z.infer + ): Promise> { return event; } } - const lambda = new TestClass(); - it('should parse custom schema event', async () => { - const testEvent = generateMock(TestSchema); + it('parses the event using the schema provided', async () => { + // Prepare + const event = structuredClone(baseEvent); + event.detail = payload; - const resp = await lambda.handler(testEvent, {} as Context); + // Act + // @ts-expect-error - extended schema + const result = await lambda.handler(event, {} as Context); - expect(resp).toEqual(testEvent); + // Assess + expect(result).toEqual(event); }); - it('should parse custom schema with envelope event', async () => { - const customPayload = generateMock(TestSchema); - const testEvent = getTestEvent({ - eventsPath: 'eventbridge', - filename: 'base', - }); - testEvent.detail = customPayload; + it('preserves the class method scope when decorated', async () => { + // Prepare + const event = structuredClone(baseEvent); + event.detail = payload; - const resp = await lambda.handlerWithSchemaAndEnvelope( - testEvent as unknown as TestEvent, + const result = await lambda.handlerWithParserCallsAnotherMethod( + // @ts-expect-error - extended schema + event, {} as Context ); - expect(resp).toEqual(customPayload); + expect(result).toEqual(event.detail); }); - it('should parse extended envelope event', async () => { - const customPayload = generateMock(TestSchema); - - const testEvent = generateMock(customEventBridgeSchema); - testEvent.detail = customPayload; - - const resp: z.infer = - (await lambda.handlerWithCustomSchema( - testEvent, - {} as Context - )) as z.infer; - - expect(customEventBridgeSchema.parse(resp)).toEqual(testEvent); - expect(resp.detail).toEqual(customPayload); - }); - - it('should parse and call private async method', async () => { - const customPayload = generateMock(TestSchema); - const testEvent = getTestEvent({ - eventsPath: 'eventbridge', - filename: 'base', - }); - testEvent.detail = customPayload; - - const resp = await lambda.handlerWithParserCallsAnotherMethod( - testEvent as unknown as TestEvent, - {} as Context - ); - - expect(resp).toEqual(customPayload); - }); - - it('should parse event with schema and safeParse', async () => { - const testEvent = generateMock(TestSchema); - - const resp = await lambda.handlerWithSchemaAndSafeParse( - testEvent as unknown as ParsedResult, - {} as Context - ); - - expect(resp).toEqual({ - success: true, - data: testEvent, - }); - }); - - it('should parse event with schema and safeParse and return error', async () => { + it('returns a parse error when schema validation fails with safeParse enabled', async () => { + // Act & Assess expect( await lambda.handlerWithSchemaAndSafeParse( - { foo: 'bar' } as unknown as ParsedResult, + { foo: 'bar' } as unknown as ParsedResult, {} as Context ) ).toEqual({ @@ -163,29 +113,29 @@ describe('Parser Decorator', () => { }); }); - it('should parse event with envelope and safeParse', async () => { - const testEvent = generateMock(TestSchema); - const event = getTestEvent({ - eventsPath: 'eventbridge', - filename: 'base', - }); - event.detail = testEvent; + it('parses the event with envelope and safeParse', async () => { + // Prepare + const event = structuredClone(baseEvent); + event.detail = payload; - const resp = await lambda.harndlerWithEnvelopeAndSafeParse( - event as unknown as ParsedResult, + // Act + const result = await lambda.harndlerWithEnvelopeAndSafeParse( + event as unknown as ParsedResult, {} as Context ); - expect(resp).toEqual({ + // Assess + expect(result).toEqual({ success: true, - data: testEvent, + data: event.detail, }); }); - it('should parse event with envelope and safeParse and return error', async () => { + it('returns a parse error when schema/envelope validation fails with safeParse enabled', async () => { + // Act & Assess expect( await lambda.harndlerWithEnvelopeAndSafeParse( - { foo: 'bar' } as unknown as ParsedResult, + { foo: 'bar' } as unknown as ParsedResult, {} as Context ) ).toEqual({ From d859acd31667e0bb43592c1099300e24317cdf2e Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 24 Jan 2025 09:06:25 +0100 Subject: [PATCH 3/3] chore: remove unused import --- packages/parser/tests/unit/schema/ses.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/parser/tests/unit/schema/ses.test.ts b/packages/parser/tests/unit/schema/ses.test.ts index 979e4091f6..3829dd8362 100644 --- a/packages/parser/tests/unit/schema/ses.test.ts +++ b/packages/parser/tests/unit/schema/ses.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest'; import { SesSchema } from '../../../src/schemas/ses.js'; import type { SesEvent } from '../../../src/types/index.js'; -import { getTestEvent, omit } from './utils.js'; +import { getTestEvent } from './utils.js'; describe('Schema: SES', () => { const baseEvent = getTestEvent({